-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.py
425 lines (334 loc) · 16.4 KB
/
meta.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
from torch import optim
import numpy as np
from neural_nets import Learner
from copy import deepcopy
import argparse
import os
from time import localtime, strftime
import rospy
class Meta(nn.Module):
"""
Meta Learner
"""
def __init__(self, args, config):
"""
:param args:
"""
super(Meta, self).__init__()
self.update_lr = args.update_lr
self.meta_lr = args.meta_lr
self.update_step = args.update_step
self.update_step_test = args.update_step_test
self.fast_adapted_params = None
self.net = Learner(config)
self.meta_optim = optim.Adam(self.net.parameters(), lr=self.meta_lr)
self.model_path = os.path.join("/home/wawa/catkin_meta/src/MBRL_transport/model_3d",strftime("%Y-%m-%d--%H:%M:%S", localtime()))
# self.online_train_meta = args.online_train_meta
if args.load_model:
test_model_path = os.path.join("/home/wawa/catkin_meta/src/MBRL_transport/model_3d")
if os.path.exists(test_model_path + '/params.pkl'):
self.initialise_networks(test_model_path+'/params.pkl')
print('Agent successfully loaded meta_network: {}'.format(test_model_path + '/params.pkl'))
def clip_grad_by_norm_(self, grad, max_norm):
"""
in-place gradient clipping.
:param grad: list of gradients
:param max_norm: maximum norm allowable
:return:
"""
total_norm = 0
counter = 0
for g in grad:
param_norm = g.data.norm(2)
total_norm += param_norm.item() ** 2
counter += 1
total_norm = total_norm ** (1. / 2)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for g in grad:
g.data.mul_(clip_coef)
return total_norm/counter
def forward(self, x_spt, y_spt, x_qry, y_qry, i_ter):
"""
:param x_spt: [b, setsz, nninput]
:param y_spt: [b, setsz, output]
:param x_qry: [b, querysz, nninput]
:param y_qry: [b, querysz, output]
:return:
"""
task_num, setsz, input_dim = x_spt.size()
querysz = x_qry.size(1)
losses_q = [0 for _ in range(self.update_step + 1)] # losses_q[i] is the loss on step i
corrects = [0 for _ in range(self.update_step + 1)] # record accuracy
# print(self.update_step)
for i in range(task_num):
# 1. run the i-th task and compute loss for k=0
logits = self.net(x_spt[i], vars=None, bn_training=True)
#self.loss = tf.reduce_mean(tf.square(self.delta_ph - self.delta_pred))
loss = F.mse_loss(logits, y_spt[i]) #change to mean squared error
grad = torch.autograd.grad(loss, self.net.parameters())
#separate weights to not influence meta update
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# print(fast_weights)
# this is the loss and accuracy before first update
with torch.no_grad():
# [setsz, nway]
logits_q = self.net(x_qry[i], self.net.parameters(), bn_training=True)
loss_q = F.mse_loss(logits_q, y_qry[i])
losses_q[0] += loss_q
correct = loss_q.item()
corrects[0] = corrects[0] + correct
# this is the loss and accuracy after the first update
with torch.no_grad():
# [setsz, nway]
# print(x_qry[i])
logits_q = self.net(x_qry[i], fast_weights, bn_training=True)
loss_q = F.mse_loss(logits_q, y_qry[i])
# print(logits_q, y_qry[i])
losses_q[1] += loss_q
# [setsz]
correct = loss_q.item()
corrects[1] = corrects[1] + correct
for k in range(1, self.update_step):
# 1. run the i-th task and compute loss for k=1~K-1
logits = self.net(x_spt[i], fast_weights, bn_training=True)
loss = F.mse_loss(logits, y_spt[i])
# 2. compute grad on theta_pi
grad = torch.autograd.grad(loss, fast_weights)
# 3. theta_pi = theta_pi - train_lr * grad
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, fast_weights)))
logits_q = self.net(x_qry[i], fast_weights, bn_training=True)
# 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])
losses_q[k + 1] += loss_q
with torch.no_grad():
correct = loss_q.item() # convert to numpy
corrects[k + 1] = corrects[k + 1] + correct
# end of all tasks
# sum over all losses on query set across all tasks
loss_q = losses_q[-1] / task_num
# optimize theta parameters
self.meta_optim.zero_grad()
loss_q.backward()
# print('meta update')
# for p in self.net.parameters()[:5]:
# print(torch.norm(p).item())
self.meta_optim.step()
if i_ter%100==0:
self.save_model(i_ter)
accs = np.array(corrects) / task_num
return accs
def finetunning(self, x_spt, y_spt, x_qry, y_qry):
"""
in meta test, we firstly adapt our model into the new scenario and use this model to do the control part
:param x_spt: [setsz, nn_input]
:param y_spt: [setsz, nn_output]
:param x_qry: [querysz, nn_input]
:param y_qry: [querysz, nn_output]
:return:
"""
assert len(x_spt.shape) == 2
querysz = x_qry.size(0)
corrects = [0 for _ in range(self.update_step_test + 1)]
# in order to not ruin the state of running_mean/variance and bn_weight/bias
# we finetunning on the copied model instead of self.net
net = deepcopy(self.net)
# 1. run the i-th task and compute loss for k=0
# here we just use the default parameter to forward test of NN
logits = net(x_spt)
loss = F.mse_loss(logits, y_spt)
grad = torch.autograd.grad(loss, net.parameters())
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, net.parameters())))
# this is the loss and accuracy before first update
with torch.no_grad():
# [setsz, nway]
logits_q = net(x_qry, net.parameters(), bn_training=True)
# scalar
correct = F.mse_loss(logits_q, y_qry).item()
corrects[0] = corrects[0] + correct
# this is the loss and accuracy after the first update
with torch.no_grad():
# [setsz, nway]
logits_q = net(x_qry, fast_weights, bn_training=True)
# scalar
correct = F.mse_loss(logits_q, y_qry).item()
corrects[1] = corrects[1] + correct
for k in range(1, self.update_step_test):
# 1. run the i-th task and compute loss for k=1~K-1
logits = net(x_spt, fast_weights, bn_training=True)
loss = F.mse_loss(logits, y_spt)
# 2. compute grad on theta_pi
grad = torch.autograd.grad(loss, fast_weights)
# 3. theta_pi = theta_pi - train_lr * grad
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, fast_weights)))
logits_q = net(x_qry, fast_weights, bn_training=True)
# loss_q will be overwritten and just keep the loss_q on last update step.
loss_q = F.mse_loss(logits_q, y_qry)
with torch.no_grad():
correct = loss_q.item() # convert to numpy
corrects[k + 1] = corrects[k + 1] + correct
del net
accs = np.array(corrects)
return accs
def pre_forward(self, x):
#input [8000,dim] 8000 is the sum of the sampled particles
#for meta test time, the meta offline model will be used firstly before using the adapted model
with torch.no_grad():
predictions = self.net(x)
return predictions
# def adapt(self,x,y):
# #[k_shot, dim]
# # losses = [0 for _ in range(self.update_adapt_step + 1)] # record loss on steo i
# # corrects = [0 for _ in range(self.update_adapt_step + 1)] # record accuracy
# # self.fast_adapted_params = None
# #firstly we only consider one step update for efficiency
# logits = self.net(x, vars=self.fast_adapted_params, bn_training=True)
# # logits = self.net(x, vars=None, bn_training=True)
# loss = F.mse_loss(logits, y)
# # grad = torch.autograd.grad(loss, self.net.parameters())
# if self.fast_adapted_params == None:
# grad = torch.autograd.grad(loss, self.net.parameters())
# fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# else:
# grad = torch.autograd.grad(loss, self.fast_adapted_params)
# fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.fast_adapted_params)))
# #separate weights to not influence meta update
# # fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# for k in range(1, self.update_step):
# logits = self.net(x, fast_weights, bn_training=True)
# loss = F.mse_loss(logits, y)
# # 2. compute grad on theta_pi
# grad = torch.autograd.grad(loss, fast_weights)
# # 3. theta_pi = theta_pi - train_lr * grad
# fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, fast_weights)))
# self.fast_adapted_params = fast_weights
def adapt(self,x,y):
#[k_shot, dim]
# losses = [0 for _ in range(self.update_adapt_step + 1)] # record loss on steo i
# corrects = [0 for _ in range(self.update_adapt_step + 1)] # record accuracy
self.fast_adapted_params = None
#firstly we only consider one step update for efficiency
logits = self.net(x, vars=None, bn_training=True)
loss = F.mse_loss(logits, y)
grad = torch.autograd.grad(loss, self.net.parameters())
#separate weights to not influence meta update
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
for k in range(1, self.update_step):
logits = self.net(x, fast_weights, bn_training=True)
loss = F.mse_loss(logits, y)
# 2. compute grad on theta_pi
grad = torch.autograd.grad(loss, fast_weights)
# 3. theta_pi = theta_pi - train_lr * grad
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, fast_weights)))
self.fast_adapted_params = fast_weights
def post_forward(self, x):
#[k_shot, dim]
assert not(self.fast_adapted_params==None)
#input [8000,dim] 8000 is the sum of the sampled particles
with torch.no_grad():
logits_q = self.net(x, self.fast_adapted_params, bn_training=True)
return logits_q
########################################################
# warning! this function is only for testing in the offline_meta.py
def single_tunning(self, x_spt, y_spt, x_qry, y_qry):
"""
in meta test, we firstly adapt our model into the new scenario and use this model to do the control part
:param x_spt: [setsz, nn_input]
:param y_spt: [setsz, nn_output]
:param x_qry: [querysz, nn_input]
:param y_qry: [querysz, nn_output]
:return:
"""
assert len(x_spt.shape) == 2
querysz = x_qry.size(0)
corrects = [0 for _ in range(self.update_step_test + 1)]
# in order to not ruin the state of running_mean/variance and bn_weight/bias
# we finetunning on the copied model instead of self.net
# net = deepcopy(self.net)
spt = 15
qry = 15
# 1. run the i-th task and compute loss for k=0
# here we just use the default parameter to forward test of NN
logits = self.net(x_spt[:spt])
loss = F.mse_loss(logits, y_spt[:spt])
grad = torch.autograd.grad(loss, self.net.parameters())
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, self.net.parameters())))
# this is the loss and accuracy before first update
with torch.no_grad():
# [setsz, nway]
logits_q = self.net(x_qry[:qry], self.net.parameters(), bn_training=True)
# scalar
correct = F.mse_loss(logits_q, y_qry[:qry]).item()
corrects[0] = corrects[0] + correct
# this is the loss and accuracy after the first update
with torch.no_grad():
# [setsz, nway]
logits_q = self.net(x_qry[:qry], fast_weights, bn_training=True)
# scalar
correct = F.mse_loss(logits_q, y_qry[:qry]).item()
corrects[1] = corrects[1] + correct
for k in range(1, self.update_step_test):
# 1. run the i-th task and compute loss for k=1~K-1
logits = self.net(x_spt[:spt], fast_weights, bn_training=True)
loss = F.mse_loss(logits, y_spt[:spt])
# 2. compute grad on theta_pi
grad = torch.autograd.grad(loss, fast_weights)
# 3. theta_pi = theta_pi - train_lr * grad
fast_weights = list(map(lambda p: p[1] - self.update_lr * p[0], zip(grad, fast_weights)))
logits_q = self.net(x_qry[:qry], fast_weights, bn_training=True)
# loss_q will be overwritten and just keep the loss_q on last update step.
loss_q = F.mse_loss(logits_q, y_qry[:qry])
with torch.no_grad():
correct = loss_q.item() # convert to numpy
corrects[k + 1] = corrects[k + 1] + correct
# del net
accs = np.array(corrects)
return accs
########################################################
def initialise_networks(self, path):
checkpoint = torch.load(path) # load the torch data
self.net.load_state_dict(checkpoint['meta_params']) # actor parameters
self.meta_optim.load_state_dict(checkpoint['meta_optim_params']) # critic optimiser state
def save_model(self, train_step, model_path=None):
num = str(train_step)
if model_path == None:
model_path = self.model_path
if not os.path.exists(model_path):
os.makedirs(model_path)
save_dict = {'meta_params' : self.net.state_dict(),
'meta_optim_params' : self.meta_optim.state_dict()}
torch.save(save_dict, model_path + '/' + num + '_params.pkl')
def main():
#3 hidden layers of 512 units with ReLU
obs_dim = 4
act_dim = 2
#config [out,in]
config = [
('linear', [512, obs_dim+1+act_dim]),
('relu', [True]),
('linear', [512, 512]),
('relu', [True]),
('linear', [obs_dim, 512]),
]
device = torch.device('cpu')
argparser = argparse.ArgumentParser()
argparser.add_argument('--epoch', type=int, help='epoch number', default=40000)
argparser.add_argument('--n_way', type=int, help='n way', default=5)
argparser.add_argument('--k_spt', type=int, help='k shot for support set', default=1)
argparser.add_argument('--k_qry', type=int, help='k shot for query set', default=15)
argparser.add_argument('--imgsz', type=int, help='imgsz', default=28)
argparser.add_argument('--imgc', type=int, help='imgc', default=1)
argparser.add_argument('--task_num', type=int, help='meta batch size, namely task num', default=32)
argparser.add_argument('--meta_lr', type=float, help='meta-level outer learning rate', default=1e-3)
argparser.add_argument('--update_lr', type=float, help='task-level inner update learning rate', default=0.4)
argparser.add_argument('--update_step', type=int, help='task-level inner update steps', default=5)
argparser.add_argument('--update_step_test', type=int, help='update steps for finetunning', default=10)
args = argparser.parse_args()
maml = Meta(args, config).to(device)
print(maml)
if __name__ == '__main__':
main()