-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvae.py
310 lines (252 loc) · 11.3 KB
/
cvae.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from torchvision import datasets, transforms
import torch
import torch.utils.data
from torch import nn, optim
from torch.autograd import Variable
from torch.nn import functional as F
def hello():
print('Hello from cvae.py!')
class CVAE(nn.Module):
def __init__(self, *,
input_size, latent_size, num_classes, hidden_units=400):
super().__init__()
self.input_size = input_size
self.num_classes = num_classes
self.latent_size = latent_size
self.units = hidden_units
#######################################################
### START OF YOUR CODE ###
#######################################################
### Define a three layer neural network architecture
### for the recognition_model. You are free to choose
### any reasonable architecture and implementation
### details that would make CVAE work.
#######################################################
# ~~START DELETE~~
self.hidden_dim = self.units
# Encoder
layers = []
layers.append(nn.Linear(self.input_size + self.num_classes, self.hidden_dim))
layers.append(nn.ReLU())
layers.append(nn.Linear(self.hidden_dim, self.hidden_dim))
layers.append(nn.ReLU())
layers.append(nn.Linear(self.hidden_dim, self.hidden_dim))
layers.append(nn.ReLU())
self.encoder = nn.Sequential(*layers)
self.mu_layer = nn.Linear(self.hidden_dim + self.num_classes, self.latent_size)
self.logvar_layer = nn.Linear(self.hidden_dim + self.num_classes, self.latent_size)
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
#######################################################
### START OF YOUR CODE ###
#######################################################
# Define a three layer neural network architecture for
# the generation_model. You are free to choose
# architectures and layers as you wish.
# ~~START DELETE~~
# Decoder
layers2 = []
layers2.append(nn.Linear(self.latent_size + self.num_classes, self.hidden_dim))
layers2.append(nn.ReLU())
layers2.append(nn.Linear(self.hidden_dim, self.hidden_dim))
layers2.append(nn.ReLU())
layers2.append(nn.Linear(self.hidden_dim, self.hidden_dim))
layers2.append(nn.ReLU())
layers2.append(nn.Linear(self.hidden_dim, self.input_size))
layers2.append(nn.Sigmoid())
H = int(np.sqrt(self.input_size))
layers2.append(nn.Unflatten(1, (1, H, H)))
self.decoder = nn.Sequential(*layers2)
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
def recognition_model(self, x, c):
"""
Computes the parameters of the posterior distribution q(z | x, c) using the
recognition network defined in the constructor.
Inputs:
- x: PyTorch tensor of shape (batch_size, input_size) for the input data
- c: PyTorch tensor of shape (batch_size, num_classes) for the input data class
Returns:
- mu: PyTorch tensor of shape (batch_size, latent_size) for the posterior mu
- logvar: PyTorch tensor of shape (batch_size, latent_size) for the posterior
variance in log space
"""
#######################################################
### START OF YOUR CODE ###
#######################################################
# ~~START DELETE~~
x_flat = torch.flatten(x, start_dim=1)
x_concat = torch.cat((x_flat, c), 1)
encoding = self.encoder(x_concat)
encoding_c = torch.cat((encoding, c), 1)
mu = self.mu_layer(encoding_c)
logvar = self.logvar_layer(encoding_c)
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
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) + mu
def generation_model(self, z, c): # P(x|z, c)
"""
Computes the generation output from the generative distribution p(x | z, c)
using the generation network defined in the constructor
Inputs:
- z: PyTorch tensor of shape (batch_size, latent_size) for the latent vector
- c: PyTorch tensor of shape (batch_size, num_classes) for the input data class
Returns:
- x_hat: PyTorch tensor of shape (batch_size, input_size) for the generated data
"""
#######################################################
### START OF YOUR CODE ###
#######################################################
# ~~START DELETE~~
z_c = torch.cat((z, c), 1)
x_hat = self.decoder(z_c)
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
return x_hat
def forward(self, x, c):
"""
Performs the inference and generation steps of the CVAE model using
the recognition_model, reparametrization trick, and generation_model
Inputs:
- x: PyTorch tensor of shape (batch_size, input_size) for the input data
- c: PyTorch tensor of shape (batch_size, num_classes) for the input data class
Returns:
- x_hat: PyTorch tensor of shape (batch_size, input_size) for the generated data
- mu: PyTorch tensor of shape (batch_size, latent_size) for the posterior mu
- logvar: PyTorch tensor of shape (batch_size, latent_size)
for the posterior logvar
"""
#######################################################
### START OF YOUR CODE ###
#######################################################
# ~~START DELETE~~
mu, logvar = self.recognition_model(x, c)
z = self.reparametrize(mu, logvar)
x_hat = self.generation_model(z, c)
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
return x_hat, mu, logvar
def to_var(x, use_cuda):
x = Variable(x)
if use_cuda:
x = x.cuda()
return x
def one_hot(labels, class_size, use_cuda):
targets = torch.zeros(labels.size(0), class_size)
for i, label in enumerate(labels):
targets[i, label] = 1
return to_var(targets, use_cuda)
def train(epoch, model, train_loader, optimizer, num_classes, use_cuda):
model.train()
train_loss = 0
for batch_idx, (data, labels) in enumerate(train_loader):
data = to_var(data, use_cuda).view(data.shape[0], -1)
labels = one_hot(labels, num_classes, use_cuda)
recon_batch, mu, logvar = model(data, labels)
optimizer.zero_grad()
loss = loss_function(recon_batch, data, mu, logvar)
loss.backward()
train_loss += loss.data
optimizer.step()
if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.data))
def loss_function(x_hat, x, mu, logvar):
"""
Computes the negative variational lowerbound averaged over the minibatch for conditional vae
Note: We compute -lowerbound because we optimize the network by minimizing a loss
Inputs:
- x_hat: PyTorch tensor of shape (batch_size, input_size) for the generated data
- x: PyTorch tensor of shape (batch_size, input_size) for the real data
- mu: PyTorch tensor of shape (batch_size, latent_size) for the posterior mu
- logvar: PyTorch tensor of shape (batch_size, latent_size) for the posterior logvar
Returns:
- loss: PyTorch tensor containing the (scalar) loss for the negative lowerbound.
"""
#######################################################
### START OF YOUR CODE ###
#######################################################
# ~~START DELETE~~
N, Z = mu.shape
x_hat = x_hat.view(N, -1)
ce_loss = torch.nn.functional.binary_cross_entropy(x_hat, x, reduction="sum")
kl_loss = 1 + logvar - mu ** 2 - torch.exp(logvar)
kl_loss = 0.5 * torch.sum(kl_loss)
# Why is it 2 * ce_loss, and divided by 2 not 1
# TODO(Jongwook): Verify this quesiton
loss = (ce_loss - kl_loss) / N
# ~~END DELETE~~
#######################################################
### END OF YOUR CODE ###
#######################################################
return loss
# See cvae.ipynb as well; you can either run the code through juypter notebook
# or directly execute this python code as a script.
use_cuda = False
batch_size = 32
input_size = 28 * 28
hidden_units = 400
latent_size = 20 # z dim
num_classes = 10
num_epochs = 10
def main():
# Load MNIST dataset
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
dataset = datasets.MNIST(
'./data', train=True, download=True,
transform=transforms.ToTensor())
train_dataset = torch.utils.data.Subset(dataset, indices=range(10000))
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True, **kwargs)
model = CVAE(
input_size=input_size,
latent_size=latent_size,
num_classes=num_classes,
hidden_units=hidden_units,
)
if use_cuda:
model.cuda()
# Note: You will get an ValueError here if you haven't implemented anything
optimizer = optim.Adam(model.parameters(), lr=1e-3)
start = time.time()
for epoch in range(1, num_epochs+1):
train(epoch, model, train_loader, optimizer, num_classes, use_cuda)
print('training time = %f' % (time.time() - start)) # should take less than 5 minutes
# Generate images with condition labels
c = torch.eye(num_classes, num_classes) # [one hot labels for 0-9]
c = to_var(c, use_cuda)
z = to_var(torch.randn(num_classes, latent_size), use_cuda)
samples = model.generation_model(z, c).data.cpu().numpy()
fig = plt.figure(figsize=(10, 1))
gs = gridspec.GridSpec(1, 10)
gs.update(wspace=0.05, hspace=0.05)
for i, sample in enumerate(samples):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([]) # type: ignore
ax.set_yticklabels([]) # type: ignore
ax.set_aspect('equal') # type: ignore
plt.imshow(sample.reshape(28, 28), cmap='Greys_r')
plt.show()
if __name__ == '__main__':
main()