-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder_Decoder.py
202 lines (149 loc) · 6.25 KB
/
Encoder_Decoder.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
from torchvision.utils import save_image
import matplotlib.pyplot as plt
import numpy as np
class Encoder(nn.Module):
def __init__(self, layer_sizes, latent_dim, num_labels, conditional=False):
super(Encoder, self).__init__()
"""
Arguments:
layer_sizes (list[int]): list of sizes of layers of the encoder,
latent_dim (int): dimension of latent space, i.e. dimension out output of the encoder,
num_labels (int): amount of labels,
conditional (bool): True if CVAE and False if VAE
"""
self.conditional = conditional
if self.conditional:
layer_sizes[0] += num_labels
self.MLP = nn.Sequential()
for i, (in_size, out_size) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
self.MLP.add_module(
name=f"L{i}", module=nn.Linear(in_size, out_size))
self.MLP.add_module(name=f"A{i}", module=nn.ReLU())
self.linear_means = nn.Linear(layer_sizes[-1], latent_dim)
self.linear_log_var = nn.Linear(layer_sizes[-1], latent_dim)
def forward(self, x, c=None):
"""
Arguments:
x: tensor of dimension (batch_size, 1, 28, 28) or (batch_size, 28*28)
c: None or tensor of dimension (batch_size, 1)
Output:
means: tensor of dimension (batch_size, latent_dim),
log_var: tensor of dimension (batch_size, latent_dim)
"""
if self.conditional:
c = idx2onehot(c, n=10)
x = torch.cat((x, c), dim=-1)
x = self.MLP(x)
means = self.linear_means(x)
log_vars = self.linear_log_var(x)
return means, log_vars
class Decoder(nn.Module):
def __init__(self, layer_sizes, latent_dim, num_labels, conditional=False):
super(Decoder, self).__init__()
"""
Arguments:
layer_sizes (list[int]): list of sizes of layers of the decoder,
latent_dim (int): dimension of latent space, i.e. dimension out input of the decoder,
num_labels (int): amount of labels,
conditional (bool): True if CVAE and False if VAE
Output:
x: Parameters of gaussian distribution; only mu (see above)
"""
self.MLP = nn.Sequential()
self.conditional = conditional
if self.conditional:
input_size = latent_dim + num_labels
else:
input_size = latent_dim
for i, (in_size, out_size) in enumerate(zip([input_size]+layer_sizes[:-1], layer_sizes)):
self.MLP.add_module(
name=f"L{i}", module=nn.Linear(in_size, out_size))
if i+1 < len(layer_sizes):
self.MLP.add_module(name=f"A{i}", module=nn.ReLU())
else:
self.MLP.add_module(name="sigmoid", module=nn.Sigmoid())
def forward(self, z, c=None):
"""
Argumetns:
z: tensor of dimension (batch_size, latent_dim)
c: None or tensor of dimension (batch_size, 1)
Outputs:
x: mu of gaussian distribution (reconstructed image from latent code z)
"""
if self.conditional:
c = idx2onehot(c, n=10)
z = torch.cat((z, c), dim=-1)
x = self.MLP(z)
return x
class CVAE(nn.Module):
def __init__(self, inp_dim, encoder_layer_sizes, decoder_layer_sizes,
latent_dim, num_labels=10, conditional=False):
"""
Arguments:
inp_dim (int): dimension of input,
encoder_layer_sizes (list[int]): list of the sizes of the encoder layers,
decoder_layer_sizes (list[int]): list of the sizes of the decoder layers,
latent_dim (int): dimension of latent space/bottleneck,
num_labels (int): amount of labels (important for conditional VAE),,
conditional (bool): True if CVAE, else False
"""
super(CVAE, self).__init__()
self.latent_dim = latent_dim
self.num_labels = num_labels
self.encoder = Encoder(encoder_layer_sizes, latent_dim, num_labels, conditional)
self.decoder = Decoder(decoder_layer_sizes, latent_dim, num_labels, conditional)
def forward(self, x, c=None):
cuda = torch.device('cuda')
"""
Forward Process of whole VAE/CVAE.
Arguments:
x: tensor of dimension (batch_size, 1, 28, 28) or (batch_size, 28*28)
c: None or tensor of dimension (batch_size, 1)
Output: recon_x, means, log_var
recon_x: see explanation on second part of estimator above,
means: output of encoder,
log_var: output of encoder (logarithm of variance)
"""
batch_size = x.size(0)
x = x.view(-1,62*82)
means, log_var = self.encoder(x, c)
std = torch.exp(0.5 * log_var)
eps = torch.randn([batch_size, self.latent_dim])
eps = eps.cuda()
z = eps * std + means
z = z.cuda()
recon_x = self.decoder(z, c)
return recon_x, means, log_var
def sampling(self, n=2, c=None):
"""
Arguments:
n (int): amount of samples (amount of elements in the latent space)
c (bool): condition
Output:
x_sampled: n randomly sampled elements of the output distribution
"""
batch_size = n
z = torch.randn([batch_size, self.latent_dim])
z = t.to(device)
x_sampled = self.decoder(z, c)
return x_sampled
# Implement the Loss function for the VAE/CVAE
def loss_function(recon_x, x, mu, log_var):
"""
Arguments:
recon_x: reconstruced input
x: input,
mu, log_var: parameters of posterior (distribution of z given x)
"""
'''reconstruction loss, the difference between input and output for every pixel'''
BCE = F.binary_cross_entropy(
recon_x.view(-1, 62*82), x.view(-1, 62*82), reduction='sum')
'''the loss to force mu and var in the direction of a normal distribution'''
KLD = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
return (BCE + KLD) / x.size(0)