-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
151 lines (125 loc) · 5.28 KB
/
tools.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
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.nn import functional as F
from torch.autograd import Variable as V
import pandas as pd
import seaborn as sbs
from net import SineModel
from tqdm import tqdm
import random
def sine_fit1(net, wave, optim=None, get_test_loss=False, create_graph=False, force_new=False):
net.train()
if optim is not None:
optim.zero_grad()
x, y = wave.training_set(force_new=force_new)
loss = F.mse_loss(net(V(x[:, None])), V(y))
loss.backward(create_graph=create_graph, retain_graph=True)
if optim is not None:
optim.step()
if get_test_loss:
net.eval()
x, y = wave.test_set()
loss_test = F.mse_loss(net(V(x[:, None])), V(y))
return loss.data.cpu().numpy()[0], loss_test.data.cpu().numpy()[0]
return loss.data.cpu().numpy()[0]
def plot_sine_test(model, test, fits=(0, 1), lr=0.01):
xtest, ytest = test.test_set()
xtrain, ytrain = test.training_set()
fit_res = eval_sine_test(model, test, fits, lr)
train, = plt.plot(xtrain.numpy(), ytrain.numpy(), '^')
ground_truth, = plt.plot(xtest.numpy(), ytest.numpy())
plots = [train, ground_truth]
legend = ['Training Points', 'True Function']
for n, res, loss in fit_res:
cur, = plt.plot(xtest.numpy(), res.cpu().data.numpy()[:, 0], '--')
plots.append(cur)
legend.append('After {%d} Steps' % (n))
plt.legend(plots, legend)
plt.show()
def copy_sine_model(model):
m = SineModel()
m.copy(model)
return m
def eval_sine_test(model, test, fits=(0, 1), lr=0.01):
xtest, ytest = test.test_set()
xtrain, ytrain = test.training_set()
model = copy_sine_model(model)
# Not sure if this should be Adam or SGD.
optim = torch.optim.SGD(model.params(), lr)
def get_loss(res):
return F.mse_loss(res, V(ytest[:, None])).cpu().data.numpy()[0]
fit_res = []
if 0 in fits:
results = model(V(xtest[:, None]))
fit_res.append((0, results, get_loss(results)))
for i in range(np.max(fits)):
sine_fit1(model, test, optim)
if i + 1 in fits:
results = model(V(xtest[:, None]))
fit_res.append(
(
i + 1,
results,
get_loss(results)
)
)
return fit_res
def plot_sine_learning(models, fits=(0, 1), lr=0.01, marker='s', linestyle='--', SINE_TEST=None):
data = {'model': [], 'fits': [], 'loss': [], 'set': []}
for name, models in models:
if not isinstance(models, list):
models = [models]
for n_model, model in enumerate(models):
for n_test, test in enumerate(SINE_TEST):
n_test = n_model * len(SINE_TEST) + n_test
fit_res = eval_sine_test(model, test, fits, lr)
for n, _, loss in fit_res:
data['model'].append(name)
data['fits'].append(n)
data['loss'].append(loss)
data['set'].append(n_test)
ax = sbs.tsplot(
pd.DataFrame(data), condition='model', value='loss',
time='fits', unit='set', marker=marker, linestyle=linestyle)
plt.show()
def maml_sine(model, epochs, lr_inner=0.01, batch_size=1, first_order=False, SINE_TRAIN=None):
optimizer = torch.optim.Adam(model.params())
for _ in tqdm(range(epochs)):
# Note: the paper doesn't specify the meta-batch size for this task,
# so I just use 1 for now.
for i, t in enumerate(random.sample(SINE_TRAIN, len(SINE_TRAIN))):
new_model = SineModel()
new_model.copy(model, same_var=True)
# print(dir(new_model))
loss = sine_fit1(new_model, t, create_graph=not first_order)
for name, param in new_model.named_params():
grad = param.grad
if first_order:
grad = V(grad.detach().data)
new_model.set_param(name, param - lr_inner * grad)
sine_fit1(new_model, t, force_new=True)
if (i + 1) % batch_size == 0:
optimizer.step()
optimizer.zero_grad()
def reptile_sine(model, epochs, lr_inner=0.01, lr_outer=0.001, k=32, batch_size=32, SINE_TRAIN=None):
optimizer = torch.optim.Adam(model.params(), lr=lr_outer)
name_to_param = dict(model.named_params())
for _ in tqdm(range(epochs)):
for i, t in enumerate(random.sample(SINE_TRAIN, len(SINE_TRAIN))):
new_model = SineModel()
new_model.copy(model)
inner_optim = torch.optim.SGD(new_model.params(), lr=lr_inner)
for _ in range(k):
sine_fit1(new_model, t, inner_optim)
for name, param in new_model.named_params():
cur_grad = (name_to_param[name].data - param.data) / k / lr_inner
if name_to_param[name].grad is None:
name_to_param[name].grad = V(torch.zeros(cur_grad.size()))
name_to_param[name].grad.data.add_(cur_grad / batch_size)
# if (i + 1) % 500 == 0:
# print(name_to_param[name].grad)
if (i + 1) % batch_size == 0:
to_show = name_to_param['hidden1.bias']
optimizer.step()
optimizer.zero_grad()