-
Notifications
You must be signed in to change notification settings - Fork 107
/
vae.py
100 lines (83 loc) · 2.88 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import torch
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import torchvision
from torchvision import transforms
import torch.optim as optim
from torch import nn
import matplotlib.pyplot as plt
from torch import distributions
class Encoder(torch.nn.Module):
def __init__(self, D_in, H, latent_size):
super(Encoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, H)
self.enc_mu = torch.nn.Linear(H, latent_size)
self.enc_log_sigma = torch.nn.Linear(H, latent_size)
def forward(self, x):
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
mu = self.enc_mu(x)
log_sigma = self.enc_log_sigma(x)
sigma = torch.exp(log_sigma)
return torch.distributions.Normal(loc=mu, scale=sigma)
class Decoder(torch.nn.Module):
def __init__(self, D_in, H, D_out):
super(Decoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, D_out)
def forward(self, x):
x = F.relu(self.linear1(x))
mu = torch.tanh(self.linear2(x))
return torch.distributions.Normal(mu, torch.ones_like(mu))
class VAE(torch.nn.Module):
def __init__(self, encoder, decoder):
super(VAE, self).__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, state):
q_z = self.encoder(state)
z = q_z.rsample()
return self.decoder(z), q_z
transform = transforms.Compose(
[transforms.ToTensor(),
# Normalize the images to be -0.5, 0.5
transforms.Normalize(0.5, 1)]
)
mnist = torchvision.datasets.MNIST('./', download=True, transform=transform)
input_dim = 28 * 28
batch_size = 128
num_epochs = 100
learning_rate = 0.001
hidden_size = 512
latent_size = 8
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
dataloader = torch.utils.data.DataLoader(
mnist, batch_size=batch_size,
shuffle=True,
pin_memory=torch.cuda.is_available())
print('Number of samples: ', len(mnist))
encoder = Encoder(input_dim, hidden_size, latent_size)
decoder = Decoder(latent_size, hidden_size, input_dim)
vae = VAE(encoder, decoder).to(device)
optimizer = optim.Adam(vae.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for data in dataloader:
inputs, _ = data
inputs = inputs.view(-1, input_dim).to(device)
optimizer.zero_grad()
p_x, q_z = vae(inputs)
log_likelihood = p_x.log_prob(inputs).sum(-1).mean()
kl = torch.distributions.kl_divergence(
q_z,
torch.distributions.Normal(0, 1.)
).sum(-1).mean()
loss = -(log_likelihood - kl)
loss.backward()
optimizer.step()
l = loss.item()
print(epoch, l, log_likelihood.item(), kl.item())