-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlifelong_experiment.py
363 lines (344 loc) · 17.2 KB
/
lifelong_experiment.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
import struct
import numpy as np
import torch
import argparse
import os
from datasets import datasets
from models.mlp import MLP
from models.mlp_soft_lifelong import MLPSoftLL
from models.mlp_soft_lifelong_dynamic import MLPSoftLLDynamic
from models.mlp_soft_gated_lifelong import MLPSoftGatedLL
from models.mlp_soft_gated_lifelong_dynamic import MLPSoftGatedLLDynamic
from models.cnn import CNN
from models.cnn_soft_lifelong import CNNSoftLL
from models.cnn_soft_lifelong_dynamic import CNNSoftLLDynamic
from models.cnn_soft_gated_lifelong import CNNSoftGatedLL
from models.cnn_soft_gated_lifelong_dynamic import CNNSoftGatedLLDynamic
from models.linear import Linear
from models.linear_factored import LinearFactored
# Explicitly compositional with dynamic module number (Ours)
from learners.er_dynamic import CompositionalDynamicER
from learners.ewc_dynamic import CompositionalDynamicEWC
from learners.van_dynamic import CompositionalDynamicVAN
from learners.fm_dynamic import CompositionalDynamicFM
# Explicitly compositional (Ours)
from learners.er_compositional import CompositionalER
from learners.van_compositional import CompositionalVAN
from learners.ewc_compositional import CompositionalEWC
from learners.fm_compositional import CompositionalFM
# Implicitly compositional baselines (composition in the model, not in training)
from learners.ewc_joint import JointEWC
from learners.er_joint import JointER
from learners.van_joint import JointVAN
# No-components baselines (no composition in the model or in training)
from learners.ewc_nocomponents import NoComponentsEWC
from learners.er_nocomponents import NoComponentsER
from learners.van_nocomponents import NoComponentsVAN
SEED_SCALE = 10
def main(num_tasks=10,
dataset='MNIST',
num_epochs=100,
batch_size=64,
component_update_frequency=100,
ewc_lambda=1e-5,
replay_size=-1,
layer_size=64,
num_layers=4,
num_init_tasks=4,
init_mode='random_onehot',
architecture='mlp',
algorithm='er_compositional',
num_seeds=1,
results_root='./tmp/results',
save_frequency=1,
initial_seed=0,
num_train=-1):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for seed in range(initial_seed, initial_seed + num_seeds):
torch.manual_seed(seed * SEED_SCALE)
np.random.seed(seed * SEED_SCALE)
if dataset == 'MNIST':
torch_dataset = datasets.BinaryMNIST(num_tasks, num_train=num_train)
freeze_encoder = True
elif dataset == 'Fashion':
torch_dataset = datasets.BinaryFashionMNIST(num_tasks, num_train=num_train)
freeze_encoder = True
elif dataset == 'CIFAR':
padding = 1
torch_dataset = datasets.SplitCIFAR100(num_tasks)
elif dataset == 'CUB':
torch_dataset = datasets.SplitCUB200(num_tasks, num_train=num_train)
freeze_encoder = False
elif dataset == 'Omniglot':
padding = 0
torch_dataset = datasets.Omniglot(num_tasks)
elif dataset == 'Landmine':
torch_dataset = datasets.Landmine(num_tasks)
elif dataset == 'FacialRecognition':
torch_dataset = datasets.FacialRecognition(num_tasks)
elif dataset == 'LondonSchool':
torch_dataset = datasets.LondonSchool(num_tasks)
else:
raise NotImplementedError('{} dataset is not supported'.format(dataset))
if architecture == 'mlp':
if 'dynamic' in algorithm:
net = MLPSoftLLDynamic(torch_dataset.features,
size=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
max_components=-1,
init_ordering_mode=init_mode,
device=device,
freeze_encoder=freeze_encoder)
elif 'nocomponents' in algorithm:
net = MLP(torch_dataset.features,
size=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
device=device,
freeze_encoder=freeze_encoder)
else:
net = MLPSoftLL(torch_dataset.features,
size=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
init_ordering_mode=init_mode,
device=device,
freeze_encoder=freeze_encoder)
elif architecture == 'mlp_gated':
if 'dynamic' in algorithm:
net = MLPSoftGatedLLDynamic(torch_dataset.features,
size=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
max_components=-1,
init_ordering_mode=init_mode,
device=device,
freeze_encoder=freeze_encoder)
else:
net = MLPSoftGatedLL(torch_dataset.features,
size=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
init_ordering_mode=init_mode,
device=device,
freeze_encoder=freeze_encoder)
elif architecture == 'linear':
if 'nocomponents' in algorithm:
net = Linear(torch_dataset.features,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
regression=dataset == 'LondonSchool',
device=device)
else:
net = LinearFactored(torch_dataset.features,
depth=num_layers,
num_tasks=num_tasks,
num_init_tasks=num_init_tasks,
init_ordering_mode=init_mode,
regression=dataset == 'LondonSchool',
device=device)
# Ignore the batch_size in the arguments (batch learning)
batch_size = torch_dataset.max_batch_size
elif architecture == 'cnn':
if 'dynamic' in algorithm:
net = CNNSoftLLDynamic(torch_dataset.features,
channels=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
conv_kernel=3,
maxpool_kernel=2,
padding=padding,
num_init_tasks=num_init_tasks,
max_components=-1,
init_ordering_mode=init_mode,
device=device)
elif 'nocomponents' in algorithm:
net = CNN(torch_dataset.features,
channels=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
conv_kernel=3,
maxpool_kernel=2,
padding=padding,
num_init_tasks=num_init_tasks,
device=device)
else:
net = CNNSoftLL(torch_dataset.features,
channels=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
conv_kernel=3,
maxpool_kernel=2,
padding=padding,
num_init_tasks=num_init_tasks,
init_ordering_mode=init_mode,
device=device)
elif architecture == 'cnn_gated':
if 'dynamic' in algorithm:
net = CNNSoftGatedLLDynamic(torch_dataset.features,
channels=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
conv_kernel=3,
maxpool_kernel=2,
padding=padding,
num_init_tasks=num_init_tasks,
max_components=-1,
init_ordering_mode=init_mode,
device=device)
else:
net = CNNSoftGatedLL(torch_dataset.features,
channels=layer_size,
depth=num_layers,
num_classes=torch_dataset.num_classes,
num_tasks=num_tasks,
conv_kernel=3,
maxpool_kernel=2,
padding=padding,
num_init_tasks=num_init_tasks,
init_ordering_mode=init_mode,
device=device)
else:
raise NotImplementedError('{} architecture is not supported'.format(architecture))
net.train() # training mode
kwargs = {}
if num_train == - 1:
results_dir=os.path.join(results_root, dataset, algorithm, 'seed_{}'.format(seed))
else:
results_dir=os.path.join(results_root, dataset, algorithm, 'seed_{}'.format(seed), 'numtrain_{}'.format(num_train))
if algorithm == 'er_compositional':
if replay_size == -1:
replay_size = batch_size
agent = CompositionalER(net, replay_size, results_dir=results_dir)
elif algorithm == 'ewc_compositional':
agent = CompositionalEWC(net, ewc_lambda, results_dir=results_dir)
elif algorithm == 'van_compositional':
agent = CompositionalVAN(net, results_dir=results_dir)
elif algorithm == 'fm_compositional':
agent = CompositionalFM(net, results_dir=results_dir)
elif algorithm == 'er_joint':
if replay_size == -1:
replay_size = batch_size
agent = JointER(net, replay_size, results_dir=results_dir)
elif algorithm == 'ewc_joint':
agent = JointEWC(net, ewc_lambda, results_dir=results_dir)
elif algorithm == 'van_joint':
agent = JointVAN(net, results_dir=results_dir)
elif algorithm == 'er_nocomponents':
if replay_size == -1:
replay_size = batch_size
agent = NoComponentsER(net, replay_size, results_dir=results_dir)
elif algorithm == 'ewc_nocomponents':
agent = NoComponentsEWC(net, ewc_lambda, results_dir=results_dir)
elif algorithm == 'van_nocomponents':
agent = NoComponentsVAN(net, results_dir=results_dir)
elif algorithm == 'er_dynamic':
if replay_size == -1:
replay_size = batch_size
agent = CompositionalDynamicER(net, replay_size, results_dir=results_dir)
elif algorithm == 'ewc_dynamic':
agent = CompositionalDynamicEWC(net, ewc_lambda, results_dir=results_dir)
elif algorithm == 'van_dynamic':
agent = CompositionalDynamicVAN(net, results_dir=results_dir)
elif algorithm == 'fm_dynamic':
agent = CompositionalDynamicFM(net, results_dir=results_dir)
else:
raise NotImplementedError('{} algorithm is not supported'.format(algorithm))
for task_id, trainset in enumerate(torch_dataset.trainset):
trainloader = (
torch.utils.data.DataLoader(trainset,
batch_size=batch_size,
shuffle=True,
num_workers=0,
pin_memory=True,
))
testloaders = {task: torch.utils.data.DataLoader(testset,
batch_size=torch_dataset.max_batch_size,
shuffle=False,
num_workers=0,
pin_memory=True,
) for task, testset in enumerate(torch_dataset.testset[:(task_id+1)])}
if 'dynamic' in algorithm:
valloader = torch.utils.data.DataLoader(torch_dataset.valset[task_id],
batch_size=torch_dataset.max_batch_size,
shuffle=False,
num_workers=0,
pin_memory=True,
)
kwargs = {'valloader': valloader}
agent.train(trainloader,
task_id,
component_update_freq=component_update_frequency,
num_epochs=num_epochs,
testloaders=testloaders,
save_freq=save_frequency,
**kwargs)
if __name__ == '__main__':
torch.set_num_threads(1)
parser = argparse.ArgumentParser(description='Experimental evaluation of lifelong compositional learning')
parser.add_argument('-T', '--num_tasks', dest='num_tasks', default=10, type=int)
parser.add_argument('-d', '--dataset', dest='dataset', default='MNIST',
choices=['MNIST', 'Fashion', 'CIFAR', 'CUB', 'Omniglot',
'Landmine', 'FacialRecognition', 'LondonSchool'])
parser.add_argument('-e', '--num_epochs', dest='num_epochs', default=100, type=int)
parser.add_argument('-b', '--batch_size', dest='batch_size', default=64, type=int)
parser.add_argument('-f', '--update_frequency', dest='component_update_frequency', default=100, type=int)
parser.add_argument('--lambda', dest='ewc_lambda', default=1e-5, type=float)
parser.add_argument('--replay', dest='replay_size', default=-1, type=int)
parser.add_argument('-s', '--layer_size', dest='layer_size', default=64, type=int)
parser.add_argument('-l', '--num_layers', dest='num_layers', default=4, type=int)
parser.add_argument('-k', '--init_tasks', dest='num_init_tasks', default=4, type=int)
parser.add_argument('-i', '--init_mode', dest='init_mode', default='random_onehot', choices=['random_onehot', 'one_module_per_task', 'random'])
parser.add_argument('-arc', '--architecture', dest='arch', default='mlp',
choices=['mlp', 'cnn', 'mlp_gated', 'cnn_gated', 'linear'])
parser.add_argument('-alg', '--algorithm', dest='algo', default='er_compositional',
choices=['er_compositional', 'ewc_compositional', 'van_compositional',
'er_joint', 'ewc_joint', 'van_joint',
'er_nocomponents', 'ewc_nocomponents', 'van_nocomponents',
'er_dynamic', 'ewc_dynamic', 'van_dynamic',
'fm_compositional', 'fm_dynamic'])
parser.add_argument('-n', '--num_seeds', dest='num_seeds', default=1, type=int)
parser.add_argument('-r', '--results_root', dest='results_root', default='./tmp/results')
parser.add_argument('-sf', '--save_frequency', dest='save_frequency', default=1, type=int)
parser.add_argument('--initial_seed', dest='initial_seed', default=0, type=int)
parser.add_argument('--num_train', dest='num_train', default=-1, type=int)
args = parser.parse_args()
print('Will train on {} tasks from the {} dataset for {} epochs.'.format(args.num_tasks, args.dataset, args.num_epochs))
print('The batch size will be {} and the modules will be updated every {} iterations'.format(args.batch_size, args.component_update_frequency))
print('The network will contain {} layers of size {}'.format(args.num_layers, args.layer_size))
print('The first {} tasks will be used to initialize the modules in mode {}'.format(args.num_init_tasks, args.init_mode))
print('Experiments will be repeated for {} random seeds, starting at {}'.format(args.num_seeds, args.initial_seed))
print('Results will be stored in {}'.format(os.path.join(args.results_root, args.dataset, args.algo)))
main(num_tasks=args.num_tasks,
dataset=args.dataset,
num_epochs=args.num_epochs,
batch_size=args.batch_size,
component_update_frequency=args.component_update_frequency,
ewc_lambda=args.ewc_lambda,
replay_size=args.replay_size,
layer_size=args.layer_size,
num_layers=args.num_layers,
num_init_tasks=args.num_init_tasks,
init_mode=args.init_mode,
architecture=args.arch,
algorithm=args.algo,
num_seeds=args.num_seeds,
results_root=args.results_root,
save_frequency=args.save_frequency,
initial_seed=args.initial_seed,
num_train=args.num_train)