-
Notifications
You must be signed in to change notification settings - Fork 12
/
maml.py
270 lines (223 loc) · 12.3 KB
/
maml.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import copy
import os
import sys
sys.path.append('..')
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
import util as utils
from dataset import Metamovie
from logger import Logger
from MeLU import user_preference_estimator
import argparse
import torch
import time
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser([],description='Fast Context Adaptation via Meta-Learning (CAVIA),'
'Clasification experiments.')
parser.add_argument('--seed', type=int, default=53)
parser.add_argument('--task', type=str, default='multi', help='problem setting: sine or celeba')
parser.add_argument('--tasks_per_metaupdate', type=int, default=32, help='number of tasks in each batch per meta-update')
parser.add_argument('--lr_inner', type=float, default=0.01, help='inner-loop learning rate (per task)')
parser.add_argument('--lr_meta', type=float, default=1e-3, help='outer-loop learning rate (used with Adam optimiser)')
#parser.add_argument('--lr_meta_decay', type=float, default=0.9, help='decay factor for meta learning rate')
parser.add_argument('--num_grad_steps_inner', type=int, default=5, help='number of gradient steps in inner loop (during training)')
parser.add_argument('--num_grad_steps_eval', type=int, default=1, help='number of gradient updates at test time (for evaluation)')
parser.add_argument('--first_order', action='store_true', default=False, help='run first order approximation of CAVIA')
parser.add_argument('--data_root', type=str, default="./movielens/ml-1m", help='path to data root')
parser.add_argument('--num_workers', type=int, default=4, help='num of workers to use')
parser.add_argument('--test', action='store_true', default=False, help='num of workers to use')
parser.add_argument('--embedding_dim', type=int, default=32, help='num of workers to use')
parser.add_argument('--first_fc_hidden_dim', type=int, default=64, help='num of workers to use')
parser.add_argument('--second_fc_hidden_dim', type=int, default=64, help='num of workers to use')
parser.add_argument('--num_epoch', type=int, default=30, help='num of workers to use')
parser.add_argument('--num_genre', type=int, default=25, help='num of workers to use')
parser.add_argument('--num_director', type=int, default=2186, help='num of workers to use')
parser.add_argument('--num_actor', type=int, default=8030, help='num of workers to use')
parser.add_argument('--num_rate', type=int, default=6, help='num of workers to use')
parser.add_argument('--num_gender', type=int, default=2, help='num of workers to use')
parser.add_argument('--num_age', type=int, default=7, help='num of workers to use')
parser.add_argument('--num_occupation', type=int, default=21, help='num of workers to use')
parser.add_argument('--num_zipcode', type=int, default=3402, help='num of workers to use')
parser.add_argument('--rerun', action='store_true', default=False,
help='Re-run experiment (will override previously saved results)')
args = parser.parse_args()
# use the GPU if available
#args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#print('Running on device: {}'.format(args.device))
return args
def run(args, num_workers=1, log_interval=100, verbose=True, save_path=None):
code_root = os.path.dirname(os.path.realpath(__file__))
if not os.path.isdir('{}/{}_result_files/'.format(code_root, args.task)):
os.mkdir('{}/{}_result_files/'.format(code_root, args.task))
path = '{}/{}_result_files/'.format(code_root, args.task) + utils.get_path_from_args(args)
print('File saved in {}'.format(path))
if os.path.exists(path + '.pkl') and not args.rerun:
print('File has already existed. Try --rerun')
return utils.load_obj(path)
start_time = time.time()
utils.set_seed(args.seed)
# ---------------------------------------------------------
# -------------------- training ---------------------------
# initialise model
model = user_preference_estimator(args).cuda()
model.train()
print(sum([param.nelement() for param in model.parameters()]))
# set up meta-optimiser for model parameters
meta_optimiser = torch.optim.Adam(model.parameters(), args.lr_meta)
# scheduler = torch.optim.lr_scheduler.StepLR(meta_optimiser, 5000, args.lr_meta_decay)
# initialise logger
logger = Logger()
logger.args = args
# initialise the starting point for the meta gradient (it's faster to copy this than to create new object)
meta_grad_init = [0 for _ in range(len(model.state_dict()))]
dataloader_train = DataLoader(Metamovie(args),
batch_size=1,num_workers=args.num_workers)
for epoch in range(args.num_epoch):
x_spt, y_spt, x_qry, y_qry = [],[],[],[]
iter_counter = 0
for step, batch in enumerate(dataloader_train):
if len(x_spt)<args.tasks_per_metaupdate:
x_spt.append(batch[0][0].cuda())
y_spt.append(batch[1][0].cuda())
x_qry.append(batch[2][0].cuda())
y_qry.append(batch[3][0].cuda())
if not len(x_spt)==args.tasks_per_metaupdate:
continue
if len(x_spt) != args.tasks_per_metaupdate:
continue
# initialise meta-gradient
meta_grad = copy.deepcopy(meta_grad_init)
loss_pre = []
loss_after = []
for i in range(args.tasks_per_metaupdate):
loss_pre.append(F.mse_loss(model(x_qry[i]), y_qry[i]).item())
fast_parameters = model.final_part.parameters()
for weight in model.final_part.parameters():
weight.fast = None
for k in range(args.num_grad_steps_inner):
logits = model(x_spt[i])
loss = F.mse_loss(logits, y_spt[i])
grad = torch.autograd.grad(loss, fast_parameters, create_graph=True)
fast_parameters = []
for k, weight in enumerate(model.final_part.parameters()):
if weight.fast is None:
weight.fast = weight - args.lr_inner * grad[k] #create weight.fast
else:
weight.fast = weight.fast - args.lr_inner * grad[k]
fast_parameters.append(weight.fast)
logits_q = model(x_qry[i])
# loss_q will be overwritten and just keep the loss_q on last update step.
loss_q = F.mse_loss(logits_q, y_qry[i])
loss_after.append(loss_q.item())
task_grad_test = torch.autograd.grad(loss_q, model.parameters())
for g in range(len(task_grad_test)):
meta_grad[g] += task_grad_test[g].detach()
# -------------- meta update --------------
meta_optimiser.zero_grad()
# set gradients of parameters manually
for c, param in enumerate(model.parameters()):
param.grad = meta_grad[c] / float(args.tasks_per_metaupdate)
param.grad.data.clamp_(-10, 10)
# the meta-optimiser only operates on the shared parameters, not the context parameters
meta_optimiser.step()
#scheduler.step()
x_spt, y_spt, x_qry, y_qry = [],[],[],[]
loss_pre = np.array(loss_pre)
loss_after = np.array(loss_after)
logger.train_loss.append(np.mean(loss_pre))
logger.valid_loss.append(np.mean(loss_after))
logger.train_conf.append(1.96*np.std(loss_pre, ddof=0)/np.sqrt(len(loss_pre)))
logger.valid_conf.append(1.96*np.std(loss_after, ddof=0)/np.sqrt(len(loss_after)))
logger.test_loss.append(0)
logger.test_conf.append(0)
utils.save_obj(logger, path)
# print current results
logger.print_info(epoch, iter_counter, start_time)
start_time = time.time()
iter_counter += 1
if epoch % (2) == 0:
print('saving model at iter', epoch)
logger.valid_model.append(copy.deepcopy(model))
return logger, model
def evaluate(iter_counter, args, model, logger, dataloader, save_path):
logger.prepare_inner_loop(iter_counter, mode='valid')
for c, batch in enumerate(dataloader):
x_spt = batch[0].cuda()
y_spt = batch[1].cuda()
x_qry = batch[2].cuda()
y_qry = batch[3].cuda()
for i in range(x_spt.shape[0]):
# -------------- inner update --------------
logger.log_pre_update(iter_counter,
x_spt[i], y_spt[i],
x_qry[i], y_qry[i],
model, mode='valid')
fast_parameters = model.parameters()
for weight in model.parameters():
weight.fast = None
for k in range(args.num_grad_steps_eval):
logits = model(x_spt[i])
loss = F.cross_entropy(logits, y_spt[i])
grad = torch.autograd.grad(loss, fast_parameters, create_graph=True)
fast_parameters = []
for k, weight in enumerate(model.parameters()):
#for usage of weight.fast, please see Linear_fw, Conv_fw in backbone.py
if weight.fast is None:
weight.fast = weight - args.lr_inner * grad[k] #create weight.fast
else:
weight.fast = weight.fast - args.lr_inner * grad[k]
fast_parameters.append(weight.fast)
logger.log_post_update(iter_counter, x_spt[i], y_spt[i],
x_qry[i], y_qry[i], model, mode='valid')
# this will take the mean over the batches
logger.summarise_inner_loop(mode='valid')
# keep track of best models
logger.update_best_model(model, save_path)
def evaluate_test(args, model, dataloader):
model.eval()
loss_all = []
for c, batch in tqdm(enumerate(dataloader)):
x_spt = batch[0].cuda()
y_spt = batch[1].cuda()
x_qry = batch[2].cuda()
y_qry = batch[3].cuda()
for i in range(x_spt.shape[0]):
# -------------- inner update --------------
fast_parameters = model.final_part.parameters()
for weight in model.final_part.parameters():
weight.fast = None
for k in range(args.num_grad_steps_inner):
logits = model(x_spt[i])
loss = F.mse_loss(logits, y_spt[i])
grad = torch.autograd.grad(loss, fast_parameters, create_graph=True)
fast_parameters = []
for k, weight in enumerate(model.final_part.parameters()):
if weight.fast is None:
weight.fast = weight - args.lr_inner * grad[k] #create weight.fast
else:
weight.fast = weight.fast - args.lr_inner * grad[k]
fast_parameters.append(weight.fast)
loss_all.append(F.l1_loss(y_qry[i], model(x_qry[i])).item())
loss_all = np.array(loss_all)
print('{}+/-{}'.format(np.mean(loss_all), 1.96*np.std(loss_all,0)/np.sqrt(len(loss_all))))
if __name__ == '__main__':
args = parse_args()
if not args.test:
run(args, num_workers=1, log_interval=100, verbose=True, save_path=None)
else:
utils.set_seed(args.seed)
code_root = os.path.dirname(os.path.realpath(__file__))
mode_path = utils.get_path_from_args(args)
mode_path = '9b8290dd3f63cbafcd141ba21282c783'
path = '{}/{}_result_files/'.format(code_root, args.task) + mode_path
logger = utils.load_obj(path)
model = logger.valid_model[-1]
dataloader_test = DataLoader(Metamovie(args,partition='test',test_way='old'),#old, new_user, new_item, new_item_user
batch_size=1,num_workers=args.num_workers)
evaluate_test(args, model, dataloader_test)
# --- settings ---