-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathacn.py
More file actions
343 lines (218 loc) · 8.63 KB
/
acn.py
File metadata and controls
343 lines (218 loc) · 8.63 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Associative Compression Network based on https://arxiv.org/pdf/1804.02476v2.pdf
This is a VAE with a conditional prior.
"""
import os.path as osp
import random
import numpy as np
import torch
from sklearn.neighbors import KNeighborsClassifier
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
CODE_LEN = 20
batch_size = 128
FPATH_VAE = osp.join('models', 'vae.params')
FPATH_PRIOR = osp.join('models', 'prior.params')
data_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True)
class VAE(torch.nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(28 * 28, 512)
self.fc2_u = nn.Linear(512, CODE_LEN)
self.fc2_logstd = nn.Linear(512, CODE_LEN)
self.fc3 = nn.Linear(CODE_LEN, 512)
self.fc4 = nn.Linear(512, 28 * 28)
def encode(self, inputs: torch.Tensor):
h1 = F.relu(self.fc1(inputs))
mu, logstd = self.fc2_u(h1), self.fc2_logstd(h1)
return mu, logstd
def decode(self, latent: torch.Tensor):
h3 = F.relu(self.fc3(latent))
h4 = F.sigmoid(self.fc4(h3))
return h4
def forward(self, inputs: torch.Tensor):
u, logstd = self.encode(inputs)
h2 = self.reparametrize(u, logstd)
output = self.decode(h2)
return output, u, logstd
def reparametrize(self,
u: torch.Tensor,
s: torch.Tensor):
"""
Draw from standard normal distribution (as input) and then parametrize it (so params can be backpropped).
Args:
u: Means.
s: Log standard deviations.
Returns: Draws from a distribution.
"""
if self.training:
std = s.exp()
activations = u + std * torch.randn(u.shape)
return activations
return u
class PriorNetwork(torch.nn.Module):
def __init__(self, k=5):
"""
Args:
k: Number of neighbors to choose from when picking code to condition prior.
"""
super().__init__()
self.fc1 = nn.Linear(CODE_LEN, 512)
self.fc2_u = nn.Linear(512, CODE_LEN)
self.fc2_s = nn.Linear(512, CODE_LEN)
self.k = k
self.knn = KNeighborsClassifier(n_neighbors=2 * k)
codes = torch.randn((len(data_loader.dataset), CODE_LEN)).numpy()
self.fit_knn(codes)
def pick_close_neighbor(self, code: torch.Tensor) -> torch.Tensor:
"""
K-nearest neighbors to choose a close code. This emulates an ordering of the original data.
Args:
code: Latent activations of current training example.
Returns: Numpy array of same dimension as code.
"""
# TODO(jalex): This is slow - can I make it faster by changing search algo/leaf size?
neighbor_idxs = self.knn.kneighbors([code.detach().numpy()], return_distance=False)[0]
valid_idxs = [n for n in neighbor_idxs if n not in self.seen]
if len(valid_idxs) < self.k:
codes_new = [c for i, c in enumerate(self.codes) if i not in self.seen]
self.fit_knn(codes_new)
return self.pick_close_neighbor(code)
neighbor_codes = [self.codes[idx] for idx in valid_idxs]
if len(neighbor_codes) > self.k:
code_np = code.detach().numpy()
# this is same metric KNN uses
neighbor_codes = sorted(neighbor_codes, key=lambda n: ((code_np - n) ** 2).sum())[:self.k]
neighbor = random.choice(neighbor_codes)
return neighbor
def fit_knn(self, codes: np.ndarray):
"""
Reset the KNN. This can be used when we get too many misses or want to update the codes.
Args:
codes: New codes to fit.
"""
self.codes = codes
self.seen = set()
y = [0] * len(codes)
self.knn.fit(codes, y)
def forward(self, codes: torch.Tensor):
"""
Calculate prior conditioned on codes.
Args:
codes: latent activations.
Returns: Two parameters each of dimensionality codes. These can be used as mu, std for a Gaussian.
"""
# Can use this to emulate uncoditional prior
# return torch.zeros(codes.shape[0], 1), torch.ones(codes.shape[0], 1)
previous_codes = [self.pick_close_neighbor(c) for c in codes]
previous_codes = torch.tensor(previous_codes)
return self.encode(previous_codes)
def encode(self, prev_code: torch.Tensor):
h1 = F.relu(self.fc1(prev_code))
mu, logstd = self.fc2_u(h1), self.fc2_s(h1)
return mu, logstd
def calc_loss(x, recon, u_q, s_q, u_p, s_p):
"""
Loss derived from variational lower bound (ELBO) or information theory (see bits-back for details).
The loss comprises two parts:
1. Reconstruction loss (how good the VAE is at reproducing the output).
2. The coding cost (KL divergence between the model posterior and conditional prior).
Args:
x: Inputs.
recon: Reconstruction from a VAE.
u_q: Mean of model posterior.
s_q: Log std of model posterior.
u_p: Mean of (conditional) prior.
s_p: Log std of (conditional) prior.
Returns: Loss.
"""
# reconstruction
xent = F.binary_cross_entropy(recon, x, size_average=False)
# coding cost
dkl = torch.sum(s_p - s_q - 0.5 +
((2 * s_q).exp() + (u_q - u_p).pow(2)) /
(2 * (2 * s_p).exp()))
return xent + dkl
def train(vae: VAE,
prior: PriorNetwork,
optimizer: torch.optim.Optimizer,
idx_epoch: int):
vae.train()
train_loss = 0
new_codes = []
for batch_idx, (data, _) in enumerate(data_loader):
inputs = data.view(data.shape[0], -1)
optimizer.zero_grad()
outputs, u_q, s_q = vae(inputs)
new_codes.append(u_q)
u_p, s_p = prior(u_q)
loss = calc_loss(inputs, outputs, u_q, s_q, u_p, s_p)
loss.backward()
train_loss += loss.item()
optimizer.step()
if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
idx_epoch, batch_idx * len(data), len(data_loader.dataset), 100 * batch_idx / len(data_loader),
loss.item() / len(data)))
torch.save(vae.state_dict(), FPATH_VAE)
torch.save(prior.state_dict(), FPATH_PRIOR)
new_codes = torch.cat(new_codes).detach().numpy()
prior.fit_knn(new_codes)
print('====> Epoch: {} Average loss: {:.4f}'.format(
idx_epoch, train_loss / len(data_loader.dataset)))
def daydream(image: torch.Tensor,
vae: VAE,
prior: PriorNetwork,
num_iters: int=0,
save_gif: bool=False):
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
plt.axis('off')
mpl_images = []
i = 0
while i < num_iters or not num_iters:
image_np = image.detach().numpy().reshape(28, 28)
x = plt.imshow(image_np, cmap='Greys', animated=True)
mpl_images.append([x])
if not num_iters:
plt.axis('off')
plt.show()
latent, _ = vae.encode(image)
# Generate guess of next latent
next_latent, _ = prior.encode(latent)
image = vae.decode(next_latent)
image = torch.tensor(image.reshape(1, -1))
i += 1
ani = animation.ArtistAnimation(fig, mpl_images, interval=200, blit=True)
plt.show()
if save_gif:
ani.save('animation.gif', writer='imagemagick', fps=5)
def parse_flags():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--task', default='train', choices=['train', 'daydream'], help='Task to do.')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_flags()
vae = VAE()
prior = PriorNetwork()
if args.task == 'train':
params = list(vae.parameters()) + list(prior.parameters())
optimizer = optim.Adam(params)
for i in range(100):
train(vae, prior, optimizer, i)
else:
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True)
img, _ = next(iter(test_loader))
vae.load_state_dict(torch.load(FPATH_VAE))
prior.load_state_dict(torch.load(FPATH_PRIOR))
img_ = img[0].view(1, -1)
daydream(img_, vae, prior, num_iters=0)