-
Notifications
You must be signed in to change notification settings - Fork 3
/
vae.py
65 lines (53 loc) · 1.84 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
import torch
from torch import nn
from torch.nn import functional as F
class VAE(nn.Module):
def __init__(self,
prior,
dist,
encoder,
encoder_layer,
decoder,
decoder_layer,
loss_type
):
super().__init__()
self.prior = prior
self.dist = dist
self.encoder = encoder
self.encoder_layer = encoder_layer
self.decoder = decoder
self.decoder_layer = decoder_layer
self.loss_type = loss_type
def forward(self, x, n_samples=1, beta=1.):
mean, covar = self.encoder_layer(self.encoder(x))
variational = self.dist(mean, covar)
z = variational.rsample(n_samples)
log_prob_base = variational.log_prob(z)
log_prob_target = self.prior.log_prob(z)
kl_loss = (log_prob_base - log_prob_target).mean(dim=0)
x_generated = self.generate(z)
if self.loss_type == 'BCE':
recon_loss = F.binary_cross_entropy(
x_generated,
x.unsqueeze(0).expand(x_generated.size()),
reduction='none'
)
else:
recon_loss = F.gaussian_nll_loss(
x_generated,
x.unsqueeze(0).expand(x_generated.size()),
torch.ones(x_generated.size(), device=x.device) * 0.01,
reduction='none'
)
while len(recon_loss.size()) > 2:
recon_loss = recon_loss.sum(-1)
recon_loss = recon_loss.mean(dim=0)
total_loss_sum = recon_loss + beta * kl_loss
loss = total_loss_sum.mean()
recon_loss = recon_loss.sum()
kl_loss = kl_loss.sum()
elbo = -(recon_loss + kl_loss)
return loss, elbo, z, mean, recon_loss, kl_loss
def generate(self, z):
return self.decoder(self.decoder_layer(z))