-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
409 lines (323 loc) · 13.3 KB
/
Copy pathutils.py
File metadata and controls
409 lines (323 loc) · 13.3 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (c) 2018 Rui Shu
import numpy as np
import os
import shutil
import sys
import torch
# import tensorflow as tf
# from vaes.gmvae import GMVAE
# from vaes.ssvae import SSVAE
# from vaes.vae import VAE
from torch.nn import functional as F
from torchvision import datasets, transforms
bce = torch.nn.BCEWithLogitsLoss(reduction='none')
def compute_mix_gaussian(gravity_z_pre, k_prob):
# k_prob -> (batch_size, 3)
# gravity_z_pre -> (1, 2, 3, z_dim)
batch_size = k_prob.size(0)
m, v = gaussian_parameters(gravity_z_pre, dim=1) # (1, 1, 3, z_dim)
m, v = torch.squeeze(m, dim=0), torch.squeeze(v, dim=0) # (1, 3, z_dim)
m, v = duplicate(m, batch_size), duplicate(v, batch_size) # (batch_size, 3, z_dim)
_m = (m * torch.unsqueeze(k_prob, dim=-1)).sum(dim=1) # (batch_size, z_dim)
_v = ((torch.mul(m, m) + v) * torch.unsqueeze(k_prob, dim=-1)).sum(dim=1) - torch.mul(_m, _m)
return _m, _v
################################################################################
# Please familiarize yourself with the code below.
#
# Note that the notation is
# argument: argument_type: argument_shape
#
# Furthermore, the expected argument_shape is only a guideline. You're free to
# pass in inputs that violate the expected argument_shape provided you know
# what you're doing
################################################################################
def sample_gaussian(m, v):
"""
Element-wise application reparameterization trick to sample from Gaussian
Args:
m: tensor: (batch, ...): Mean
v: tensor: (batch, ...): Variance
Return:
z: tensor: (batch, ...): Samples
"""
################################################################################
# TODO: Modify/complete the code here
# Sample z
################################################################################
dist = torch.distributions.normal.Normal(m, torch.sqrt(v))
z = dist.rsample()
################################################################################
# End of code modification
################################################################################
return z
def log_normal(x, m, v):
"""
Computes the elem-wise log probability of a Gaussian and then sum over the
last dim. Basically we're assuming all dims are batch dims except for the
last dim.
Args:
x: tensor: (batch_1, batch_2, ..., batch_k, dim): Observation
m: tensor: (batch_1, batch_2, ..., batch_k, dim): Mean
v: tensor: (batch_1, batch_2, ..., batch_k, dim): Variance
Return:
log_prob: tensor: (batch_1, batch_2, ..., batch_k): log probability of
each sample. Note that the summation dimension is not kept
"""
element_wise = -0.5*(torch.log(v)+(x - m).pow(2)/v + np.log(2*np.pi))
log_prob = element_wise.sum(-1)
return log_prob
def log_normal_mixture(z, m, v):
"""
Computes log probability of a uniformly-weighted Gaussian mixture.
Args:
z: tensor: (batch, dim): Observations
m: tensor: (batch, mix, dim): Mixture means
v: tensor: (batch, mix, dim): Mixture variances
Return:
log_prob: tensor: (batch,): log probability of each sample
"""
################################################################################
# TODO: Modify/complete the code here
# Compute the uniformly-weighted mixture of Gaussians density for each sample
# in the batch
################################################################################
log_probs = log_normal(z.unsqueeze(1), m, v)
log_prob = log_mean_exp(log_probs, -1)
################################################################################
# End of code modification
################################################################################
return log_prob
def gaussian_parameters(h, dim=-1):
"""
Converts generic real-valued representations into mean and variance
parameters of a Gaussian distribution
Args:
h: tensor: (batch, ..., dim, ...): Arbitrary tensor
dim: int: (): Dimension along which to split the tensor for mean and
variance
Returns:
m: tensor: (batch, ..., dim / 2, ...): Mean
v: tensor: (batch, ..., dim / 2, ...): Variance
"""
m, h = torch.split(h, h.size(dim) // 2, dim=dim)
v = F.softplus(h) + 1e-8
return m, v
def log_bernoulli_with_logits(x, logits):
"""
Computes the log probability of a Bernoulli given its logits
Args:
x: tensor: (batch, dim): Observation
logits: tensor: (batch, dim): Bernoulli logits
Return:
log_prob: tensor: (batch,): log probability of each sample
"""
log_prob = -bce(input=logits, target=x).sum(-1)
return log_prob
def kl_cat(q, log_q, log_p):
"""
Computes the KL divergence between two categorical distributions
Args:
q: tensor: (batch, dim): Categorical distribution parameters
log_q: tensor: (batch, dim): Log of q
log_p: tensor: (batch, dim): Log of p
Return:
kl: tensor: (batch,) kl between each sample
"""
element_wise = (q * (log_q - log_p))
kl = element_wise.sum(-1)
return kl
def kl_normal(qm, qv, pm, pv):
"""
Computes the elem-wise KL divergence between two normal distributions KL(q || p) and
sum over the last dimension
Args:
qm: tensor: (batch, dim): q mean
qv: tensor: (batch, dim): q variance
pm: tensor: (batch, dim): p mean
pv: tensor: (batch, dim): p variance
Return:
kl: tensor: (batch,): kl between each sample
"""
element_wise = 0.5 * (torch.log(pv) - torch.log(qv) + qv / pv + (qm - pm).pow(2) / pv - 1)
kl = element_wise.sum(-1)
return kl
def duplicate(x, rep):
"""
Duplicates x along dim=0
Args:
x: tensor: (batch, ...): Arbitrary tensor
rep: int: (): Number of replicates. Setting rep=1 returns orignal x
Returns:
_: tensor: (batch * rep, ...): Arbitrary replicated tensor
"""
return x.expand(rep, *x.shape).reshape(-1, *x.shape[1:])
def log_mean_exp(x, dim):
"""
Compute the log(mean(exp(x), dim)) in a numerically stable manner
Args:
x: tensor: (...): Arbitrary tensor
dim: int: (): Dimension along which mean is computed
Return:
_: tensor: (...): log(mean(exp(x), dim))
"""
return log_sum_exp(x, dim) - np.log(x.size(dim))
def log_sum_exp(x, dim=0):
"""
Compute the log(sum(exp(x), dim)) in a numerically stable manner
Args:
x: tensor: (...): Arbitrary tensor
dim: int: (): Dimension along which sum is computed
Return:
_: tensor: (...): log(sum(exp(x), dim))
"""
max_x = torch.max(x, dim)[0]
new_x = x - max_x.unsqueeze(dim).expand_as(x)
return max_x + (new_x.exp().sum(dim)).log()
def load_model_by_name(model, global_step, device=None):
"""
Load a model based on its name model.name and the checkpoint iteration step
Args:
model: Model: (): A model
global_step: int: (): Checkpoint iteration
"""
file_path = os.path.join('checkpoints',
model.name,
'model-{:05d}.pt'.format(global_step))
state = torch.load(file_path, map_location=device)
model.load_state_dict(state)
print("Loaded from {}".format(file_path))
def save_model_by_name(model, global_step):
save_dir = os.path.join('checkpoints', model.name)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
file_path = os.path.join(save_dir, 'model-{:05d}.pt'.format(global_step))
state = model.state_dict()
torch.save(state, file_path)
print('Saved to {}'.format(file_path))
def prepare_writer(model_name, overwrite_existing=False):
log_dir = os.path.join('logs', model_name)
save_dir = os.path.join('checkpoints', model_name)
if overwrite_existing:
delete_existing(log_dir)
delete_existing(save_dir)
# Sadly, I've been told *not* to use tensorflow :<
# writer = tf.summary.FileWriter(log_dir)
writer = None
return writer
def log_summaries(writer, summaries, global_step):
pass # Sad :<
# for tag in summaries:
# val = summaries[tag]
# tf_summary = tf.Summary.Value(tag=tag, simple_value=val)
# writer.add_summary(tf.Summary(value=[tf_summary]), global_step)
# writer.flush()
def delete_existing(path):
if os.path.exists(path):
print("Deleting existing path: {}".format(path))
shutil.rmtree(path)
def reset_weights(m):
try:
m.reset_parameters()
except AttributeError:
pass
def get_mnist_data(device, use_test_subset=True):
preprocess = transforms.ToTensor()
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../DATASET/', train=True, download=True, transform=preprocess),
batch_size=97, # Using a weird batch size to prevent students from hard-coding
shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../DATASET/', train=False, download=True, transform=preprocess),
batch_size=97,
shuffle=True)
# Create pre-processed training and test sets
X_train = train_loader.dataset.train_data.to(device).reshape(-1, 784).float() / 255
y_train = train_loader.dataset.train_labels.to(device)
X_test = test_loader.dataset.test_data.to(device).reshape(-1, 784).float() / 255
y_test = test_loader.dataset.test_labels.to(device)
# Create supervised subset (deterministically chosen)
# This subset will serve dual purpose of log-likelihood evaluation and
# semi-supervised learning. Pretty hacky. Don't judge :<
X = X_test if use_test_subset else X_train
y = y_test if use_test_subset else y_train
xl, yl = [], []
for i in range(10):
idx = y == i
idx_choice = get_mnist_index(i, test=use_test_subset)
xl += [X[idx][idx_choice]]
yl += [y[idx][idx_choice]]
if use_test_subset:
xl = static_binarize(torch.cat(xl)).to(device)
else:
xl = torch.cat(xl).to(device)
yl = torch.cat(yl).to(device)
yl = yl.new(np.eye(10)[yl.cpu()]).to(device)
labeled_subset = (xl, yl)
return train_loader, labeled_subset, (X_test, y_test)
def static_binarize(x):
# torch.bernoulli seeding behavior is different on CPU v GPU
# so we'll convert to numpy array and use binomial to sample static x
with FixedSeed(0):
x = np.random.binomial(1, x.cpu().numpy())
x = torch.FloatTensor(x)
return x
def get_mnist_index(i, test=True):
# Obviously *hand*-coded
train_idx = np.array([[2732,2607,1653,3264,4931,4859,5827,1033,4373,5874],
[5924,3468,6458,705,2599,2135,2222,2897,1701,537],
[2893,2163,5072,4851,2046,1871,2496,99,2008,755],
[797,659,3219,423,3337,2745,4735,544,714,2292],
[151,2723,3531,2930,1207,802,2176,2176,1956,3622],
[3560,756,4369,4484,1641,3114,4984,4353,4071,4009],
[2105,3942,3191,430,4187,2446,2659,1589,2956,2681],
[4180,2251,4420,4870,1071,4735,6132,5251,5068,1204],
[3918,1167,1684,3299,2767,2957,4469,560,5425,1605],
[5795,1472,3678,256,3762,5412,1954,816,2435,1634]])
test_idx = np.array([[684,559,629,192,835,763,707,359,9,723],
[277,599,1094,600,314,705,551,87,174,849],
[537,845,72,777,115,976,755,448,850,99],
[984,177,755,797,659,147,910,423,288,961],
[265,697,639,544,543,714,244,151,675,510],
[459,882,183,28,802,128,128,53,550,488],
[756,273,335,388,617,42,442,543,888,257],
[57,291,779,430,91,398,611,908,633,84],
[203,324,774,964,47,639,131,972,868,180],
[1000,846,143,660,227,954,791,719,909,373]])
if test:
return test_idx[i]
else:
return train_idx[i]
def get_svhn_data(device):
preprocess = transforms.ToTensor()
train_loader = torch.utils.data.DataLoader(
datasets.SVHN('data', split='extra', download=True, transform=preprocess),
batch_size=100,
shuffle=True)
return train_loader, (None, None), (None, None)
def gumbel_softmax(logits, tau, eps=1e-8):
U = torch.rand_like(logits)
gumbel = -torch.log(-torch.log(U + eps) + eps)
y = logits + gumbel
y = F.softmax(y / tau, dim=1)
return y
def evaluate_classifier(model, test_set):
print('*' * 80)
print("CLASSIFICATION EVALUATION ON ENTIRE TEST SET")
print('*' * 80)
X, y = test_set
pred = model.cls.classify(X)
accuracy = (pred.argmax(1) == y).float().mean()
print("Test set classification accuracy: {}".format(accuracy))
class FixedSeed:
def __init__(self, seed):
self.seed = seed
self.state = None
def __enter__(self):
self.state = np.random.get_state()
np.random.seed(self.seed)
def __exit__(self, exc_type, exc_value, traceback):
np.random.set_state(self.state)
# System verifier
if sys.version_info[0] < 3:
raise Exception("Detected unpermitted Python version: Python{}. You should use Python3."
.format(sys.version_info[0]))