-
Notifications
You must be signed in to change notification settings - Fork 9
/
den.py
318 lines (219 loc) · 8.63 KB
/
den.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
from __future__ import print_function
import os
import random
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim as optim
from models import FeedForward
from utils import *
# PATHS
CHECKPOINT = "./checkpoints/mnist-den"
# BATCH
BATCH_SIZE = 256
NUM_WORKERS = 4
# SGD
LEARNING_RATE = 0.01
MOMENTUM = 0.9
WEIGHT_DECAY = 0
# Step Decay
LR_DROP = 0.5
EPOCHS_DROP = 20
# MISC
MAX_EPOCHS = 200
CUDA = True
# L1 REGULARIZATION
L1_COEFF = 1e-5
# weight below this value will be considered as zero
ZERO_THRESHOLD = 1e-4
# Manual seed
SEED = 20
random.seed(SEED)
torch.manual_seed(SEED)
if CUDA:
torch.cuda.manual_seed_all(SEED)
ALL_CLASSES = range(10)
def main():
if not os.path.isdir(CHECKPOINT):
os.makedirs(CHECKPOINT)
print('==> Preparing dataset')
trainloader, validloader, testloader = load_MNIST(batch_size = BATCH_SIZE, num_workers = NUM_WORKERS)
print("==> Creating model")
model = FeedForward(num_classes=len(ALL_CLASSES))
if CUDA:
model = model.cuda()
model = nn.DataParallel(model)
cudnn.benchmark = True
# initialize parameters
for name, param in model.named_parameters():
if 'bias' in name:
param.data.zero_()
elif 'weight' in name:
param.data.normal_(0,0.005)
print(' Total params: %.2fK' % (sum(p.numel() for p in model.parameters()) / 1000) )
criterion = nn.BCELoss()
CLASSES = []
AUROCs = []
for t, cls in enumerate(ALL_CLASSES):
print('\nTask: [%d | %d]\n' % (t + 1, len(ALL_CLASSES)))
CLASSES.append(cls)
if t == 0:
print("==> Learning")
optimizer = optim.SGD(model.parameters(),
lr=LEARNING_RATE,
momentum=MOMENTUM,
weight_decay=WEIGHT_DECAY
)
penalty = l1_penalty(coeff = L1_COEFF)
best_loss = 1e10
learning_rate = LEARNING_RATE
# epochs = 10
for epoch in range(MAX_EPOCHS):
# decay learning rate
if (epoch + 1) % EPOCHS_DROP == 0:
learning_rate *= LR_DROP
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate
print('Epoch: [%d | %d]' % (epoch + 1, MAX_EPOCHS))
train_loss = train(trainloader, model, criterion, ALL_CLASSES, [cls], optimizer = optimizer, penalty = penalty, use_cuda = CUDA)
test_loss = train(validloader, model, criterion, ALL_CLASSES, [cls], test = True, penalty = penalty, use_cuda = CUDA)
# save model
is_best = test_loss < best_loss
best_loss = min(test_loss, best_loss)
save_checkpoint({'state_dict': model.state_dict()}, CHECKPOINT, is_best)
suma = 0
for p in model.parameters():
p = p.data.cpu().numpy()
suma += (abs(p) < ZERO_THRESHOLD).sum()
print( "Number of zero weights: %d" % (suma) )
else:
# copy model
model_copy = copy.deepcopy(model)
print("==> Selective Retraining")
## Solve Eq.3
# freeze all layers except the last one (last 2 parameters)
params = list(model.parameters())
for param in params[:-2]:
param.requires_grad = False
optimizer = optim.SGD(
filter(lambda p: p.requires_grad, model.parameters()),
lr=LEARNING_RATE,
momentum=MOMENTUM,
weight_decay=WEIGHT_DECAY
)
penalty = l1_penalty(coeff = L1_COEFF)
best_loss = 1e10
learning_rate = LEARNING_RATE
for epoch in range(MAX_EPOCHS):
# decay learning rate
if (epoch + 1) % EPOCHS_DROP == 0:
learning_rate *= LR_DROP
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate
print('Epoch: [%d | %d]' % (epoch + 1, MAX_EPOCHS))
train(trainloader, model, criterion, ALL_CLASSES, [cls], optimizer = optimizer, penalty = penalty, use_cuda = CUDA)
train(validloader, model, criterion, ALL_CLASSES, [cls], test = True, penalty = penalty, use_cuda = CUDA)
for param in model.parameters():
param.requires_grad = True
print("==> Selecting Neurons")
hooks = select_neurons(model, t)
print("==> Training Selected Neurons")
optimizer = optim.SGD(
model.parameters(),
lr=LEARNING_RATE,
momentum=MOMENTUM,
weight_decay=1e-4
)
best_loss = 1e10
learning_rate = LEARNING_RATE
for epoch in range(MAX_EPOCHS):
# decay learning rate
if (epoch + 1) % EPOCHS_DROP == 0:
learning_rate *= LR_DROP
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate
print('Epoch: [%d | %d]' % (epoch + 1, MAX_EPOCHS))
train_loss = train(trainloader, model, criterion, ALL_CLASSES, [cls], optimizer = optimizer, use_cuda = CUDA)
test_loss = train(validloader, model, criterion, ALL_CLASSES, [cls], test = True, use_cuda = CUDA)
# save model
is_best = test_loss < best_loss
best_loss = min(test_loss, best_loss)
save_checkpoint({'state_dict': model.state_dict()}, CHECKPOINT, is_best)
# remove hooks
for hook in hooks:
hook.remove()
print("==> Splitting Neurons")
split_neurons(model_copy, model)
print("==> Calculating AUROC")
filepath_best = os.path.join(CHECKPOINT, "best.pt")
checkpoint = torch.load(filepath_best)
model.load_state_dict(checkpoint['state_dict'])
auroc = calc_avg_AUROC(model, testloader, ALL_CLASSES, CLASSES, CUDA)
print( 'AUROC: {}'.format(auroc) )
AUROCs.append(auroc)
print( '\nAverage Per-task Performance over number of tasks' )
for i, p in enumerate(AUROCs):
print("%d: %f" % (i+1,p))
class my_hook(object):
def __init__(self, mask1, mask2):
self.mask1 = torch.Tensor(mask1).long().nonzero().view(-1).numpy()
self.mask2 = torch.Tensor(mask2).long().nonzero().view(-1).numpy()
def __call__(self, grad):
grad_clone = grad.clone()
if self.mask1.size:
grad_clone[self.mask1, :] = 0
if self.mask2.size:
grad_clone[:, self.mask2] = 0
return grad_clone
def select_neurons(model, task):
prev_active = [True]*len(ALL_CLASSES)
prev_active[task] = False
layers = []
for name, param in model.named_parameters():
if 'bias' not in name:
layers.append(param)
layers = reversed(layers)
hooks = []
selected = []
for layer in layers:
x_size, y_size = layer.size()
active = [True]*y_size
data = layer.data
for x in range(x_size):
# we skip the weight if connected neuron wasn't selected
if prev_active[x]:
continue
for y in range(y_size):
weight = data[x,y]
# check if weight is active
if (weight > ZERO_THRESHOLD):
# mark connected neuron as active
active[y] = False
h = layer.register_hook(my_hook(prev_active, active))
hooks.append(h)
prev_active = active
selected.append( (y_size - sum(active), y_size) )
for nr, (sel, neurons) in enumerate(reversed(selected)):
print( "layer %d: %d / %d" % (nr+1, sel, neurons) )
return hooks
def split_neurons(old_model, new_model):
old_layers = []
for name, param in old_model.named_parameters():
if 'bias' not in name:
old_layers.append(param)
new_layers = []
for name, param in new_model.named_parameters():
if 'bias' not in name:
new_layers.append(param)
suma = 0
for old_layer, new_layer in zip(old_layers, new_layers):
for data1, data2 in zip(old_layer.data, new_layer.data):
diff = data1 - data2
drift = diff.norm(2)
if( drift > 0.02 ):
suma += 1
print( "Number of neurons to split: %d" % (suma) )
if __name__ == '__main__':
main()