-
Notifications
You must be signed in to change notification settings - Fork 0
/
VAE.py
83 lines (67 loc) · 2.43 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
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, sampler
from torchvision import datasets, transforms
# Necessary Hyperparameters
num_epochs = 40
learning_rate = 0.0001
batch_size = 64
latent_dim = 10
transform = transforms.Compose([
transforms.ToTensor(),
])
def denorm(x):
return x
class VAE(nn.Module):
def __init__(self, latent_dim):
super(VAE, self).__init__()
self.latent_dim = latent_dim
self.z_dim = 28//2**2
# fully connected layers for learning representations
self.fc_mu = nn.Linear(self.z_dim**2 * 32, latent_dim)
self.fc_log_var = nn.Linear(self.z_dim**2 *32, latent_dim)
self.fc2 = nn.Linear(latent_dim, self.z_dim**2 * 32)
self.encoder = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=4, stride=2, padding = 1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=4, stride=2, padding = 1),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(32, 64, kernel_size=3, stride=2, padding =1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=1, padding = 1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.ConvTranspose2d(32, 1, kernel_size=4, stride=2, padding = 1),
nn.Sigmoid()
)
def encode(self, x):
x = self.encoder(x)
x = x.view(x.size(0), -1)
mu = self.fc_mu(x)
log_var = self.fc_log_var(x)
return mu, log_var
def reparametrize(self, mu, logvar): #REPARAMETRIZATION TRICK: normal distributions are just scaled and translated
#from normal distributions
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std) #random noise
return mu + std*eps
def decode(self, z):
z = self.fc2(z)
dec = z.view(z.size(0), 32, self.z_dim, self.z_dim)
dec = self.decoder(dec)
return dec
def forward(self, x):
mean, logvar = self.encode(x)
z = self.reparametrize(mean, logvar)
output = self.decode(z)
return output, mean, logvar
model = VAE(latent_dim)
params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Total number of parameters is: {}".format(params))
print(model)
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)