-
Notifications
You must be signed in to change notification settings - Fork 0
/
critic_trainer.py
196 lines (153 loc) · 7.56 KB
/
critic_trainer.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
"""This is code for training a critic with or without minibatch optimal ray selection.
Once the critic is trained, contour plots are made which record some gradient flow lines for n points. An empirical
histogram for the gradient penalty sampling strategy is also created.
"""
import os
import sys
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), 'networks'))
sys.path.append(os.path.join(os.getcwd(), 'helpers'))
sys.path.append(os.path.join(os.getcwd(), 'plotting'))
import argparse
import time
import log
import json
import random
import numpy as np
import torch
from torch import optim
from calc_gradient_penalty import calc_gradient_penalty
from compute_emd import compute_emd
from scatter_plot import scatter_plot
from contour_plot import contour_plot
from sigma_plot import sigma_plot
import window_finder
import dataloader
import networks
from get_data import get_data
# get command line args~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parser = argparse.ArgumentParser('Testbed for training critics with MORS')
parser.add_argument('--save_dir', type=str, required=True, help='directory for saving')
# inputs for datasets
parser.add_argument('--source', type=str, required=True, default='circle',
choices=['circle', 'rotated_circle', 'line', 'gaussian', 'uniform', 'sin', 'spiral',
'hollow_rectangle'],
help='Which source distribution?')
parser.add_argument('--source_params', nargs='+', help='A list specifying the source dist. Enter 0 to get syntax',
required=True, type=float)
parser.add_argument('--target', type=str, required=True, default='circle',
choices=['circle', 'rotated_circle', 'line', 'gaussian', 'uniform', 'sin', 'spiral',
'hollow_rectangle'],
help='Which target distribution?')
parser.add_argument('--target_params', nargs='+', help='A list specifying the target dist. Enter 0 to get syntax',
required=True, type=float)
parser.add_argument('--bs', type=int, default=128, help='batch size')
# critic parameters
parser.add_argument('--dim', type=int, default=128, help='int determining width of critic')
parser.add_argument('--lamb', type=float, default=0.1, help='parameter multiplying gradient penalty')
parser.add_argument('--clr', type=float, default=0.01, help='learning rate for critic updates')
parser.add_argument('--critters', type=int, default=5000, help='number of iters to train critic')
parser.add_argument('--ot', action='store_true', help='use minibatch optimal ray selection')
parser.add_argument('--p', type=int, default=1, help='power for ot cost matrix; only used if ot is True')
parser.add_argument('--relu', action='store_true', help='use relus in discriminator')
# plotting args
parser.add_argument('--num_points', type=int, default=20,
help='For generating flow lines - Number of points to flow')
parser.add_argument('--num_step', type=int, default=200,
help='For generating flow lines - Number of steps for gradient descent iterations')
parser.add_argument('--step_size', type=float, default=0.001,
help='For generating flow lines - Step size for gradient descent iterations')
parser.add_argument('--nice_contours', action='store_true',
help='use automatic spacing for contours. If False, use spacing of 1')
parser.add_argument('--xwin', nargs='+',
help='A list for plotting window. Syntax: [xlow, xhigh]. If not specified'
'will compute automatically from data', default=[0, 0], type=float)
parser.add_argument('--ywin', nargs='+',
help='A list for plotting window. Syntax: [ylow, yhigh]. If not specified'
'will compute automatically from data', default=[0, 0], type=float)
# random seed
parser.add_argument('--seed', type=int, default=-1, help='Reproducible if not equal to -1')
args = parser.parse_args()
# code to get deterministic behaviour
if args.seed != -1: # if non-default seed
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False # If true, optimizes conv for hardware, but gives non-determ. behaviour
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
# save args in config file
config_file_name = os.path.join(args.save_dir, 'train_config.txt')
with open(config_file_name, 'w') as f:
json.dump(args.__dict__, f, indent=2)
# begin definitions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source_loader = getattr(dataloader, args.source)(args.source_params, args.bs)
source_gen = iter(source_loader)
target_loader = getattr(dataloader, args.target)(args.target_params, args.bs)
target_gen = iter(target_loader)
critic = getattr(networks, 'Discriminator')(args.dim, relu=args.relu)
generator = getattr(networks, 'Identity')()
print(critic)
print('Arguments:')
for p in vars(args).items():
print(' ', p[0] + ': ', p[1])
print('\n')
use_cuda = torch.cuda.is_available()
if use_cuda:
critic = critic.cuda()
beta_1 = 0.0
beta_2 = 0.9
print('Using betas of {} and {} for Adam'.format(beta_1, beta_2))
optimizerD = optim.Adam(critic.parameters(), lr=args.clr, betas=(beta_1, beta_2))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
start_time = time.time()
max_len = 1000000 # max number of points for histogram
interpolates_history = []
flowed_points = get_data(source_gen)[0:args.num_points, :]
# Train critic
for iteration in range(args.critters):
############################
# (1) Update D network
###########################
real = get_data(target_gen)
for param in critic.parameters(): # more efficient than critic.zero_grad()
param.grad = None
D_real = critic(real)
D_real = D_real.mean()
D_real.backward()
# generate fake data
fake = get_data(source_gen)
if args.ot:
G0, mb_wcost = compute_emd(np.array(real.cpu()), np.array(fake.cpu()), args.p)
otmap = torch.LongTensor(G0.dot(np.arange(G0.shape[0])))
fake = fake[otmap, :].clone().detach() # unscrambles fake data according to OT plan.
D_fake = -critic(fake)
D_fake = D_fake.mean()
D_fake.backward()
# compute gradient penalty
gradient_penalty, interpolates = calc_gradient_penalty(critic, real, fake, args, return_samples=True)
gradient_penalty.backward()
if len(interpolates_history) < max_len:
interpolates_history.append(interpolates.cpu())
D_cost = D_fake + D_real + gradient_penalty # D_fake has negative baked in
nopen = D_real + D_fake
optimizerD.step()
# record training metrics
log.plot('dcost', D_cost.cpu().data.numpy())
log.plot('time', time.time() - start_time)
log.plot('no_gpen', nopen.cpu().data.numpy())
if args.ot:
log.plot('mb_Wcost', mb_wcost)
log.tick() # increments log by one
if iteration % 1000 == 999 or iteration == args.critters - 1:
log.flush(args.save_dir) # saves log
interpolates_history = torch.cat(interpolates_history, dim=0)
if args.xwin == [0, 0] or args.ywin == [0, 0]:
_ = window_finder.window_finder(interpolates_history, args) # finds window for plotting from data
# plot initial distributions, contour, and sigma histogram
scatter_plot(generator, args)
contour_plot(critic, args, data=flowed_points)
sigma_plot(interpolates_history, iteration, args)
# save log to reflect training setup
path = os.path.join(args.save_dir, 'log.pkl')
os.replace(path, os.path.join(args.save_dir, 'log_ot{}_p{}.pkl'.format(args.ot, args.p)))