-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
210 lines (150 loc) · 6.88 KB
/
main.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
import torch
from torch import nn, optim
from torch.autograd import Variable, grad
import GAN
import utils
import visdom
import numpy as np
import sys
import os
import pickle
#============ PARSE ARGUMENTS =============
args = utils.setup_args()
args.save_name = args.save_file + args.env
print(args)
#============ GRADIENT PENALTY (for discriminator) ================
def calc_gradient_penalty(netD, real_data, fake_data):
alpha = torch.rand(real_data.size(0), 1)
alpha = alpha.expand(real_data.size())
if torch.cuda.is_available():
alpha = alpha.cuda()
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
if torch.cuda.is_available():
interpolates = interpolates.cuda()
interpolates = Variable(interpolates, requires_grad=True)
disc_interpolates = netD(interpolates)
gradients = grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).cuda() if torch.cuda.is_available() else torch.ones(
disc_interpolates.size()),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * args.lambG
return gradient_penalty
def calc_gradient_penalty_rho(netD, real_data, fake_data):
alpha = torch.rand(real_data.size(0), 1)
alpha = alpha.expand(real_data.size())
if torch.cuda.is_available():
alpha = alpha.cuda()
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
if torch.cuda.is_available():
interpolates = interpolates.cuda()
interpolates = Variable(interpolates, requires_grad=True)
_, disc_interpolates = netD(interpolates)
gradients = grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).cuda() if torch.cuda.is_available() else torch.ones(
disc_interpolates.size()),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * args.lambG2
return gradient_penalty
#============= TRAINING INITIALIZATION ==============
# initialize discriminator
netD = GAN.Discriminator(args.nz, args.n_hidden)
print("Discriminator loaded")
# initialize generator
netG = GAN.Generator(args.nz, args.n_hidden)
print("Generator loaded")
if torch.cuda.is_available():
netD.cuda()
netG.cuda()
print("Using GPU")
# load data
loader = utils.setup_data_loaders(args.batch_size, args.source_data_file, args.target_data_file)
print('Data loaded')
sys.stdout.flush()
# setup optimizers
G_opt = optim.Adam(list(netG.parameters()), lr = args.lrG)
D_opt = optim.Adam(list(netD.parameters()), lr = args.lrD)
# loss criteria
logsigmoid = nn.LogSigmoid()
mse = nn.MSELoss(reduce=False)
LOG2 = Variable(torch.from_numpy(np.ones(1)*np.log(2)).float())
print(LOG2)
if torch.cuda.is_available():
LOG2 = LOG2.cuda()
#=========== LOGGING INITIALIZATION ================
vis = utils.init_visdom(args.env)
tracker = utils.Tracker()
tracker_plot=None
scale_plot=None
#============================================================
#============ MAIN TRAINING LOOP ============================
#============================================================
for epoch in range(args.max_iter):
for it, (s_inputs, t_inputs) in enumerate(loader):
s_inputs, t_inputs = Variable(s_inputs), Variable(t_inputs)
if torch.cuda.is_available():
s_inputs, t_inputs = s_inputs.cuda(), t_inputs.cuda()
#================== Train generator =========================
if it % args.critic_iter == args.critic_iter-1:
netG.train()
netD.eval()
netG.zero_grad()
# pass source inputs through generator network
s_generated, s_scale = netG(s_inputs)
# pass generated source data and target inputs through discriminator network
s_outputs = netD(s_generated)
# compute loss
G_loss = args.lamb0*torch.mean(s_scale*torch.sum(mse(s_generated, s_inputs), dim=1)) + args.lamb1*torch.mean((s_scale-1)*torch.log(s_scale))
G_loss += calc_gradient_penalty_rho(netG, s_inputs.data, s_inputs.data[torch.randperm(s_inputs.size(0))])
if args.psi2 == "EQ":
G_loss += - args.lamb2*torch.mean(s_scale*s_outputs)
else:
G_loss += args.lamb2*torch.mean(s_scale*(LOG2.expand_as(s_outputs)+logsigmoid(s_outputs)-s_outputs))
# update params
G_loss.backward()
G_opt.step()
#================== Train discriminator =========================
else:
netD.train()
netG.eval()
netD.zero_grad()
# pass source inputs through generator network
s_generated, s_scale = netG(s_inputs)
# pass generated source data and target inputs through discriminator network
s_outputs, t_outputs = netD(s_generated), netD(t_inputs)
# compute loss
#D_loss = 0
D_loss = calc_gradient_penalty(netD, s_generated.data, t_inputs.data)
if args.psi2 == "EQ":
D_loss += torch.mean(s_scale*s_outputs) - torch.mean(t_outputs)
else:
D_loss += -torch.mean(s_scale*(LOG2.expand_as(s_outputs)+logsigmoid(s_outputs)-s_outputs)) - torch.mean(LOG2.expand_as(t_outputs)+logsigmoid(t_outputs))
# update params
D_loss.backward()
D_opt.step()
#================= Log results ===========================================
netD.eval()
netG.eval()
for s_inputs, t_inputs in loader:
num = s_inputs.size(0)
s_inputs, t_inputs = Variable(s_inputs), Variable(t_inputs)
if torch.cuda.is_available():
s_inputs, t_inputs = s_inputs.cuda(), t_inputs.cuda()
s_generated, s_scale = netG(s_inputs)
s_outputs, t_outputs = netD(s_generated), netD(t_inputs)
# update tracker
W_loss = args.lamb0*torch.mean(s_scale*torch.sum(mse(s_generated, s_inputs), dim=1)) + args.lamb1*torch.mean(-torch.log(s_scale)+s_scale)
W_loss += torch.mean(s_scale*(LOG2.expand_as(s_outputs)+logsigmoid(s_outputs)-s_outputs))
W_loss += torch.mean(LOG2.expand_as(t_outputs)+logsigmoid(t_outputs))
tracker.add(W_loss.cpu().data, num)
tracker.tick()
# save models
torch.save(netD.cpu().state_dict(), args.save_name+"_netD.pth")
torch.save(netG.cpu().state_dict(), args.save_name+"_netG.pth")
if torch.cuda.is_available():
netD.cuda()
netG.cuda()
# save tracker
with open(args.save_name+"_tracker.pkl", 'wb') as f:
pickle.dump(tracker, f)
if epoch % 100 == 0:
tracker_plot, scale_plot = utils.plot(tracker, tracker_plot, scale_plot, s_scale.cpu().data.numpy(), args.env, vis)