-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVAE_Linear.py
More file actions
223 lines (172 loc) · 7.13 KB
/
VAE_Linear.py
File metadata and controls
223 lines (172 loc) · 7.13 KB
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
# Variational Autoencoder with fully connnected layers,
# Please email me if you have any comments or questions: alotfi@utexas.edu
import torch
from torch.autograd import Variable
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import torch.optim as optim
import math
import numpy as np
import os
import torch.nn.functional as F
import torch.nn.init as init
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import pickle
# Calling Seaborn causes pytorch warnings to be repeated in each loop, so I turned off these redudant warnings, but make sure
# you do not miss something important.
warnings.filterwarnings('ignore')
class VAE1(nn.Module):
def __init__(self, z_dim=2):
super(VAE1, self).__init__()
self.z_dim = z_dim
self.encode = nn.Sequential(
nn.Linear(784, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 2000),
nn.LeakyReLU(0.2, True),
nn.Linear(2000, 2000),
nn.LeakyReLU(0.2, True),
nn.Linear(2000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 2 * z_dim),
)
self.decode = nn.Sequential(
nn.Linear(z_dim, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 2000),
nn.LeakyReLU(0.2, True),
nn.Linear(2000, 2000),
nn.LeakyReLU(0.2, True),
nn.Linear(2000, 1000),
nn.LeakyReLU(0.2, True),
nn.Linear(1000, 784),
nn.Sigmoid(),
)
self.weight_init()
def weight_init(self, mode='normal'):
initializer = normal_init
for block in self._modules:
for m in self._modules[block]:
initializer(m)
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
def forward(self, x, no_enc=False):
if no_enc:
gen_z = Variable(torch.randn(100, z_dim), requires_grad=False)
gen_z = gen_z.to(device)
return self.decode(gen_z).view(x.size())
else:
stats = self.encode(x.view(-1, 784))
mu = stats[:, :self.z_dim]
logvar = stats[:, self.z_dim:]
z = self.reparametrize(mu, logvar)
x_recon = self.decode(z).view(x.size())
return x_recon, mu, logvar, z.squeeze()
def normal_init(m):
if isinstance(m, (nn.Linear, nn.Conv2d)):
init.normal(m.weight, 0, 0.02)
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.fill_(0)
def recon_loss(x_recon, x):
n = x.size(0)
loss = F.binary_cross_entropy(x_recon, x, size_average=False).div(n)
return loss
def kl_divergence(mu, logvar):
kld = -0.5 * (1 + logvar - mu ** 2 - logvar.exp()).sum(1).mean()
return kld
def convert_to_display(samples):
cnt, height, width = int(math.floor(math.sqrt(samples.shape[0]))), samples.shape[1], samples.shape[2]
samples = np.transpose(samples, axes=[1, 0, 2, 3])
samples = np.reshape(samples, [height, cnt, cnt, width])
samples = np.transpose(samples, axes=[1, 0, 2, 3])
samples = np.reshape(samples, [height*cnt, width*cnt])
return samples
use_cuda = torch.cuda.is_available()
device = 'cuda' if use_cuda else 'cpu'
print('The code is running over', device)
max_iter = int(1)
batch_size = 100
z_dim = 2
lr = 0.001
beta1 = 0.9
beta2 = 0.999
gamma = 1
training_set = datasets.MNIST('./tmp/MNIST', train=True, download=True, transform=transforms.ToTensor())
test_set = datasets.MNIST('./tmp/MNIST', train=False, download=True, transform=transforms.ToTensor())
data_loader = DataLoader(training_set, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_set, batch_size=500, shuffle=True, num_workers=3)
VAE = VAE1().to(device)
optim = optim.Adam(VAE.parameters(), lr=lr, betas=(beta1, beta2))
Result = []
for epoch in range(max_iter):
train_loss = 0
KL_loss = 0
Loglikelihood_loss= 0
for batch_idx, (x_true, _) in enumerate(data_loader):
x_true = x_true.to(device)
x_recon, mu, logvar, z = VAE(x_true)
vae_recon_loss = recon_loss(x_recon, x_true)
KL = kl_divergence(mu, logvar)
loss = vae_recon_loss + KL
train_loss += loss.item()
Loglikelihood_loss += vae_recon_loss.item()
KL_loss += KL.item()
optim.zero_grad()
loss.backward()
optim.step()
if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)] \t Loss: {:.6f} \t Cross Entropy: {:.6f} \t KL Loss: {:.6f}'.format(epoch, batch_idx * len(x_true),
len(data_loader.dataset),
100. * batch_idx / len(data_loader),
loss.item(),
vae_recon_loss,
KL))
print('====> Epoch: {}, \t Average loss: {:.4f}, \t Log Likeihood: {:.4f}, \t KL: {:.4f} '
.format(epoch, train_loss / (batch_idx + 1), Loglikelihood_loss/ (batch_idx + 1), KL_loss/ (batch_idx + 1)))
Result.append(('====>epoch:', epoch,
'loss:', train_loss / (batch_idx + 1),
'Loglikeihood:', Loglikelihood_loss / (batch_idx + 1),
'KL:', KL_loss / (batch_idx + 1),
))
with open("file.txt", "w") as output:
output.write(str(Result))
torch.save(VAE.state_dict(), './Saved_Networks/Plain_VAE_Linear')
print('The net\'s parameters are saved')
if z_dim == 2:
batch_size_test = 500
z_list, label_list = [], []
for i in range(20):
x_test, y_test= iter(test_loader).next()
x_test = Variable(x_test, requires_grad=False).to(device)
_, _, _, z = VAE(x_test)
z_list.append(z.cpu().data.numpy())
label_list.append(y_test.numpy())
z = np.concatenate(z_list, axis=0)
label = np.concatenate(label_list)
frame1 = sns.kdeplot(z[:, 0], z[:, 1], n_levels=300, cmap='hot')
frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)
plt.savefig('Latent_Distirbutions.eps', format='eps', dpi=1000)
plt.show()
frame2 = plt.scatter(z[:, 0], z[:, 1], c=label, cmap='jet', edgecolors='black')
frame2.axes.get_xaxis().set_visible(False)
frame2.axes.get_yaxis().set_visible(False)
plt.colorbar()
plt.savefig('Latent_Distirbutions_Labels.eps', format='eps', dpi=1000)
plt.show()
samples = VAE(x_test[:100], no_enc=True)
samples = samples.permute(0, 2, 3, 1).contiguous().cpu().data.numpy()
frame3 = plt.imshow(convert_to_display(samples), cmap='Greys_r')
frame3.axes.get_xaxis().set_visible(False)
frame3.axes.get_yaxis().set_visible(False)
plt.savefig('Recon_Images.eps', format='eps', dpi=1000)
plt.show()