-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_aae.py
179 lines (143 loc) · 5.22 KB
/
main_aae.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
from logger import Logger
# MNIST Dataset
dataset = dsets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
# Data Loader (Input Pipeline)
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=100,
shuffle=True)
def to_np(x):
return x.data.cpu().numpy()
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
#Encoder
class Q_net(nn.Module):
def __init__(self,X_dim,N,z_dim):
super(Q_net, self).__init__()
self.lin1 = nn.Linear(X_dim, N)
self.lin2 = nn.Linear(N, N)
self.lin3gauss = nn.Linear(N, z_dim)
def forward(self, x):
x = F.dropout(self.lin1(x), p=0.25, training=self.training)
x = F.relu(x)
x = F.dropout(self.lin2(x), p=0.25, training=self.training)
x = F.relu(x)
xgauss = self.lin3gauss(x)
return xgauss
# Decoder
class P_net(nn.Module):
def __init__(self,X_dim,N,z_dim):
super(P_net, self).__init__()
self.lin1 = nn.Linear(z_dim, N)
self.lin2 = nn.Linear(N, N)
self.lin3 = nn.Linear(N, X_dim)
def forward(self, x):
x = F.dropout(self.lin1(x), p=0.25, training=self.training)
x = F.relu(x)
x = F.dropout(self.lin2(x), p=0.25, training=self.training)
x = self.lin3(x)
return F.sigmoid(x)
# Discriminator
class D_net_gauss(nn.Module):
def __init__(self,N,z_dim):
super(D_net_gauss, self).__init__()
self.lin1 = nn.Linear(z_dim, N)
self.lin2 = nn.Linear(N, N)
self.lin3 = nn.Linear(N, 1)
def forward(self, x):
x = F.dropout(self.lin1(x), p=0.2, training=self.training)
x = F.relu(x)
x = F.dropout(self.lin2(x), p=0.2, training=self.training)
x = F.relu(x)
return F.sigmoid(self.lin3(x))
EPS = 1e-15
z_red_dims = 120
Q = Q_net(784,1000,z_red_dims).cuda()
P = P_net(784,1000,z_red_dims).cuda()
D_gauss = D_net_gauss(500,z_red_dims).cuda()
# Set the logger
logger = Logger('./logs/z_120_fixed_LR_2')
# Set learning rates
gen_lr = 0.0001
reg_lr = 0.00005
#encode/decode optimizers
optim_P = torch.optim.Adam(P.parameters(), lr=gen_lr)
optim_Q_enc = torch.optim.Adam(Q.parameters(), lr=gen_lr)
#regularizing optimizers
optim_Q_gen = torch.optim.Adam(Q.parameters(), lr=reg_lr)
optim_D = torch.optim.Adam(D_gauss.parameters(), lr=reg_lr)
data_iter = iter(data_loader)
iter_per_epoch = len(data_loader)
total_step = 50000
# Start training
for step in range(total_step):
# Reset the data_iter
if (step+1) % iter_per_epoch == 0:
data_iter = iter(data_loader)
# Fetch the images and labels and convert them to variables
images, labels = next(data_iter)
images, labels = to_var(images.view(images.size(0), -1)), to_var(labels)
#reconstruction loss
P.zero_grad()
Q.zero_grad()
D_gauss.zero_grad()
z_sample = Q(images) #encode to z
X_sample = P(z_sample) #decode to X reconstruction
recon_loss = F.binary_cross_entropy(X_sample+EPS,images+EPS)
recon_loss.backward()
optim_P.step()
optim_Q_enc.step()
# Discriminator
## true prior is random normal (randn)
## this is constraining the Z-projection to be normal!
Q.eval()
z_real_gauss = Variable(torch.randn(images.size()[0], z_red_dims) * 5.).cuda()
D_real_gauss = D_gauss(z_real_gauss)
z_fake_gauss = Q(images)
D_fake_gauss = D_gauss(z_fake_gauss)
D_loss = -torch.mean(torch.log(D_real_gauss + EPS) + torch.log(1 - D_fake_gauss + EPS))
D_loss.backward()
optim_D.step()
# Generator
Q.train()
z_fake_gauss = Q(images)
D_fake_gauss = D_gauss(z_fake_gauss)
G_loss = -torch.mean(torch.log(D_fake_gauss + EPS))
G_loss.backward()
optim_Q_gen.step()
if (step+1) % 100 == 0:
# print ('Step [%d/%d], Loss: %.4f, Acc: %.2f'
# %(step+1, total_step, loss.data[0], accuracy.data[0]))
#============ TensorBoard logging ============#
# (1) Log the scalar values
info = {
'recon_loss': recon_loss.data[0],
'discriminator_loss': D_loss.data[0],
'generator_loss': G_loss.data[0]
}
for tag, value in info.items():
logger.scalar_summary(tag, value, step+1)
# (2) Log values and gradients of the parameters (histogram)
for net,name in zip([P,Q,D_gauss],['P_','Q_','D_']):
for tag, value in net.named_parameters():
tag = name+tag.replace('.', '/')
logger.histo_summary(tag, to_np(value), step+1)
logger.histo_summary(tag+'/grad', to_np(value.grad), step+1)
# (3) Log the images
info = {
'images': to_np(images.view(-1, 28, 28)[:10])
}
for tag, images in info.items():
logger.image_summary(tag, images, step+1)
#save the Encoder
torch.save(Q.state_dict(),'Q_encoder_weights.pt')