-
Notifications
You must be signed in to change notification settings - Fork 1
/
vae.py
103 lines (87 loc) · 3.26 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
import torch
from torch import nn
from math import log
from torch.nn import functional as F
from tasks.utils import discretized_mix_logistic_loss
class VAE(nn.Module):
def __init__(self,
args,
prior,
dist,
encoder,
encoder_layer,
decoder,
decoder_layer,
loss_type
):
super().__init__()
self.args = args
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., iwae=0):
mean, covar = self.encoder_layer(self.encoder(x))
if self.args.dist != "PGMNormal":
variational = self.dist(mean, covar)
else:
variational = self.dist(mean, covar, self.args.c)
z = variational.rsample(n_samples)
x_generated = self.generate(z)
if self.loss_type == 'BCE':
recon_loss = F.binary_cross_entropy_with_logits(
x_generated,
x.unsqueeze(0).expand(x_generated.size()),
reduction='none'
)
elif self.loss_type == 'NLL':
xmean, xlogvar = x_generated[..., 0], x_generated[..., 1]
recon_loss = F.gaussian_nll_loss(
xmean,
x.unsqueeze(0).expand(xmean.size()),
xlogvar,
full=True,
reduction='none'
)
elif self.loss_type == 'MSE':
recon_loss = nn.MSELoss(reduction='none')(
x_generated,
x.unsqueeze(0).expand(x_generated.size())
)
else:
recon_loss = discretized_mix_logistic_loss(x, x_generated)
while len(recon_loss.size()) > 2:
recon_loss = recon_loss.sum(-1)
if iwae == 0 or n_samples == 1:
if variational.kl_div is None:
kl_loss = variational.log_prob(z) - self.prior.log_prob(z)
kl_loss = kl_loss.mean(dim=0)
else:
kl_loss = variational.kl_div(self.prior)
kl_loss = kl_loss.sum(dim=-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_)
else:
kl_loss = variational.log_prob(z) - self.prior.log_prob(z)
kl_loss = kl_loss.sum(dim=-1)
total_loss_sum = -recon_loss - beta * kl_loss
loss = total_loss_sum.logsumexp(dim=0) # total_loss_sum.exp().sum(dim=0).log()
loss = loss - log(n_samples)
loss = -loss.mean()
total_elbo_sum = -recon_loss - kl_loss
elbo = total_elbo_sum.logsumexp(dim=0)
elbo = elbo - log(n_samples)
elbo = elbo.sum()
recon_loss = recon_loss.mean(dim=0).sum()
kl_loss = kl_loss.mean(dim=0)
kl_loss_ = kl_loss.sum()
return loss, elbo, z, mean, recon_loss, kl_loss_, kl_loss
def generate(self, z):
return self.decoder(self.decoder_layer(z))