-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathengine.py
281 lines (225 loc) · 10.7 KB
/
engine.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
import torch
import torch.nn as nn
import time
def train_BP(model, criterion, optimizer, loader, device, measures):
"""
Train only the traditional blocks with backprop
"""
# with torch.autograd.set_detect_anomaly(True):
t = time.time()
for inputs, target in loader:
## 1. forward propagation$
inputs = inputs.float().to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
output = model(inputs)
# print(r"%s" % (time.time() - t))
## 2. loss calculation
loss = criterion(output, target)
## 3. compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# print(optimizer.param_groups)
## 4. Accuracy assessment
predict = output.data.max(1)[1]
acc = predict.eq(target.data).sum()
# Save if measurement is wanted
# print(model.blocks[1].layer.weight.mean(), model.blocks[1].layer.weight.std())
convergence, R1 = model.convergence()
measures.step(target.shape[0], loss.clone().detach().cpu(), acc.cpu(), convergence, R1, model.get_lr())
return measures, optimizer.param_groups[0]['lr']
def train_hebb(model, loader, device, measures=None, criterion=None):
"""
Train only the hebbian blocks
"""
t = time.time()
loss_acc = (not model.is_hebbian()) and (criterion is not None)
with torch.no_grad():
for inputs, target in loader:
# print(inputs.min(), inputs.max(), inputs.mean(), inputs.std())
## 1. forward propagation
inputs = inputs.float().to(device) # , non_blocking=True)
output = model(inputs)
# print(r"%s"%(time.time()-t))
if loss_acc:
target = target.to(device, non_blocking=True)
## 2. loss calculation
loss = criterion(output, target)
## 3. Accuracy assessment
predict = output.data.max(1)[1]
acc = predict.eq(target.data).sum()
# Save if measurement is wanted
conv, r1 = model.convergence()
measures.step(target.shape[0], loss.clone().detach().cpu(), acc.cpu(), conv, r1, model.get_lr())
model.update()
info = model.radius()
convergence, R1 = model.convergence()
return measures, model.get_lr(), info, convergence, R1
def train_sup_hebb(model, loader, device, measures=None, criterion=None):
"""
Train only the hebbian blocks
"""
t = time.time()
loss_acc = (not model.is_hebbian()) and (criterion is not None)
with torch.no_grad():
for inputs, target in loader:
# print(inputs.min(), inputs.max(), inputs.mean(), inputs.std())
## 1. forward propagation
inputs = inputs.float().to(device)
output = model(inputs)
model.blocks[-1].layer.plasticity(x=model.blocks[-1].layer.forward_store['x'],
pre_x=model.blocks[-1].layer.forward_store['pre_x'],
wta=torch.nn.functional.one_hot(target, num_classes=
model.blocks[-1].layer.forward_store['pre_x'].shape[1]).type(
model.blocks[-1].layer.forward_store['pre_x'].type()))
if loss_acc:
target = target.to(device, non_blocking=True)
## 2. loss calculation
loss = criterion(output, target)
## 3. Accuracy assessment
predict = output.data.max(1)[1]
acc = predict.eq(target.data).sum()
# Save if measurement is wanted
conv, r1 = model.convergence()
measures.step(target.shape[0], loss.clone().detach().cpu(), acc.cpu(), conv, r1, model.get_lr())
model.update()
info = model.radius()
convergence, R1 = model.convergence()
return measures, model.get_lr(), info, convergence, R1
def train_unsup(model, loader, device,
blocks=[]): # fixed bug as optimizer is not used or pass in the only use it has in this repo currently
"""
Unsupervised learning only works with hebbian learning
"""
model.train(blocks=blocks) # set unsup blocks to train mode
_, lr, info, convergence, R1 = train_hebb(model, loader, device)
return lr, info, convergence, R1
def train_sup(model, criterion, optimizer, loader, device, measures, learning_mode, blocks=[]):
"""
train hybrid model.
learning_mode=HB --> train_hebb
learning_mode=BP --> train_BP
"""
if len(blocks) == 1:
model.train(blocks=blocks)
if model.get_block(blocks[0]).is_hebbian():
measures, lr, info, convergence, R1 = train_sup_hebb(model, loader, device, measures, criterion)
else:
measures, lr = train_BP(model, criterion, optimizer, loader, device, measures)
else:
model.train(blocks=blocks)
if learning_mode == 'HB':
measures, lr, info, convergence, R1 = train_sup_hebb(model, loader, device, measures, criterion)
else:
measures, lr = train_BP(model, criterion, optimizer, loader, device, measures)
return measures, lr
def evaluate_unsup(model, train_loader, test_loader, device, blocks):
"""
Unsupervised evaluation, only support MLP architecture
"""
if model.get_block(blocks[-1]).arch == 'MLP':
sub_model = model.sub_model(blocks)
return evaluate_hebb(sub_model, train_loader, test_loader, device)
else:
return 0., 0.
def evaluate_hebb(model, train_loader, test_loader, device):
if train_loader.dataset.split == 'unlabeled':
print('Unalbeled dataset, cant perform unsupervised evaluation')
return 0, 0
preactivations, winner_ids, neuron_labels, targets = infer_dataset(model, train_loader, device)
acc_train = get_accuracy(model, winner_ids, targets, preactivations, neuron_labels, device)
preactivations_test, winner_ids_test, _, targets_test = infer_dataset(model, test_loader, device)
acc_test = get_accuracy(model, winner_ids_test, targets_test, preactivations_test, neuron_labels, device)
return float(acc_train.cpu()), float(acc_test.cpu())
def infer_dataset(model, loader, device):
model.eval()
targets_lst = []
winner_ids = []
preactivations_lst = []
wta_lst = []
with torch.no_grad():
for inputs, targets in loader:
## 1. forward propagation
inputs = inputs[targets != -1]
targets = targets[targets != -1]
if targets.nelement() != 0:
inputs = inputs.float().to(device, non_blocking=True)
preactivations, wta = model.foward_x_wta(inputs)
preactivations_lst.append(preactivations)
wta_lst.append(wta)
targets_lst += targets.tolist()
winner_ids_minibatch = wta.argmax(dim=1)
winner_ids += winner_ids_minibatch.tolist()
winner_ids = torch.FloatTensor(winner_ids).to(torch.int64).to(device)
targets = torch.FloatTensor(targets_lst).to(torch.int64).to(device)
preactivations = torch.cat(preactivations_lst).to(device)
wta = torch.cat(wta_lst).to(device)
neuron_labels = get_neuron_labels(model, winner_ids, targets, preactivations, wta)
return preactivations, winner_ids, neuron_labels, targets
def evaluate_sup(model, criterion, loader, device):
"""
Evaluate the model, returning loss and acc
"""
model.eval()
loss_sum = 0
acc_sum = 0
n_inputs = 0
with torch.no_grad():
for inputs, target in loader:
## 1. forward propagation
inputs = inputs.float().to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
output = model(inputs)
## 2. loss calculation
loss = criterion(output, target)
loss_sum += loss.clone().detach()
## 3. Accuracy assesment
predict = output.data.max(1)[1]
acc = predict.eq(target.data).sum()
acc_sum += acc
n_inputs += target.shape[0]
return loss_sum.cpu() / n_inputs, 100 * acc_sum.cpu() / n_inputs
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
"""Code needs to be rewrite"""
def get_neuron_labels(model, winner_ids, targets, preactivations, wta):
targets_onehot = nn.functional.one_hot(targets, num_classes=preactivations.shape[1]).to(torch.float32)
winner_ids_onehot = nn.functional.one_hot(winner_ids, num_classes=preactivations.shape[1]).to(torch.float32)
responses_matrix = torch.matmul(winner_ids_onehot.t(), targets_onehot)
neuron_outputs_for_label_total = torch.matmul(wta.t(), targets_onehot)
responses_matrix[responses_matrix.sum(dim=1) == 0] = neuron_outputs_for_label_total[
responses_matrix.sum(dim=1) == 0]
neuron_labels = responses_matrix.argmax(1)
return neuron_labels
def get_accuracy(model, winner_ids, targets, preactivations, neuron_labels, device):
n_samples = preactivations.shape[0]
# if not model.ensemble:
predlabels = torch.FloatTensor([neuron_labels[i] for i in winner_ids]).to(device)
'''
else:
if model.test_uses_softmax:
soft_acts = activation(preactivations, model.t_invert, model.activation_fn, dim=1, power=model.power, normalize=True)
winner_ensembles = [
np.argmax([np.sum(np.where(neuron_labels == ensemble, soft_acts[sample], np.asarray(0))) for
ensemble in range(10)]) for sample in range(n_samples)]
else:
winner_ensembles = [
np.argmax([np.sum(np.where(neuron_labels == ensemble, preactivations[sample], np.asarray(0))) for
ensemble in range(10)]) for sample in range(n_samples)]
predlabels = winner_ensembles
'''
correct_pred = predlabels == targets
n_correct = correct_pred.sum()
accuracy = n_correct / len(targets)
return 100 * accuracy.cpu()