-
Notifications
You must be signed in to change notification settings - Fork 8
/
linear_vae.py
47 lines (41 loc) · 1.33 KB
/
linear_vae.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import torch
from torch.autograd import Variable
class VAE(torch.nn.Module):
def __init__(self):
super(VAE, self).__init__()
self.encoder = torch.nn.Sequential(
torch.nn.Linear(784, 500),
torch.nn.ReLU(),
torch.nn.Linear(500, 500),
torch.nn.ReLU(),
torch.nn.Linear(500, 200),
torch.nn.ReLU()
)
self.fc1 = torch.nn.Linear(200, 10)
self.fc2 = torch.nn.Linear(200, 10)
self.decoder = torch.nn.Sequential(
torch.nn.Linear(10, 200),
torch.nn.ReLU(),
torch.nn.Linear(200, 500),
torch.nn.ReLU(),
torch.nn.Linear(500, 500),
torch.nn.ReLU(),
torch.nn.Linear(500, 784),
torch.nn.Sigmoid()
)
def encode(self, x):
h = self.encoder(x)
mu = self.fc1(h)
logvar = self.fc2(h)
return mu, logvar
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp()
eps = Variable(std.data.new(std.size()).normal_())
return eps.mul(std).add(mu)
def decode(self, z):
return self.decoder(z)
def forward(self, x):
mu, logvar = self.encode(x.view(-1, 784))
z = self.reparametrize(mu, logvar)
recon_x = self.decode(z)
return recon_x, mu, logvar