-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtrain.py
185 lines (163 loc) · 6.35 KB
/
train.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
import argparse
import numpy as np
import stheno.torch as stheno
import torch
import convcnp.data
from convcnp.architectures import SimpleConv, UNet
from convcnp.cnp import RegressionANP as ANP
from convcnp.cnp import RegressionCNP as CNP
from convcnp.experiment import (
report_loss,
generate_root,
WorkingDirectory,
save_checkpoint
)
from convcnp.set_conv import ConvCNP
from convcnp.utils import device, gaussian_logpdf
def validate(data, model, report_freq=None):
"""Compute the validation loss."""
model.eval()
likelihoods = []
with torch.no_grad():
for step, task in enumerate(data):
num_target = task['y_target'].shape[1]
y_mean, y_std = \
model(task['x_context'], task['y_context'], task['x_target'])
obj = \
gaussian_logpdf(task['y_target'], y_mean, y_std,
'batched_mean')
likelihoods.append(obj.item() / num_target)
if report_freq:
avg_ll = np.array(likelihoods).mean()
report_loss('Validation', avg_ll, step, report_freq)
avg_ll = np.array(likelihoods).mean()
return avg_ll
def train(data, model, opt, report_freq):
"""Perform a training epoch."""
model.train()
losses = []
for step, task in enumerate(data):
y_mean, y_std = model(task['x_context'], task['y_context'],
task['x_target'])
obj = -gaussian_logpdf(task['y_target'], y_mean, y_std, 'batched_mean')
# Optimization
obj.backward()
opt.step()
opt.zero_grad()
# Track training progress
losses.append(obj.item())
avg_loss = np.array(losses).mean()
report_loss('Training', avg_loss, step, report_freq)
return avg_loss
# Parse arguments given to the script.
parser = argparse.ArgumentParser()
parser.add_argument('data',
choices=['eq',
'matern',
'noisy-mixture',
'weakly-periodic',
'sawtooth'],
help='Data set to train the CNP on. ')
parser.add_argument('model',
choices=['convcnp', 'convcnpxl', 'cnp', 'anp'],
help='Choice of model. ')
parser.add_argument('--root',
help='Experiment root, which is the directory from which '
'the experiment will run. If it is not given, '
'a directory will be automatically created.')
parser.add_argument('--train',
action='store_true',
help='Perform training. If this is not specified, '
'the model will be attempted to be loaded from the '
'experiment root.')
parser.add_argument('--epochs',
default=100,
type=int,
help='Number of epochs to train for.')
parser.add_argument('--learning_rate',
default=1e-3,
type=float,
help='Learning rate.')
parser.add_argument('--weight_decay',
default=1e-5,
type=float,
help='Weight decay.')
args = parser.parse_args()
# Load working directory.
if args.root:
wd = WorkingDirectory(root=args.root)
else:
experiment_name = f'{args.model}-{args.data}'
wd = WorkingDirectory(root=generate_root(experiment_name))
# Load data generator.
if args.data == 'sawtooth':
gen = convcnp.data.SawtoothGenerator()
gen_val = convcnp.data.SawtoothGenerator(num_tasks=60)
gen_test = convcnp.data.SawtoothGenerator(num_tasks=2048)
else:
if args.data == 'eq':
kernel = stheno.EQ().stretch(0.25)
elif args.data == 'matern':
kernel = stheno.Matern52().stretch(0.25)
elif args.data == 'noisy-mixture':
kernel = stheno.EQ().stretch(1.) + \
stheno.EQ().stretch(.25) + \
0.001 * stheno.Delta()
elif args.data == 'weakly-periodic':
kernel = stheno.EQ().stretch(0.5) * stheno.EQ().periodic(period=0.25)
else:
raise ValueError(f'Unknown data "{args.data}".')
gen = convcnp.data.GPGenerator(kernel=kernel)
gen_val = convcnp.data.GPGenerator(kernel=kernel, num_tasks=60)
gen_test = convcnp.data.GPGenerator(kernel=kernel, num_tasks=2048)
# Load model.
if args.model == 'convcnp':
model = ConvCNP(learn_length_scale=True,
points_per_unit=64,
architecture=SimpleConv())
elif args.model == 'convcnpxl':
model = ConvCNP(learn_length_scale=True,
points_per_unit=64,
architecture=UNet())
elif args.model == 'cnp':
model = CNP(latent_dim=128)
elif args.model == 'anp':
model = ANP(latent_dim=128)
else:
raise ValueError(f'Unknown model {args.model}.')
model.to(device)
# Perform training.
opt = torch.optim.Adam(model.parameters(),
args.learning_rate,
weight_decay=args.weight_decay)
if args.train:
# Run the training loop, maintaining the best objective value.
best_obj = -np.inf
for epoch in range(args.epochs):
print('\nEpoch: {}/{}'.format(epoch + 1, args.epochs))
# Compute training objective.
train_obj = train(gen, model, opt, report_freq=50)
report_loss('Training', train_obj, 'epoch')
# Compute validation objective.
val_obj = validate(gen_val, model, report_freq=20)
report_loss('Validation', val_obj, 'epoch')
# Update the best objective value and checkpoint the model.
is_best = False
if val_obj > best_obj:
best_obj = val_obj
is_best = True
save_checkpoint(wd,
{'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_acc_top1': best_obj,
'optimizer': opt.state_dict()},
is_best=is_best)
else:
# Load saved model.
load_dict = torch.load(wd.file('model_best.pth.tar', exists=True))
model.load_state_dict(load_dict['state_dict'])
# Finally, test model on ~2000 tasks.
test_obj = validate(gen_test, model)
print('Model averages a log-likelihood of %s on unseen tasks.' % test_obj)
with open(wd.file('test_log_likelihood.txt'), 'w') as f:
f.write(str(test_obj))