forked from facebookresearch/GradientEpisodicMemory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
255 lines (200 loc) · 8.02 KB
/
main.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
# Copyright 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import importlib
import datetime
import argparse
import random
import uuid
import time
import os
import numpy as np
import torch
from torch.autograd import Variable
from metrics.metrics import confusion_matrix
# continuum iterator #########################################################
def load_datasets(args):
d_tr, d_te = torch.load(args.data_path + '/' + args.data_file)
n_inputs = d_tr[0][1].size(1)
n_outputs = 0
for i in range(len(d_tr)):
n_outputs = max(n_outputs, d_tr[i][2].max())
n_outputs = max(n_outputs, d_te[i][2].max())
return d_tr, d_te, n_inputs, n_outputs + 1, len(d_tr)
class Continuum:
def __init__(self, data, args):
self.data = data
self.batch_size = args.batch_size
n_tasks = len(data)
task_permutation = range(n_tasks)
if args.shuffle_tasks == 'yes':
task_permutation = torch.randperm(n_tasks).tolist()
sample_permutations = []
for t in range(n_tasks):
N = data[t][1].size(0)
if args.samples_per_task <= 0:
n = N
else:
n = min(args.samples_per_task, N)
p = torch.randperm(N)[0:n]
sample_permutations.append(p)
self.permutation = []
for t in range(n_tasks):
task_t = task_permutation[t]
for _ in range(args.n_epochs):
task_p = [[task_t, i] for i in sample_permutations[task_t]]
random.shuffle(task_p)
self.permutation += task_p
self.length = len(self.permutation)
self.current = 0
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
if self.current >= self.length:
raise StopIteration
else:
ti = self.permutation[self.current][0]
j = []
i = 0
while (((self.current + i) < self.length) and
(self.permutation[self.current + i][0] == ti) and
(i < self.batch_size)):
j.append(self.permutation[self.current + i][1])
i += 1
self.current += i
j = torch.LongTensor(j)
return self.data[ti][1][j], ti, self.data[ti][2][j]
# train handle ###############################################################
def eval_tasks(model, tasks, args):
model.eval()
result = []
for i, task in enumerate(tasks):
t = i
x = task[1]
y = task[2]
rt = 0
eval_bs = x.size(0)
for b_from in range(0, x.size(0), eval_bs):
b_to = min(b_from + eval_bs, x.size(0) - 1)
if b_from == b_to:
xb = x[b_from].view(1, -1)
yb = torch.LongTensor([y[b_to]]).view(1, -1)
else:
xb = x[b_from:b_to]
yb = y[b_from:b_to]
if args.cuda:
xb = xb.cuda()
xb = Variable(xb, volatile=True)
_, pb = torch.max(model(xb, t).data.cpu(), 1, keepdim=False)
rt += (pb == yb).float().sum()
result.append(rt / x.size(0))
return result
def life_experience(model, continuum, x_te, args):
result_a = []
result_t = []
current_task = 0
time_start = time.time()
for (i, (x, t, y)) in enumerate(continuum):
if(((i % args.log_every) == 0) or (t != current_task)):
result_a.append(eval_tasks(model, x_te, args))
result_t.append(current_task)
current_task = t
v_x = x.view(x.size(0), -1)
v_y = y.long()
if args.cuda:
v_x = v_x.cuda()
v_y = v_y.cuda()
model.train()
model.observe(Variable(v_x), t, Variable(v_y))
result_a.append(eval_tasks(model, x_te, args))
result_t.append(current_task)
time_end = time.time()
time_spent = time_end - time_start
return torch.Tensor(result_t), torch.Tensor(result_a), time_spent
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Continuum learning')
# model parameters
parser.add_argument('--model', type=str, default='single',
help='model to train')
parser.add_argument('--n_hiddens', type=int, default=100,
help='number of hidden neurons at each layer')
parser.add_argument('--n_layers', type=int, default=2,
help='number of hidden layers')
# memory parameters
parser.add_argument('--n_memories', type=int, default=0,
help='number of memories per task')
parser.add_argument('--memory_strength', default=0, type=float,
help='memory strength (meaning depends on memory)')
parser.add_argument('--finetune', default='no', type=str,
help='whether to initialize nets in indep. nets')
# optimizer parameters
parser.add_argument('--n_epochs', type=int, default=1,
help='Number of epochs per task')
parser.add_argument('--batch_size', type=int, default=10,
help='batch size')
parser.add_argument('--lr', type=float, default=1e-3,
help='SGD learning rate')
# experiment parameters
parser.add_argument('--cuda', type=str, default='no',
help='Use GPU?')
parser.add_argument('--seed', type=int, default=0,
help='random seed')
parser.add_argument('--log_every', type=int, default=100,
help='frequency of logs, in minibatches')
parser.add_argument('--save_path', type=str, default='results/',
help='save models at the end of training')
# data parameters
parser.add_argument('--data_path', default='data/',
help='path where data is located')
parser.add_argument('--data_file', default='mnist_permutations.pt',
help='data file')
parser.add_argument('--samples_per_task', type=int, default=-1,
help='training samples per task (all if negative)')
parser.add_argument('--shuffle_tasks', type=str, default='no',
help='present tasks in order')
args = parser.parse_args()
args.cuda = True if args.cuda == 'yes' else False
args.finetune = True if args.finetune == 'yes' else False
# multimodal model has one extra layer
if args.model == 'multimodal':
args.n_layers -= 1
# unique identifier
uid = uuid.uuid4().hex
# initialize seeds
torch.backends.cudnn.enabled = False
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if args.cuda:
torch.cuda.manual_seed_all(args.seed)
# load data
x_tr, x_te, n_inputs, n_outputs, n_tasks = load_datasets(args)
# set up continuum
continuum = Continuum(x_tr, args)
# load model
Model = importlib.import_module('model.' + args.model)
model = Model.Net(n_inputs, n_outputs, n_tasks, args)
if args.cuda:
model.cuda()
# run model on continuum
result_t, result_a, spent_time = life_experience(
model, continuum, x_te, args)
# prepare saving path and file name
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
fname = args.model + '_' + args.data_file + '_'
fname += datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
fname += '_' + uid
fname = os.path.join(args.save_path, fname)
# save confusion matrix and print one line of stats
stats = confusion_matrix(result_t, result_a, fname + '.txt')
one_liner = str(vars(args)) + ' # '
one_liner += ' '.join(["%.3f" % stat for stat in stats])
print(fname + ': ' + one_liner + ' # ' + str(spent_time))
# save all results in binary file
torch.save((result_t, result_a, model.state_dict(),
stats, one_liner, args), fname + '.pt')