-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.py
223 lines (164 loc) · 7.27 KB
/
mnist.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
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data
from torchvision import datasets, transforms
from collections import defaultdict
from hyperspherical_vae.distributions import VonMisesFisher
from hyperspherical_vae.distributions import HypersphericalUniform
train_loader = torch.utils.data.DataLoader(datasets.MNIST('./data', train=True, download=True,
transform=transforms.ToTensor()), batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(datasets.MNIST('./data', train=False, download=True,
transform=transforms.ToTensor()), batch_size=64)
class ModelVAE(torch.nn.Module):
def __init__(self, h_dim, z_dim, activation=F.relu, distribution='normal'):
"""
ModelVAE initializer
:param h_dim: dimension of the hidden layers
:param z_dim: dimension of the latent representation
:param activation: callable activation function
:param distribution: string either `normal` or `vmf`, indicates which distribution to use
"""
super(ModelVAE, self).__init__()
self.z_dim, self.activation, self.distribution = z_dim, activation, distribution
# 2 hidden layers encoder
self.fc_e0 = nn.Linear(784, h_dim * 2)
self.fc_e1 = nn.Linear(h_dim * 2, h_dim)
if self.distribution == 'normal':
# compute mean and std of the normal distribution
self.fc_mean = nn.Linear(h_dim, z_dim)
self.fc_var = nn.Linear(h_dim, z_dim)
elif self.distribution == 'vmf':
# compute mean and concentration of the von Mises-Fisher
self.fc_mean = nn.Linear(h_dim, z_dim)
self.fc_var = nn.Linear(h_dim, 1)
else:
raise NotImplemented
# 2 hidden layers decoder
self.fc_d0 = nn.Linear(z_dim, h_dim)
self.fc_d1 = nn.Linear(h_dim, h_dim * 2)
self.fc_logits = nn.Linear(h_dim * 2, 784)
def encode(self, x):
# 2 hidden layers encoder
x = self.activation(self.fc_e0(x))
x = self.activation(self.fc_e1(x))
if self.distribution == 'normal':
# compute mean and std of the normal distribution
z_mean = self.fc_mean(x)
z_var = F.softplus(self.fc_var(x))
elif self.distribution == 'vmf':
# compute mean and concentration of the von Mises-Fisher
z_mean = self.fc_mean(x)
z_mean = z_mean / z_mean.norm(dim=-1, keepdim=True)
# the `+ 1` prevent collapsing behaviors
z_var = F.softplus(self.fc_var(x)) + 1
else:
raise NotImplemented
return z_mean, z_var
def decode(self, z):
x = self.activation(self.fc_d0(z))
x = self.activation(self.fc_d1(x))
x = self.fc_logits(x)
return x
def reparameterize(self, z_mean, z_var):
if self.distribution == 'normal':
q_z = torch.distributions.normal.Normal(z_mean, z_var)
p_z = torch.distributions.normal.Normal(torch.zeros_like(z_mean), torch.ones_like(z_var))
elif self.distribution == 'vmf':
q_z = VonMisesFisher(z_mean, z_var)
p_z = HypersphericalUniform(self.z_dim - 1)
else:
raise NotImplemented
return q_z, p_z
def forward(self, x):
z_mean, z_var = self.encode(x)
q_z, p_z = self.reparameterize(z_mean, z_var)
z = q_z.rsample()
x_ = self.decode(z)
return (z_mean, z_var), (q_z, p_z), z, x_
def log_likelihood(model, x, n=10):
"""
:param model: model object
:param optimizer: optimizer object
:param n: number of MC samples
:return: MC estimate of log-likelihood
"""
z_mean, z_var = model.encode(x.reshape(-1, 784))
q_z, p_z = model.reparameterize(z_mean, z_var)
z = q_z.rsample(torch.Size([n]))
x_mb_ = model.decode(z)
log_p_z = p_z.log_prob(z)
if model.distribution == 'normal':
log_p_z = log_p_z.sum(-1)
log_p_x_z = -nn.BCEWithLogitsLoss(reduction='none')(x_mb_, x.reshape(-1, 784).repeat((n, 1, 1))).sum(-1)
log_q_z_x = q_z.log_prob(z)
if model.distribution == 'normal':
log_q_z_x = log_q_z_x.sum(-1)
return ((log_p_x_z + log_p_z - log_q_z_x).t().logsumexp(-1) - np.log(n)).mean()
def train(model, optimizer):
for i, (x_mb, y_mb) in enumerate(train_loader):
optimizer.zero_grad()
# dynamic binarization
x_mb = (x_mb > torch.distributions.Uniform(0, 1).sample(x_mb.shape)).float()
_, (q_z, p_z), _, x_mb_ = model(x_mb.reshape(-1, 784))
loss_recon = nn.BCEWithLogitsLoss(reduction='none')(x_mb_, x_mb.reshape(-1, 784)).sum(-1).mean()
if model.distribution == 'normal':
loss_KL = torch.distributions.kl.kl_divergence(q_z, p_z).sum(-1).mean()
elif model.distribution == 'vmf':
loss_KL = torch.distributions.kl.kl_divergence(q_z, p_z).mean()
else:
raise NotImplemented
loss = loss_recon + loss_KL
loss.backward()
optimizer.step()
return(loss_recon.item(), loss_KL.item(), -loss.item())
def test(model, optimizer):
print_ = defaultdict(list)
for x_mb, y_mb in test_loader:
# dynamic binarization
x_mb = (x_mb > torch.distributions.Uniform(0, 1).sample(x_mb.shape)).float()
_, (q_z, p_z), _, x_mb_ = model(x_mb.reshape(-1, 784))
print_['recon loss'].append(float(nn.BCEWithLogitsLoss(reduction='none')(x_mb_,
x_mb.reshape(-1, 784)).sum(-1).mean().data))
if model.distribution == 'normal':
print_['KL'].append(float(torch.distributions.kl.kl_divergence(q_z, p_z).sum(-1).mean().data))
elif model.distribution == 'vmf':
print_['KL'].append(float(torch.distributions.kl.kl_divergence(q_z, p_z).mean().data))
else:
raise NotImplemented
print_['ELBO'].append(- print_['recon loss'][-1] - print_['KL'][-1])
print_['LL'].append(float(log_likelihood(model, x_mb).data))
print({k: np.mean(v) for k, v in print_.items()})
return(print_)
# hidden dimension and dimension of latent space
H_DIM = 128
Z_DIM = 2
# normal VAE
modelN = ModelVAE(h_dim=H_DIM, z_dim=Z_DIM, distribution='normal')
optimizerN = optim.Adam(modelN.parameters(), lr=1e-3)
# print('##### Normal VAE #####')
# # training for 1 epoch
# train(modelN, optimizerN)
# # test
# test(modelN, optimizerN)
# print()
# hyper-spherical VAE
modelS = ModelVAE(h_dim=H_DIM, z_dim=Z_DIM + 1, distribution='vmf')
optimizerS = optim.Adam(modelS.parameters(), lr=1e-3)
print('##### Hyper-spherical VAE #####')
# training for 1 epoch
N_EPOCHS = 100
res = np.zeros((N_EPOCHS, 3))
for ep in range(N_EPOCHS):
recon, kl, elbo = train(modelS, optimizerS)
res[ep] = [recon, kl, elbo]
if ep % 5 == 0:
print(f"Epoch {ep} : loss_recon={recon}, KL={kl}, ELBO={elbo}")
df_res = pd.DataFrame(res, columns=['recon loss', 'KL loss', 'ELBO'])
df_res.to_csv("training_perf.csv")
# test
test_perf = test(modelS, optimizerS)
np.save('test_perf.npy', test_perf)