forked from dragen1860/pytorch-mnist-vae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvae2.py
108 lines (74 loc) · 2.62 KB
/
vae2.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
import torch
from torch import nn
from torch.nn import functional as F
class Encoder(torch.nn.Module):
def __init__(self, D_in, H, D_out, keep_prob=0):
super(Encoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, 32)
self._enc_mu = torch.nn.Linear(32, D_out)
self._enc_log_sigma = torch.nn.Linear(32, D_out)
def forward(self, x):
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
# print(x.shape)
return self._enc_mu(x), self._enc_log_sigma(x)
class Decoder(torch.nn.Module):
def __init__(self, D_in, H, D_out, keep_prob=0):
super(Decoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, D_out)
def forward(self, x):
x = F.relu(self.linear1(x))
return F.relu(self.linear2(x))
def get_ae(encoder, decoder, x):
# encoding
mu, log_sigma = encoder(x)
sigma = torch.exp(log_sigma)
# sampling by re-parameterization technique
z = mu + sigma * torch.randn_like(mu)
# decoding
y = decoder(z)
# y = torch.clamp(y, 1e-8, 1 - 1e-8)
return y
def get_z(encoder, x):
# encoding
mu, log_sigma = encoder(x)
sigma = torch.exp(log_sigma)
# sampling by re-parameterization technique
z = mu + sigma * torch.randn_like(mu)
return z
def get_loss(encoder, decoder, x, x_target):
"""
:param encoder:
:param decoder:
:param x: input
:param x_hat: target
:param dim_img:
:param dim_z:
:param n_hidden:
:param keep_prob:
:return:
"""
batchsz = x.size(0)
# encoding
mu, log_sigma = encoder(x)
sigma = torch.exp(log_sigma)
# sampling by re-parameterization technique
z = mu + sigma * torch.randn_like(mu)
# decoding
y = decoder(z)
# y = torch.clamp(y, 1e-8, 1 - 1e-8)
# loss
# marginal_likelihood2 = torch.sum(x_target * torch.log(y) + (1 - x_target) * torch.log(1 - y)) / batchsz
# marginal_likelihood = -F.binary_cross_entropy(y, x_target, reduction='sum') / batchsz
marginal_likelihood = -torch.pow(x_target - y, 2).sum() / batchsz
# print(marginal_likelihood2.item(), marginal_likelihood.item())
KL_divergence = 0.5 * torch.sum(
torch.pow(mu, 2) +
torch.pow(sigma, 2) -
torch.log(1e-8 + torch.pow(sigma, 2)) - 1
).sum() / batchsz
ELBO = marginal_likelihood - KL_divergence
loss = -ELBO
return y, z, loss, marginal_likelihood, KL_divergence