-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·305 lines (236 loc) · 14.5 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
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
import matplotlib as mpl
mpl.use('Agg')
import argparse
import numpy as np
import matplotlib.pyplot as plt
import torch
import random
import trainer
import pickle
import os
from utils.utils import get_benchmark_data_loader, test_error, train_error
from algos import ogd,ewc,gem,sogd
from tqdm.auto import tqdm
import time
if __name__ == '__main__':
#if True:
parser = argparse.ArgumentParser()
### Algo parameters
parser.add_argument("--seed", default=1, type=int) # Sets Gym, PyTorch and Numpy seeds
parser.add_argument("--val_size", default=256, type=int)
parser.add_argument("--nepoch", default=30, type=int) # Number of epoches
parser.add_argument("--batch_size", default=10, type=int) # Batch size
parser.add_argument("--memory_size", default=480, type=int) # size of the memory 302sogd vs 122ogd
parser.add_argument("--hidden_dim", default=100, type=int) # size of the hidden layer
parser.add_argument('--lr',default=1e-3, type=float)
parser.add_argument('--n_tasks',default=20, type=int) # Sets number of tasks to run, standard is 10
parser.add_argument('--workers',default=0, type=int) # original was 2, error when more than 0
parser.add_argument('--eval_freq',default=1000, type=int)
parser.add_argument('--compute_singular_values', default=False, type=bool) # rotated, permuted, split_mnist
## Methods parameters
parser.add_argument("--all_features",default=0, type=int) # Leave it to 0, this is for the case when using Lenet, projecting orthogonally only against the linear layers seems to work better
## Dataset
parser.add_argument('--dataset_root_path',default="~/PycharmProjects/ContinuumLearning/datasets", type=str,help="path to your dataset ex: /home/usr/datasets/")
parser.add_argument('--subset_size',default=1000, type=int, help="number of samples per class, ex: for MNIST, \
subset_size=1000 wil results in a dataset of total size 10,000") # default 1000, change to 10,000 for split_mnist
parser.add_argument('--dataset',default="rotated", type=str) #rotated, permuted, split_mnist, split_cifar
parser.add_argument('--rotate_step',default=5, type=int)
parser.add_argument("--is_split", action="store_true")
parser.add_argument('--first_split_size',default=2, type=int)
parser.add_argument('--other_split_size',default=2, type=int)
parser.add_argument("--rand_split",default=False, action="store_true")
parser.add_argument('--force_out_dim', type=int, default=0,
help="Set 0 to let the task decide the required output dimension", required=False)
## Method
parser.add_argument('--method',default="ogd", type=str,help="sgd,ogd,pca,agem,gem-nt,sogd")
## SOGD
parser.add_argument("--sketch_per_task", default=480, # 480
type=int) # Number of sketched/compressed vectors per task, only for sogd
parser.add_argument("--sketch_method", default="basic",
type=str) # Changes the type of sketching. SketchOGD-1: basic, SketchOGD-2: lowrankapprox, SketchOGD-3: lowranksym
## PCA-OGD
parser.add_argument('--pca_sample',default=480, type=int)
## agem
parser.add_argument("--agem_mem_batch_size", default=256, type=int) # size of the memory
parser.add_argument('--margin_gem',default=0.5, type=float)
## EWC
parser.add_argument('--ewc_reg',default=10, type=float)
parser.add_argument('--fisher_sample',default=1024, type=int)
## Folder / Logging results
parser.add_argument('--save_name',default="result", type=str, help="name of the file")
config = parser.parse_args()
config.device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
np.set_printoptions(suppress=True)
config_dict=vars(config)
### setting seeds
torch.manual_seed(config.seed)
np.random.seed(config.seed)
random.seed(config.seed)
torch.backends.cudnn.benchmark=True
torch.backends.cudnn.enabled=True
config.folder="method_{}_dataset_{}_memory_size_{}_bs_{}_lr_{}_epochs_per_task_{}".format(config.method,
config.dataset,config.memory_size,config.batch_size, config.lr,config.nepoch)
if config.method=='sogd':
config.folder = "method_{}_dataset_{}_memory_size_{}_bs_{}_lr_{}_epochs_per_task_{}_sketched_{}_sketchmethod_{}".format(config.method,
config.dataset,
config.memory_size,
config.batch_size,
config.lr,
config.nepoch,
config.sketch_per_task,
config.sketch_method,)
if config.method=='pca':
config.folder = "method_{}_dataset_{}_memory_size_{}_bs_{}_lr_{}_epochs_per_task_{}_compressed_{}".format(config.method,
config.dataset,
config.memory_size,
config.batch_size,
config.lr,
config.nepoch,
config.pca_sample)
if config.method=='agem':
config.folder = "method_{}_dataset_{}_memory_size_{}_bs_{}_lr_{}_epochs_per_task_{}_averaged_{}".format(config.method,
config.dataset,
config.memory_size,
config.batch_size,
config.lr,
config.nepoch,
config.agem_mem_batch_size)
config.folder = config.folder + "_tasks_{}".format(config.n_tasks)
if config.dataset == "rotated":
config.folder = config.folder + "_angle_{}".format(config.rotate_step)
if config.subset_size!= 1000:
config.folder = config.folder + "_subset_size"
## create folder to log results
if not os.path.exists("results/"+config.folder):
os.makedirs("results/"+config.folder, exist_ok=True)
print("Starting test for: ", config.folder)
### name of the file
config.save_name=config.save_name+'_seed_'+str(config.seed)
config.save_name2='singular_values'+'_seed_'+str(config.seed)
### dataset path
# config.dataset_root_path="..."
########################################################################################
### dataset ############################################################################
print('loading dataset')
train_dataset_splits,val_loaders,task_output_space=get_benchmark_data_loader(config)
config.out_dim = {'All': config.force_out_dim} if config.force_out_dim > 0 else task_output_space
### loading trainer module
trainer=trainer.Trainer(config,val_loaders)
start_time = time.time()
t=0
########################################################################################
### start training #####################################################################
first_task_inputs = []
first_task_targets = []
for task_in in range(config.n_tasks):
rr=0
train_loader = torch.utils.data.DataLoader(train_dataset_splits[str(task_in+1)],
batch_size=config.batch_size,
shuffle=True,
num_workers=config.workers)
### train for EPOCH times
print("================== TASK {} / {} =================".format(task_in+1, config.n_tasks))
#for epoch in tqdm(range(config.nepoch), desc="Train task"):
for epoch in range(config.nepoch):
#trainer.ogd_basis.to(trainer.config.device)
#print("starting epoch: ", epoch)
for i, (input, target, task) in enumerate(train_loader):
trainer.task_id = int(task[0])
t+=1
rr+=1
inputs = input.to(trainer.config.device)
target = target.long().to(trainer.config.device)
out = trainer.forward(inputs,task).to(trainer.config.device)
loss = trainer.criterion(out, target)
if config.method=="ewc" and (task_in)>0:
loss+=config.ewc_reg*ewc.penalty(trainer)
loss.backward()
trainer.optimizer_step(first_task=(task_in==0))
### validation accuracy
if (rr-1)%trainer.config.eval_freq==0:
for element in range(task_in+1):
trainer.acc[element]['test_acc'].append(test_error(trainer,element))
trainer.acc[element]['training_steps'].append(t)
#print(" train score: ", train_error(trainer,train_loader, task_in))
#print(" test score: ", test_error(trainer,task_in))
print(" t (epochs_so_far*step_per_epoch: ", t)
for element in range(task_in+1):
trainer.acc[element]['test_acc'].append(test_error(trainer,element))
trainer.acc[element]['training_steps'].append(t)
print(" task {} / accuracy: {} ".format(element+1, trainer.acc[element]['test_acc'][-1]))
## update memory at the end of each tasks depending on the method
if config.method in ['ogd','pca']:
trainer.ogd_basis.to(trainer.config.device)
ogd.update_mem(trainer,train_loader,task_in+1)
if config.method=='sogd' or (config.method in ['ogd','pca'] and config.compute_singular_values): # update sketch memory
sogd.update_mem(trainer,train_loader, method=config.sketch_method)
if config.method=="agem":
gem.update_agem_memory(trainer,train_loader,task_in+1)
if config.method=="gem-nt": ## GEM-NT
gem.update_gem_no_transfer_memory(trainer,train_loader,task_in+1)
if config.method=="ewc":
ewc.update_means(trainer)
ewc.consolidate(trainer,ewc._diag_fisher(trainer,train_loader))
#U, D, V = torch.linalg.svd(trainer.unchanged_gradients)
#print("SINGULAR VALUES: ", D)
if task_in == 0 and config.compute_singular_values:
G = trainer.unchanged_gradients.to(trainer.config.device)
print("G shape: ", G.shape)
# random_ogd_indices = torch.randperm(G.shape[0])[:int(G.shape[1]/4)]
# G_rogd = torch.linalg.qr(G[:,:120])[0]
# dif_G_rogd = G - ogd.project_vec(G,
# proj_basis=G_rogd)
#
# rogd_norm = torch.linalg.matrix_norm(dif_G_rogd)
# print("rogd_norm shape: ",rogd_norm.shape)
# print("rogd try .item(): ", rogd_norm.item())
# #print("||G - G_rogd||^2: ", rogd_norm)
G.to(trainer.config.device)
# _, _, v1 = torch.pca_lowrank(G[:,:480].T.cpu(), q=120, center=True, niter=2)
#
# G_pca = ogd.project_vec(G,
# proj_basis=v1.to(trainer.config.device))
# print("480->120 pca ||G - G_pca||^2: ", torch.linalg.matrix_norm(G - G_pca))
#
# _, _, v2 = torch.pca_lowrank(G[:, :200].T.cpu(), q=100, center=True, niter=2)
#
# G_pca2 = ogd.project_vec(G,
# proj_basis=v2.to(trainer.config.device))
# print("200->100 pca ||G - G_pca||^2: ", torch.linalg.matrix_norm(G - G_pca2))
_, _, v3 = torch.pca_lowrank(G[:, :1100].T.cpu(), q=100, center=True, niter=2)
G_pca3 = ogd.project_vec(G,
proj_basis=v3.to(trainer.config.device))
print("1100->100 pca ||G - G_pca||^2: ", torch.linalg.matrix_norm(G - G_pca3))
G_sogd = sogd.project_vec(G,
proj_basis=trainer.sketch_basis.to(trainer.config.device))
print("||G - G_sogd||^2: ", torch.linalg.matrix_norm(G - G_sogd))
end_time = time.time()
time_spent = end_time - start_time
### Plotting accuracies
print('plotting accuracies')
plt.close('all')
for tasks_id in range(len(trainer.acc.items())):
plt.plot(trainer.acc[tasks_id]['training_steps'],trainer.acc[tasks_id]['test_acc'])
plt.xlabel("Trained per Class")
plt.ylabel("Accuracy")
title_string = str(config.method) + ", "+str(config.dataset)+", "+str(config.n_tasks)+" Tasks"
plt.title(title_string)
plt.grid()
plt.savefig('results/'+config.folder+'/'+config.save_name+".png",dpi=72)
print('Saving results')
print(str(config.folder))
output = open('results/'+config.folder+'/'+config.save_name+'.p', 'wb')
pickle.dump(trainer.acc, output)
output.close()
# # save singular values
# U, D, V = torch.linalg.svd(trainer.unchanged_gradients)
# print("SINGULAR VALUES: ", D)
# svd_output = open('results/'+config.folder+'/'+config.save_name2+'.p', 'wb')
# pickle.dump(D, svd_output)
# svd_output.close()
time_output = open('results/'+config.folder+'/'+'time_spent'+'.p', 'wb')
pickle.dump(time_spent, time_output)
time_output.close()
print('time spent: ', time_spent)
for element in range(config.n_tasks):
print(" task {} / accuracy: {} ".format(element + 1, trainer.acc[element]['test_acc'][-1]))