-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvae.py
239 lines (208 loc) · 8.16 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
from typing import Any, Dict, Tuple
import torch
import torch.nn as nn
class Autoencoder(nn.Module):
"""Vanilla autoencoder implemented with MLPs."""
def __init__(
self,
num_features: int = 28 * 28,
num_hidden: int = 28 * 28,
num_latent: int = 100
):
super(Autoencoder, self).__init__()
self.num_features = num_features
self.num_hidden = num_hidden
self.num_latent = num_latent
self.encoder = nn.Sequential(
nn.Linear(num_features, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_latent),
nn.ReLU()
)
self.decoder = nn.Sequential(
nn.Linear(num_latent, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_features),
nn.Sigmoid()
)
def encode(self, x: torch.Tensor) -> torch.Tensor:
"""Compresses x to latent representation, i.e. z = enc(x)."""
z = self.encoder(x)
return z
def decode(self, z: torch.Tensor) -> torch.Tensor:
"""Reconstructs x from latent representation. i.e. x_hat = dec(z)"""
x_hat = self.decoder(z)
return x_hat
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Generates x_hat from x, i.e. x_hat = dec(enc(x))."""
z = self.encode(x.view(x.size()[0], -1)) # flatten image tensor
x_hat = self.decode(z).view(x.size()) # reshape again as image
return x_hat
def training_step(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
"""Computes the reconstruction loss."""
x_hat = self.forward(x)
loss = nn.functional.mse_loss(x_hat, x, reduction="sum")
return {"loss": loss}
class VAE(nn.Module):
"""Simple variational autoencoder (VAE) implemented with MLPs."""
def __init__(
self,
num_features: int = 28 * 28,
num_hidden: int = 28 * 28,
num_latent: int = 100,
kl_weight: float = 1.0
):
super(VAE, self).__init__()
self.num_features = num_features
self.num_hidden = num_hidden
self.num_latent = num_latent
self.kl_weight = kl_weight
self.encoder = nn.Sequential(
nn.Linear(num_features, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU()
)
self.decoder = nn.Sequential(
nn.Linear(num_latent, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_features),
nn.Sigmoid()
)
self.mu = nn.Linear(num_hidden, num_latent)
self.log_var = nn.Linear(num_hidden, num_latent)
self.eps = torch.distributions.Normal(0, 1)
# Speed up sampling by utilizing GPU.
if torch.cuda.is_available():
self.eps.loc = self.eps.loc.cuda()
self.eps.scale = self.eps.scale.cuda()
def encode(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""Maps input x to latent distribution p_theta(z|x)."""
h = self.encoder(x)
mu = self.mu(h)
log_var = self.log_var(h)
return mu, log_var
def reparameterize(
self, mu: torch.Tensor, log_var: torch.Tensor
) -> torch.Tensor:
"""Performs reparameterization trick: z = eps * std + mu."""
std = torch.exp(0.5 * log_var) # equivalent to sqrt(exp(log_var))
eps = self.eps.sample(mu.shape) # sample eps ~ N(0, 1)
z = eps * std + mu
return z
def decode(self, z: torch.Tensor) -> torch.Tensor:
"""Maps latent variable z to distribution q_phi(x|z)."""
x_hat = self.decoder(z)
return x_hat
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, ...]:
"""Generates x_hat from x, i.e. x_hat = dec(enc(x))."""
mu, log_var = self.encode(x.view(x.size()[0], -1)) # flatten again
z = self.reparameterize(mu, log_var)
x_hat = self.decode(z).view(x.size()) # reshape again as an image
return x_hat, mu, log_var
def training_step(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
"""Computes the loss as KL + reconstruction loss."""
x_hat, mu, log_var = self.forward(x)
recon_loss = nn.functional.mse_loss(x_hat, x, reduction="sum")
kl_loss = kl_div_loss(mu, log_var)
loss = recon_loss + self.kl_weight * kl_loss
return {
"loss": loss,
"recon_loss": recon_loss,
"kl_loss": kl_loss
}
class ConditionalVAE(nn.Module):
"""Simple variational autoencoder using MLPs."""
def __init__(
self,
num_classes: int = 10,
num_features: int = 28 * 28,
num_hidden: int = 28 * 28,
num_latent: int = 100,
kl_weight: float = 1.0
):
super(ConditionalVAE, self).__init__()
self.num_classes = num_classes
# increase feature and hidden dimensionality by size of
# the label vector
num_features += num_classes
num_hidden += num_classes
self.num_features = num_features
self.num_hidden = num_hidden
self.num_latent = num_latent
self.kl_weight = kl_weight
self.encoder = nn.Sequential(
nn.Linear(num_features, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU()
)
self.decoder = nn.Sequential(
nn.Linear(num_latent + num_classes, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_hidden),
nn.ReLU(),
nn.Linear(num_hidden, num_features - num_classes),
nn.Sigmoid()
)
self.mu = nn.Linear(num_hidden, num_latent)
self.log_var = nn.Linear(num_hidden, num_latent)
self.eps = torch.distributions.Normal(0, 1)
# Speed up sampling by utilizing GPU.
if torch.cuda.is_available():
self.eps.loc = self.eps.loc.cuda()
self.eps.scale = self.eps.scale.cuda()
def encode(
self, x: torch.Tensor, y: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Maps input x, y to latent distribution p_theta(z|x, y)."""
x = torch.cat([x, y], dim=1) # combine x and y
h = self.encoder(x)
mu = self.mu(h)
log_var = self.log_var(h)
return mu, log_var
def reparameterize(
self, mu: torch.Tensor, log_var: torch.Tensor
) -> torch.Tensor:
"""Performs reparameterization trick: z = eps * std + mu."""
std = torch.exp(0.5 * log_var)
eps = self.eps.sample(mu.shape) # sample eps ~ N(0, 1)
z = eps * std + mu
return z
def decode(self, z: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
"""Maps latent variable z to distribution q_phi(x|z, y)."""
z = torch.cat([z, y], dim=1) # combine z and y
x_hat = self.decoder(z)
return x_hat
def forward(
self, x: torch.Tensor, y: torch.Tensor
) -> Tuple[torch.Tensor, ...]:
"""Generates x_hat from x and y, i.e. x_hat = dec(enc(x, y))."""
mu, log_var = self.encode(x.view(x.size()[0], -1), y) # flatten again
z = self.reparameterize(mu, log_var)
x_hat = self.decode(z, y).view(x.size()) # reshape again as an image
return x_hat, mu, log_var
def training_step(
self, x: torch.Tensor, y: torch.Tensor
) -> Dict[str, torch.Tensor]:
"""Computes the loss as KL + reconstruction loss."""
# vectorize label
y = nn.functional.one_hot(y, self.num_classes).to(x.device)
x_hat, mu, log_var = self.forward(x, y)
recon_loss = nn.functional.mse_loss(x_hat, x, reduction="sum")
kl_loss = kl_div_loss(mu, log_var)
loss = recon_loss + self.kl_weight * kl_loss
return {
"loss": loss,
"recon_loss": recon_loss,
"kl_loss": kl_loss
}
def kl_div_loss(mu: torch.Tensor, log_var: torch.Tensor) -> torch.Tensor:
"""Kullback-Leibler Divergence loss."""
return -0.5 * torch.sum(1 + log_var - (mu ** 2) - log_var.exp())