1.pytorch中给出的例子
https://github.com/pytorch/examples/blob/master/vae/main.py
实现过程非常简单:
class VAE(nn.Module): def __init__(self): super(VAE, self).__init__() self.fc1 = nn.Linear(784, 400)#第一层,推断 self.fc21 = nn.Linear(400, 20)#对应均值 self.fc22 = nn.Linear(400, 20)#对应方差 self.fc3 = nn.Linear(20, 400)#生成层1 self.fc4 = nn.Linear(400, 784)#生成层2 def encode(self, x): h1 = F.relu(self.fc1(x)) return self.fc21(h1), self.fc22(h1) def reparameterize(self, mu, logvar): std = torch.exp(0.5*logvar) eps = torch.randn_like(std) return mu + eps*std def decode(self, z): h3 = F.relu(self.fc3(z)) return torch.sigmoid(self.fc4(h3))#这里为什么选sigmoid而不是其他,需要斟酌 def forward(self, x): mu, logvar = self.encode(x.view(-1, 784)) z = self.reparameterize(mu, logvar)#对均值和方差进行重参数 return self.decode(z), mu, logvar
那我不明白了,这个https://github.com/wiseodd/generative-models里给的这些VAE实现有什么意义呢?还很难看懂
2.torch中Variable已弃用
来源:https://www.cnblogs.com/BlueBlueSea/p/12289801.html