-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconditional_vae.py
261 lines (188 loc) · 7.28 KB
/
conditional_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
''' This code contains the implementation of conditional VAE
'''
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
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 = 75 # latent vector dimension
N_CLASSES = 10 # number of classes in the data
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)
def idx2onehot(idx, n=N_CLASSES):
assert idx.shape[1] == 1
assert torch.max(idx).item() < n
onehot = torch.zeros(idx.size(0), n)
onehot.scatter_(1, idx.data, 1)
return onehot
class Encoder(nn.Module):
''' This the encoder part of VAE
'''
def __init__(self, input_dim, hidden_dim, latent_dim, n_classes):
'''
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.
latent_dim: A integer indicating the latent size.
n_classes: A integer indicating the number of classes. (dimension of one-hot representation of labels)
'''
super().__init__()
self.linear = nn.Linear(input_dim + n_classes, hidden_dim)
self.mu = nn.Linear(hidden_dim, latent_dim)
self.var = nn.Linear(hidden_dim, latent_dim)
def forward(self, x):
# x is of shape [batch_size, input_dim + n_classes]
hidden = F.relu(self.linear(x))
# hidden is of shape [batch_size, hidden_dim]
# latent parameters
mean = self.mu(hidden)
# mean is of shape [batch_size, latent_dim]
log_var = self.var(hidden)
# log_var is of shape [batch_size, latent_dim]
return mean, log_var
class Decoder(nn.Module):
''' This the decoder part of VAE
'''
def __init__(self, latent_dim, hidden_dim, output_dim, n_classes):
'''
Args:
latent_dim: A integer indicating the latent size.
hidden_dim: A integer indicating the size of hidden dimension.
output_dim: A integer indicating the size of output (in case of MNIST 28 * 28).
n_classes: A integer indicating the number of classes. (dimension of one-hot representation of labels)
'''
super().__init__()
self.latent_to_hidden = nn.Linear(latent_dim + n_classes, hidden_dim)
self.hidden_to_out = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# x is of shape [batch_size, latent_dim + num_classes]
x = F.relu(self.latent_to_hidden(x))
# x is of shape [batch_size, hidden_dim]
generated_x = F.sigmoid(self.hidden_to_out(x))
# x is of shape [batch_size, output_dim]
return generated_x
class CVAE(nn.Module):
''' This the VAE, which takes a encoder and decoder.
'''
def __init__(self, input_dim, hidden_dim, latent_dim, n_classes):
'''
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.
latent_dim: A integer indicating the latent size.
n_classes: A integer indicating the number of classes. (dimension of one-hot representation of labels)
'''
super().__init__()
self.encoder = Encoder(input_dim, hidden_dim, latent_dim, n_classes)
self.decoder = Decoder(latent_dim, hidden_dim, input_dim, n_classes)
def forward(self, x, y):
x = torch.cat((x, y), dim=1)
# encode
z_mu, z_var = self.encoder(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)
z = torch.cat((x_sample, y), dim=1)
# decode
generated_x = self.decoder(z)
return generated_x, z_mu, z_var
def calculate_loss(x, reconstructed_x, mean, log_var):
# reconstruction loss
RCL = F.binary_cross_entropy(reconstructed_x, x, size_average=False)
# kl divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
return RCL + KLD
model = CVAE(INPUT_DIM, HIDDEN_DIM, LATENT_DIM, N_CLASSES)
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, y) in enumerate(train_iterator):
# reshape the data into [batch_size, 784]
x = x.view(-1, 28 * 28)
x = x.to(device)
# convert y into one-hot encoding
y = idx2onehot(y.view(-1, 1))
y = y.to(device)
# update the gradients to zero
optimizer.zero_grad()
# forward pass
reconstructed_x, z_mu, z_var = model(x, y)
# loss
loss = calculate_loss(x, reconstructed_x, z_mu, z_var)
# 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, y) in enumerate(test_iterator):
# reshape the data
x = x.view(-1, 28 * 28)
x = x.to(device)
# convert y into one-hot encoding
y = idx2onehot(y.view(-1, 1))
y = y.to(device)
# forward pass
reconstructed_x, z_mu, z_var = model(x, y)
# loss
loss = calculate_loss(x, reconstructed_x, z_mu, z_var)
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
# create a random latent vector
z = torch.randn(1, LATENT_DIM).to(device)
# pick randomly 1 class, for which we want to generate the data
y = torch.randint(0, N_CLASSES, (1, 1)).to(dtype=torch.long)
print(f'Generating a {y.item()}')
y = idx2onehot(y).to(device, dtype=z.dtype)
z = torch.cat((z, y), dim=1)
reconstructed_img = model.decoder(z)
img = reconstructed_img.view(28, 28).data
plt.figure()
plt.imshow(img, cmap='gray')
plt.show()