-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvae.py
342 lines (293 loc) · 12.6 KB
/
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
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
import torch
from torch import nn
from likelihoods import GaussianLikelihood, BernoulliLikelihood
from posteriors import DiagonalGaussian, FullCovarianceGaussian
from networks import MLP, EncodingCNN, DecodingCNN
from typing import Tuple, Optional, List
class VAE(nn.Module):
"""Represents a Variational Autoencoder that acts on image data
Args:
image_dims: the image dimensions
colour_channels: the number of colour channels in the image data
latent_dim: the dimension of the latent variable/code
enc_architecture: either 'cnn' or 'mlp' to define encoder architecture
dec_architecture: either 'cnn' or 'mlp' to define decoder architecture
enc_mlp_hidden_dims: the dimensions of the hidden layers of encoding MLP
enc_cnn_chans: the channels in each layer of the encoding CNN
dec_mlp_hidden_dims: the dimensions of the hidden layers of decoding MLP
dec_cnn_chans: the channels in each layer of the decoding CNN
kernel_size: the size of kernel used in encoding and decoding CNNs
posterior_form: the choice of variational posterior. See posteriors.py
likelihood: the choice of likelihood function. See likelihoods.py
noise: the choice of hetero- or homo- scedastic noise under a Gaussian likelihood
noise_std: the initial standard deviation used if the noise is homoscedastic
train_noise: an option to train the homoscedastic noise variance
prior_std: the std of the isotropic Guassian prior. User-defined hyperparameter
likelihood_activation: the activation function applied to mean of Gaussian likelihood
nonlinearity: the inter-layer activation function of encoding and decoding networks
"""
def __init__(
self,
image_dims: Tuple[int] = (28, 28),
colour_channels: int = 1,
latent_dim: int = 16,
enc_architecture: str = "cnn",
dec_architecture: str = "cnn",
enc_mlp_hidden_dims: Optional[List[int]] = [50, 50],
enc_cnn_chans: Optional[List[int]] = [32, 64],
dec_mlp_hidden_dims: Optional[List[int]] = [50, 50],
dec_cnn_chans: Optional[List[int]] = [32, 64],
kernel_size: Optional[int] = 4,
posterior_form: str = "diagonal_gaussian", # 'full_covariance_gaussian',
likelihood: str = "gaussian", # 'bernoulli'
noise: str = "heteroscedastic", # 'homoscedastic'
noise_std: float = 0.1,
train_noise: bool = False,
prior_std: torch.Tensor = torch.tensor(1.0),
likelihood_activation: nn.Module = nn.Sigmoid(),
nonlinearity: nn.Module = nn.ReLU(),
):
super().__init__()
assert len(image_dims) == 2
self.image_dims = image_dims
self.colour_channels = colour_channels
self.num_pixels = image_dims[0] * image_dims[1]
self.latent_dim = latent_dim
self.posterior_form = posterior_form
self.prior_std = prior_std
self.nonlinearity = nonlinearity
if posterior_form == "diagonal_gaussian":
self.posterior = DiagonalGaussian()
elif posterior_form == "full_covariance_gaussian":
self.posterior = FullCovarianceGaussian(latent_dim)
else:
raise NotImplementedError("Selected posterior not yet implemented")
if likelihood == "gaussian":
self.likelihood = GaussianLikelihood(
noise=noise,
std=noise_std,
train_noise=train_noise,
activation=likelihood_activation,
)
elif likelihood == "bernoulli":
self.likelihood = BernoulliLikelihood()
else:
raise NotImplementedError("Likelihood chosen not yet implemented")
self.encoder = Encoder(
architecture=enc_architecture,
image_dims=image_dims,
colour_channels=colour_channels,
mlp_hidden_dims=enc_mlp_hidden_dims,
cnn_chans=enc_cnn_chans,
kernel_size=kernel_size,
latent_dim=latent_dim,
posterior=self.posterior,
nonlinearity=nonlinearity,
)
self.decoder = Decoder(
architecture=dec_architecture,
image_dims=image_dims,
colour_channels=colour_channels,
mlp_hidden_dims=dec_mlp_hidden_dims,
cnn_chans=dec_cnn_chans,
kernel_size=kernel_size,
latent_dim=latent_dim,
likelihood=self.likelihood,
nonlinearity=nonlinearity,
)
self.prior = torch.distributions.MultivariateNormal(
loc=torch.zeros(latent_dim),
covariance_matrix=prior_std**2 * torch.eye(latent_dim),
)
def forward(self, x: torch.Tensor, num_samples: int = 1):
assert len(x.shape) == 4
assert x.shape[1] == self.image_dims[0] and x.shape[2] == self.image_dims[1]
assert x.shape[-1] == self.colour_channels
batch_size = x.shape[0]
q = self.encoder(x)
z = q.rsample(sample_shape=torch.Size([num_samples])).permute(
1, 0, 2
) # reparameterisation trick implemented implicitly here
assert len(z.shape) == 3
assert z.shape[0] == batch_size
assert z.shape[1] == num_samples
assert z.shape[2] == self.latent_dim
l = self.decoder(z)
return q, z, l
def elbo(
self,
x: torch.Tensor,
num_samples: int = 1,
beta_nll: bool = False,
beta: float = 0.5,
):
repeated_x = x.clone().unsqueeze(1).repeat(1, num_samples, 1, 1, 1)
metrics = {}
q, _, l = self(x, num_samples=num_samples)
kl = torch.distributions.kl.kl_divergence(q, self.prior)
exp_ll = l.log_prob(repeated_x)
if isinstance(self.likelihood, GaussianLikelihood):
rmse = ((l.loc - repeated_x) ** 2).mean().sqrt()
elif isinstance(self.likelihood, BernoulliLikelihood):
rmse = ((l.probs - repeated_x) ** 2).mean().sqrt()
# compute elbo and average over samples, sum over pixels/colours
if beta_nll:
if isinstance(self.likelihood, GaussianLikelihood):
var = l.scale ** 2
elif isinstance(self.likelihood, BernoulliLikelihood):
var = l.probs * (1 - l.probs) # Bernoulli variance = p(1-p)
elbo = (exp_ll * (var.detach() ** beta)
).mean(1).sum(dim=(1, 2, 3)) - kl
else:
elbo = exp_ll.mean(1).sum(dim=(1, 2, 3)) - kl
# elbo = - rmse - kl
exp_ll = exp_ll.mean(1).sum(dim=(1, 2, 3))
# report batch-averaged metrics
elbo_metric_name = 'elbo'
if beta_nll:
elbo_metric_name = 'beta_nll_elbo'
metrics[elbo_metric_name] = elbo.mean()
metrics["ll"] = exp_ll.mean()
metrics["kl"] = kl.mean()
if isinstance(self.likelihood, GaussianLikelihood):
if self.likelihood.noise == "homoscedastic":
metrics["noise"] = self.likelihood.std
metrics["RMSE"] = rmse
return elbo, metrics
def generate(self, z: torch.Tensor) -> torch.distributions.Distribution:
for _ in range(3 - len(z.shape)):
z = z.unsqueeze(0)
return self.decoder(z)
class Encoder(nn.Module):
def __init__(
self,
architecture: str = "cnn", # 'mlp' or 'cnn'
image_dims: Tuple[int] = (28, 28),
colour_channels: int = 1,
mlp_hidden_dims: Optional[List[int]] = [50, 50],
cnn_chans: Optional[List[int]] = [32, 64],
kernel_size: Optional[int] = 4,
latent_dim: int = 16,
posterior: nn.Module = DiagonalGaussian(),
nonlinearity: nn.Module = nn.ReLU(),
):
super().__init__()
if architecture not in ["mlp", "cnn"]:
raise NotImplementedError(
f"Only 'cnn' and 'mlp' are supported, not {architecture}."
)
if architecture == "mlp":
assert mlp_hidden_dims is not None
if architecture == "cnn":
assert cnn_chans is not None and kernel_size is not None
self.image_dims = image_dims
self.colour_channels = colour_channels
self.latent_dim = latent_dim
self.nonlinearity = nonlinearity
self.num_pixels = image_dims[0] * image_dims[1]
self.posterior = posterior
self.architecture = architecture
if architecture == "mlp":
self.enc_dims = (
[self.num_pixels * self.colour_channels]
+ mlp_hidden_dims
+ [
self.latent_dim * self.posterior.multiplier
]
)
self.network = MLP(dims=self.enc_dims, nonlinearity=self.nonlinearity)
elif architecture == "cnn":
if cnn_chans[0] != colour_channels:
cnn_chans = [colour_channels] + cnn_chans
if cnn_chans[-1] == latent_dim:
cnn_chans[-1] = latent_dim * self.posterior.multiplier
if cnn_chans[-1] != latent_dim * self.posterior.multiplier:
cnn_chans.append(latent_dim * self.posterior.multiplier)
self.network = EncodingCNN(
channels=cnn_chans,
kernel_size=kernel_size,
nonlinearity=self.nonlinearity,
)
def forward(self, x: torch.Tensor) -> torch.distributions.Distribution:
batch_size = x.shape[0]
if self.architecture == "mlp":
x = x.view(batch_size, self.num_pixels * self.colour_channels)
elif self.architecture == "cnn":
x = x.permute(
0, 3, 1, 2
) # move colour channels to conv2d channels dimension
x = self.network(x)
assert len(x.shape) == 2
assert x.shape[0] == batch_size
assert x.shape[1] == self.latent_dim * self.posterior.multiplier
q = self.posterior(x)
return q
class Decoder(nn.Module):
def __init__(
self,
architecture: str = "cnn", # 'mlp' or 'cnn'
image_dims: Tuple[int] = (28, 28),
colour_channels: int = 1,
mlp_hidden_dims: Optional[List[int]] = [50, 50],
cnn_chans: Optional[List[int]] = [32, 64],
kernel_size: int = 4,
latent_dim: int = 16,
likelihood: nn.Module = GaussianLikelihood(),
nonlinearity: nn.Module = nn.ReLU(),
):
super().__init__()
if architecture not in ["mlp", "cnn"]:
raise NotImplementedError(
f"Only 'cnn' and 'mlp' are supported, not {architecture}."
)
if architecture == "mlp":
assert mlp_hidden_dims is not None
if architecture == "cnn":
assert cnn_chans is not None and kernel_size is not None
self.image_dims = image_dims
self.colour_channels = colour_channels
self.latent_dim = latent_dim
self.nonlinearity = nonlinearity
self.num_pixels = image_dims[0] * image_dims[1]
self.likelihood = likelihood
self.architecture = architecture
if architecture == "mlp":
self.enc_dims = (
[self.latent_dim]
+ mlp_hidden_dims
+ [self.num_pixels * self.colour_channels * likelihood.multiplier]
)
self.network = MLP(dims=self.enc_dims, nonlinearity=self.nonlinearity)
elif architecture == "cnn":
if cnn_chans[0] != latent_dim:
cnn_chans = [latent_dim] + cnn_chans
if cnn_chans[-1] == colour_channels:
cnn_chans[-1] = colour_channels * likelihood.multiplier
if cnn_chans[-1] != colour_channels * likelihood.multiplier:
cnn_chans.append(colour_channels * likelihood.multiplier)
self.network = DecodingCNN(
image_dims=image_dims,
channels=cnn_chans,
kernel_size=kernel_size,
nonlinearity=self.nonlinearity,
)
def forward(self, x: torch.Tensor) -> torch.distributions.Distribution:
batch_size = x.shape[0]
num_samples = x.shape[1]
assert x.shape[2] == self.latent_dim
assert len(x.shape) == 3
x = x.view(batch_size * num_samples, self.latent_dim)
if self.architecture == "cnn":
x = x.unsqueeze(-1).unsqueeze(-1)
x = self.network(x)
if self.architecture == "cnn":
x = x.permute(0, 2, 3, 1) # move colour channels dim to colour dimension
x = x.view(
batch_size,
num_samples,
self.image_dims[0],
self.image_dims[1],
self.colour_channels * self.likelihood.multiplier,
)
l = self.likelihood(x)
return l