-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVAE_GAN.py
307 lines (270 loc) · 8.72 KB
/
VAE_GAN.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
VAE-GAN model
"""
from typing import Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
class Encoder(nn.Module):
def __init__(self, input_dim:int, latent_dim:int, hidden_dim:int)->Tuple[torch.Tensor, torch.Tensor]:
super(Encoder, self).__init__()
"""
Encoder network
Parameters
----------
input_dim : int
input dimension, e.g. 28*28=784 for MNIST dataset
latent_dim : int
dimension of the latent variable z
hidden_dim : int
dimension of the hidden layer
"""
self.input_dim = input_dim
self.latent_dim = latent_dim
self.hidden_dim = hidden_dim
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
self.fc31 = nn.Linear(hidden_dim, latent_dim)
self.fc32 = nn.Linear(hidden_dim, latent_dim)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x:torch.Tensor)->Tuple[torch.Tensor, torch.Tensor]:
"""
Forward pass
Parameters
----------
x : torch.Tensor
input data
Returns
-------
Tuple[torch.Tensor, torch.Tensor]
mean and log variance of the latent variable z
"""
h = self.relu(self.fc1(x))
h = self.relu(self.fc2(h))
mu = self.fc31(h)
logvar = self.fc32(h)
return mu, logvar
class Decoder(nn.Module):
def __init__(self, latent_dim:int, hidden_dim:int, output_dim:int)->torch.Tensor:
super(Decoder, self).__init__()
"""
Decoder network
Parameters
----------
latent_dim : int
dimension of the latent variable z
hidden_dim : int
dimension of the hidden layer
output_dim : int
output dimension, e.g. 28*28=784 for MNIST dataset
Returns
-------
torch.Tensor
reconstructed data x_hat
"""
self.latent_dim = latent_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.fc = nn.Sequential(
nn.Linear(latent_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim),
nn.Tanh()
)
def forward(self, z:torch.Tensor)->torch.Tensor:
"""
Forward pass
Parameters
----------
z : torch.Tensor
latent variable z
Returns
-------
torch.Tensor
reconstructed data x_hat
"""
return self.fc(z)
class Discriminator(nn.Module):
def __init__(self, input_dim:int, hidden_dim:int)->torch.Tensor:
super(Discriminator, self).__init__()
"""
Discriminator network
Parameters
----------
input_dim : int
input dimension, e.g. 28*28=784 for MNIST dataset
hidden_dim : int
dimension of the hidden layer
Returns
-------
torch.Tensor
probability of the input data being real
"""
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.fc = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1),
nn.Sigmoid()
)
def forward(self, x:torch.Tensor)->torch.Tensor:
"""
Forward pass
Parameters
----------
x : torch.Tensor
input data
Returns
-------
torch.Tensor
probability of the input data being real
"""
return self.fc(x)
class VAE_GAN(nn.Module):
def __init__(self, input_dim:int, latent_dim:int, hidden_dim:int, output_dim:int)->Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
super(VAE_GAN, self).__init__()
"""
VAE-GAN model
Parameters
----------
input_dim : int
input dimension, e.g. 28*28=784 for MNIST dataset
latent_dim : int
dimension of the latent variable z
hidden_dim : int
dimension of the hidden layer
output_dim : int
output dimension, e.g. 28*28=784 for MNIST dataset
Returns
-------
Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]
reconstructed data x_hat, probability of the input data being real, mean and log variance of the latent variable z
"""
self.input_dim = input_dim
self.latent_dim = latent_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.encoder = Encoder(input_dim, latent_dim, hidden_dim)
self.decoder = Decoder(latent_dim, hidden_dim, output_dim)
self.discriminator = Discriminator(input_dim, hidden_dim)
def forward(self, x:torch.Tensor)->Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Forward pass
Parameters
----------
x : torch.Tensor
input data
Returns
-------
Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]
reconstructed data x_hat, probability of the input data being real, mean and log variance of the latent variable z
"""
mu, logvar = self.encoder(x)
z = self.reparameterize(mu, logvar)
x_hat = self.decoder(z)
y = self.discriminator(x_hat)
return x_hat, y, mu, logvar
def reparameterize(self, mu:torch.Tensor, logvar:torch.Tensor)->torch.Tensor:
"""
Reparameterization trick
Parameters
----------
mu : torch.Tensor
mean of the latent variable z
logvar : torch.Tensor
log variance of the latent variable z
Returns
-------
torch.Tensor
latent variable z
"""
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
z = mu + eps * std
return z
def loss_function(self, x:torch.Tensor, x_hat:torch.Tensor, y:torch.Tensor, mu:torch.Tensor, logvar:torch.Tensor):
"""
Loss function
Parameters
----------
x : torch.Tensor
input data
x_hat : torch.Tensor
reconstructed data x_hat
y : torch.Tensor
probability of the input data being real
mu : torch.Tensor
mean of the latent variable z
logvar : torch.Tensor
log variance of the latent variable z
Returns
-------
total_loss : torch.Tensor
total loss
recon_loss : torch.Tensor
reconstruction loss
kl_div : torch.Tensor
KL divergence
disc_loss : torch.Tensor
discriminator loss
gen_loss : torch.Tensor
generator loss
"""
# Reconstruction loss
mse_loss = nn.MSELoss(reduction='sum')
bce_loss = nn.BCELoss(reduction='sum')
recon_loss = mse_loss(x_hat, x)
# KL divergence
kl_div = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
# Discriminator loss
disc_loss = bce_loss(y, torch.ones_like(y))
# Generator loss
gen_loss = bce_loss(y, torch.zeros_like(y))
# Total loss
total_loss = recon_loss + kl_div + disc_loss + gen_loss
return total_loss#, recon_loss, kl_div, disc_loss, gen_loss
def sample(self, num_samples:int)->torch.Tensor:
"""
Sample from the latent space
Parameters
----------
num_samples : int
number of samples
Returns
-------
torch.Tensor
samples from the latent space
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
z = torch.randn(num_samples, self.latent_dim).to(device)
x_hat = self.decoder(z)
return x_hat
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# parameters
input_dim = 784
latent_dim = 100
hidden_dim = 400
output_dim = 784
batch_size = 128
num_samples = 16
# test vae-gan
x = torch.randn(batch_size, input_dim).to(device)
model = VAE_GAN(input_dim, latent_dim, hidden_dim, output_dim).to(device)
x_hat, y, mu, logvar = model(x)
print(x_hat.shape)
print(y.shape)
print(mu.shape)
print(logvar.shape)
# test loss function
total_loss = model.loss_function(x, x_hat, y, mu, logvar)
print(total_loss)
# test sample
x_hat = model.sample(num_samples)
print(x_hat.shape)