-
Notifications
You must be signed in to change notification settings - Fork 11
/
simple_vae.py
235 lines (170 loc) · 5.95 KB
/
simple_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'''This code contains the implementation of simple VAE
Refer to the blog post here: https://graviraja.github.io/vanillavae/
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 64 # number of data points in each batch
N_EPOCHS = 10 # times to run the model on complete data
INPUT_DIM = 28 * 28 # size of each input
HIDDEN_DIM = 256 # hidden dimension
LATENT_DIM = 50 # latent vector dimension
lr = 1e-3 # learning rate
transforms = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.MNIST(
'./data',
train=True,
download=True,
transform=transforms)
test_dataset = datasets.MNIST(
'./data',
train=False,
download=True,
transform=transforms
)
train_iterator = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
test_iterator = DataLoader(test_dataset, batch_size=BATCH_SIZE)
lr = 1e-3
class Encoder(nn.Module):
''' This the encoder part of VAE
'''
def __init__(self, input_dim, hidden_dim, z_dim):
'''
Args:
input_dim: A integer indicating the size of input (in case of MNIST 28 * 28).
hidden_dim: A integer indicating the size of hidden dimension.
z_dim: A integer indicating the latent dimension.
'''
super().__init__()
self.linear = nn.Linear(input_dim, hidden_dim)
self.mu = nn.Linear(hidden_dim, z_dim)
self.var = nn.Linear(hidden_dim, z_dim)
def forward(self, x):
# x is of shape [batch_size, input_dim]
hidden = F.relu(self.linear(x))
# hidden is of shape [batch_size, hidden_dim]
z_mu = self.mu(hidden)
# z_mu is of shape [batch_size, latent_dim]
z_var = self.var(hidden)
# z_var is of shape [batch_size, latent_dim]
return z_mu, z_var
class Decoder(nn.Module):
''' This the decoder part of VAE
'''
def __init__(self, z_dim, hidden_dim, output_dim):
'''
Args:
z_dim: A integer indicating the latent size.
hidden_dim: A integer indicating the size of hidden dimension.
output_dim: A integer indicating the output dimension (in case of MNIST it is 28 * 28)
'''
super().__init__()
self.linear = nn.Linear(z_dim, hidden_dim)
self.out = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# x is of shape [batch_size, latent_dim]
hidden = F.relu(self.linear(x))
# hidden is of shape [batch_size, hidden_dim]
predicted = torch.sigmoid(self.out(hidden))
# predicted is of shape [batch_size, output_dim]
return predicted
class VAE(nn.Module):
def __init__(self, enc, dec):
''' This the VAE, which takes a encoder and decoder.
'''
super().__init__()
self.enc = enc
self.dec = dec
def forward(self, x):
# encode
z_mu, z_var = self.enc(x)
# sample from the distribution having latent parameters z_mu, z_var
# reparameterize
std = torch.exp(z_var / 2)
eps = torch.randn_like(std)
x_sample = eps.mul(std).add_(z_mu)
# decode
predicted = self.dec(x_sample)
return predicted, z_mu, z_var
# encoder
encoder = Encoder(INPUT_DIM, HIDDEN_DIM, LATENT_DIM)
# decoder
decoder = Decoder(LATENT_DIM, HIDDEN_DIM, INPUT_DIM)
# vae
model = VAE(encoder, decoder).to(device)
# optimizer
optimizer = optim.Adam(model.parameters(), lr=lr)
def train():
# set the train mode
model.train()
# loss of the epoch
train_loss = 0
for i, (x, _) in enumerate(train_iterator):
# reshape the data into [batch_size, 784]
x = x.view(-1, 28 * 28)
x = x.to(device)
# update the gradients to zero
optimizer.zero_grad()
# forward pass
x_sample, z_mu, z_var = model(x)
# reconstruction loss
recon_loss = F.binary_cross_entropy(x_sample, x, size_average=False)
# kl divergence loss
kl_loss = 0.5 * torch.sum(torch.exp(z_var) + z_mu**2 - 1.0 - z_var)
# total loss
loss = recon_loss + kl_loss
# backward pass
loss.backward()
train_loss += loss.item()
# update the weights
optimizer.step()
return train_loss
def test():
# set the evaluation mode
model.eval()
# test loss for the data
test_loss = 0
# we don't need to track the gradients, since we are not updating the parameters during evaluation / testing
with torch.no_grad():
for i, (x, _) in enumerate(test_iterator):
# reshape the data
x = x.view(-1, 28 * 28)
x = x.to(device)
# forward pass
x_sample, z_mu, z_var = model(x)
# reconstruction loss
recon_loss = F.binary_cross_entropy(x_sample, x, size_average=False)
# kl divergence loss
kl_loss = 0.5 * torch.sum(torch.exp(z_var) + z_mu**2 - 1.0 - z_var)
# total loss
loss = recon_loss + kl_loss
test_loss += loss.item()
return test_loss
best_test_loss = float('inf')
for e in range(N_EPOCHS):
train_loss = train()
test_loss = test()
train_loss /= len(train_dataset)
test_loss /= len(test_dataset)
print(f'Epoch {e}, Train Loss: {train_loss:.2f}, Test Loss: {test_loss:.2f}')
if best_test_loss > test_loss:
best_test_loss = test_loss
patience_counter = 1
else:
patience_counter += 1
if patience_counter > 3:
break
# sample and generate a image
z = torch.randn(1, LATENT_DIM).to(device)
reconstructed_img = model.dec(z)
img = reconstructed_img.view(28, 28).data
print(z.shape)
print(img.shape)
plt.figure()
plt.imshow(img, cmap='gray')
plt.show()