-
Notifications
You must be signed in to change notification settings - Fork 0
/
vae.py
152 lines (114 loc) · 4.07 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
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
# -*- coding: utf-8 -*-
"""
# Author : Ming
# File : {NAME}.py
# Time : 2019/4/19 0019 下午 5:56
"""
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import MNIST
from matplotlib import pyplot as plt
def display_images(in_, out, n=1, label=None, count=False):
for N in range(n):
plt.cla()
if in_ is not None:
in_pic = in_.data.cpu().view(-1, 28, 28)
plt.figure(figsize=(18, 4))
plt.suptitle(label ' – real test data / reconstructions', color='w', fontsize=16)
for i in range(4):
plt.subplot(1,4,i 1)
plt.imshow(in_pic[i 4*N])
plt.axis('off')
out_pic = out.data.cpu().view(-1, 28, 28)
plt.figure(figsize=(18, 6))
for i in range(4):
plt.subplot(1,4,i 1)
plt.imshow(out_pic[i 4*N])
plt.axis('off')
if count: plt.title(str(4 * N i), color='w')
plt.pause(0.33)
torch.manual_seed(1)
torch.cuda.manual_seed(1)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
batch_size = 256
kwargs = {'num_workers': 1, 'pin_memory': True}
train_loader = torch.utils.data.DataLoader(
MNIST('./data', train=True, download=True,
transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
MNIST('./data', train=False, transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True, **kwargs)
d = 20
class VAE(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(784, d ** 2),
nn.ReLU(),
nn.Linear(d ** 2, d * 2)
)
self.decoder = nn.Sequential(
nn.Linear(d, d ** 2),
nn.ReLU(),
nn.Linear(d ** 2, 784),
nn.Sigmoid(),
)
def reparameterise(self, mu, logvar):
if self.training:
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x):
mu_logvar = self.encoder(x.view(-1, 784)).view(-1, 2, d)
mu = mu_logvar[:, 0, :]
logvar = mu_logvar[:, 1, :]
z = self.reparameterise(mu, logvar)
return self.decoder(z), mu, logvar
model = VAE().to(device)
learning_rate = 1e-3
optimizer = torch.optim.Adam(
model.parameters(),
lr=learning_rate,
)
def loss_function(x_hat, x, mu, logvar):
BCE = nn.functional.binary_cross_entropy(
x_hat, x.view(-1, 784), reduction='sum'
)
KLD = -0.5 * torch.sum(1 logvar - mu.pow(2) - logvar.exp())
return BCE KLD
epochs = 10
for epoch in range(1, epochs 1):
# Training
model.train()
train_loss = 0
for x, _ in train_loader:
x = x.to(device)
# ===================forward=====================
x_hat, mu, logvar = model(x)
loss = loss_function(x_hat, x, mu, logvar)
train_loss = loss.item()
# ===================backward====================
optimizer.zero_grad()
loss.backward()
optimizer.step()
# ===================log========================
print('====> Epoch: {} Average loss: {}'.format(epoch, train_loss / len(train_loader.dataset)))
# Testing
with torch.no_grad():
model.eval()
test_loss = 0
for x, _ in test_loader:
x = x.to(device)
# ===================forward=====================
x_hat, mu, logvar = model(x)
test_loss = loss_function(x_hat, x, mu, logvar).item()
# ===================log========================
test_loss /= len(test_loader.dataset)
print('====> Test set loss: {}'.format(test_loss))
display_images(x, x_hat, 1, f'Epoch {epoch}')