-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
383 lines (321 loc) · 14.5 KB
/
models.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import torch
import pytorch_lightning as pl
from torchmetrics.functional import accuracy
class ShallowNeuralNet(pl.LightningModule):
r"""Shallow Neural Network model with Pytorch Lightning
This class implements a single layer neural network which
predicts a classes from feature vectors.
Parameters
----------
n_features : int
Number of features in the input of the network.
n_classes : int
Number of classes in the output of the network.
learning_rate : float, optional (default=None)
Learning rate for the classifier part of the network.
If it is None, uses 10 * learning_rate_encoder.
loss_fn : function, optional (default=None)
Function taking as arguments the network predictions
and the ground-truths, and returns a differentiable
loss. If None, uses torch.nn.CrossEntropyLoss().
l2_penalty : float, optional (default=0.0)
If positive, adds l2_penalty to the network weights.
weight_decay : float, optional (default=0.0)
If positive, adds weight decay to network weights.
momentum : float, optional (default=0.9)
Only used if optimizer is SGD. Momentum term in the optimizer.
log_gradients : bool, optional (default=False)
If True, logs histograms of network gradients. NOTE: the
generated tensorboard logs may be heavy if this parameter
is set to true.
optimizer_name : str, optional (default='adam')
Either 'adam' or 'sgd'. Chooses which optimization strategy
the network will adopt.
max_norm : float, optional (default=None)
If given, constrains the network weights to have maximum norm
equal to the given value.
"""
def __init__(self,
n_features,
n_classes,
learning_rate=1e-4,
loss_fn=None,
l2_penalty=0.0,
momentum=0.9,
optimizer_name='adam',
log_gradients=False,
max_norm=None):
super(ShallowNeuralNet, self).__init__()
self.main = torch.nn.Linear(n_features, n_classes)
self.learning_rate = learning_rate
if loss_fn is None:
self.loss_fn = torch.nn.CrossEntropyLoss()
else:
self.loss_fn = loss_fn
self.l2_penalty = l2_penalty
self.momentum = momentum
self.history = {'loss': [], 'acc': [], 'val_loss': [], 'val_acc': []}
self.log_gradients = log_gradients
self.optimizer_name = optimizer_name
self.n_classes = n_classes
self.max_norm = max_norm
self.training_step_outputs = []
self.validation_step_outputs = []
self.test_step_outputs = []
def max_norm_normalization(self, w):
with torch.no_grad():
norm = torch.sqrt(torch.sum(w ** 2, dim=1, keepdim=True))
desired = torch.clamp(norm, 0, self.max_norm)
w *= (desired / (1e-10 + norm))
def custom_histogram_adder(self):
if self.logger is not None:
for name, params in self.named_parameters():
self.logger.experiment.add_histogram(name,
params,
self.current_epoch)
def forward(self, x):
if self.max_norm is not None:
self.max_norm_normalization(self.main.weight)
return self.main(x)
def configure_optimizers(self):
if self.optimizer_name.lower() == 'adam':
return torch.optim.Adam(self.parameters(),
lr=self.learning_rate,
weight_decay=self.l2_penalty)
else:
return torch.optim.SGD(self.parameters(),
lr=self.learning_rate,
weight_decay=self.l2_penalty,
momentum=self.momentum)
def __step(self, batch, batch_idx):
x, y = batch
y_pred = self(x)
L = self.loss_fn(target=y.argmax(dim=1), input=y_pred)
acc = accuracy(preds=y_pred,
target=y.argmax(dim=1),
task='multiclass',
num_classes=self.n_classes,
top_k=1)
return {'loss': L, 'acc': acc}
def training_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.training_step_outputs.append(output)
return output
def validation_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.validation_step_outputs.append(output)
return output
def test_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.test_step_outputs.append(output)
return output
def on_train_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.training_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.training_step_outputs]).mean()
self.training_step_outputs.clear()
self.log('loss', avg_loss)
self.log('accuracy', avg_acc)
self.history['loss'].append(avg_loss)
self.history['acc'].append(avg_acc)
# Logs scalars
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Train",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Train",
avg_acc,
self.current_epoch)
# Logs histograms
if self.log_gradients:
self.custom_histogram_adder()
def on_validation_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.validation_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.validation_step_outputs]).mean()
self.validation_step_outputs.clear()
self.log('val_loss', avg_loss)
self.log('val_accuracy', avg_acc)
self.history['val_loss'].append(avg_loss)
self.history['val_acc'].append(avg_acc)
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Validation",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Validation",
avg_acc,
self.current_epoch)
def on_test_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.test_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.test_step_outputs]).mean()
self.test_step_outputs.clear()
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Test",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Test",
avg_acc,
self.current_epoch)
class WeightedShallowNeuralNet(pl.LightningModule):
r"""Weighted Shallow Neural Network model with Pytorch Lightning
This class implements a single layer neural network which
predicts a classes from feature vectors. Losses can be weighted
by sample importance
Parameters
----------
n_features : int
Number of features in the input of the network.
n_classes : int
Number of classes in the output of the network.
learning_rate : float, optional (default=None)
Learning rate for the classifier part of the network.
If it is None, uses 10 * learning_rate_encoder.
loss_fn : function, optional (default=None)
Function taking as arguments the network predictions
and the ground-truths, and returns a differentiable
loss. If None, uses torch.nn.CrossEntropyLoss().
l2_penalty : float, optional (default=0.0)
If positive, adds l2_penalty to the network weights.
weight_decay : float, optional (default=0.0)
If positive, adds weight decay to network weights.
momentum : float, optional (default=0.9)
Only used if optimizer is SGD. Momentum term in the optimizer.
log_gradients : bool, optional (default=False)
If True, logs histograms of network gradients. NOTE: the
generated tensorboard logs may be heavy if this parameter
is set to true.
optimizer_name : str, optional (default='adam')
Either 'adam' or 'sgd'. Chooses which optimization strategy
the network will adopt.
max_norm : float, optional (default=None)
If given, constrains the network weights to have maximum norm
equal to the given value.
"""
def __init__(self,
n_features,
n_classes,
learning_rate=1e-4,
l2_penalty=0.0,
momentum=0.9,
optimizer_name='adam',
log_gradients=False,
max_norm=None):
super(WeightedShallowNeuralNet, self).__init__()
self.main = torch.nn.Linear(n_features, n_classes)
self.learning_rate = learning_rate
self.loss_fn = torch.nn.CrossEntropyLoss(reduction='none')
self.l2_penalty = l2_penalty
self.momentum = momentum
self.history = {'loss': [], 'acc': [], 'val_loss': [], 'val_acc': []}
self.log_gradients = log_gradients
self.optimizer_name = optimizer_name
self.n_classes = n_classes
self.max_norm = max_norm
self.training_step_outputs = []
self.validation_step_outputs = []
self.test_step_outputs = []
def max_norm_normalization(self, w):
with torch.no_grad():
norm = torch.sqrt(torch.sum(w ** 2, dim=1, keepdim=True))
desired = torch.clamp(norm, 0, self.max_norm)
w *= (desired / (1e-10 + norm))
def custom_histogram_adder(self):
if self.logger is not None:
for name, params in self.named_parameters():
self.logger.experiment.add_histogram(name,
params,
self.current_epoch)
def forward(self, x):
if self.max_norm is not None:
self.max_norm_normalization(self.main.weight)
return self.main(x)
def configure_optimizers(self):
if self.optimizer_name.lower() == 'adam':
return torch.optim.Adam(self.parameters(),
lr=self.learning_rate,
weight_decay=self.l2_penalty)
else:
return torch.optim.SGD(self.parameters(),
lr=self.learning_rate,
weight_decay=self.l2_penalty,
momentum=self.momentum)
def __step(self, batch, batch_idx):
w, x, y = batch
y_pred = self(x)
# NOTE. L is a vector of shape (n,)
Lvec = self.loss_fn(target=y.argmax(dim=1), input=y_pred)
# We need to multiply it by the sample importance vector
L = (Lvec * (w / (w.sum() + 1e-9))).sum()
acc = accuracy(preds=y_pred,
target=y.argmax(dim=1),
task='multiclass',
num_classes=self.n_classes,
top_k=1)
return {'loss': L, 'acc': acc}
def training_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.training_step_outputs.append(output)
return output
def validation_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.validation_step_outputs.append(output)
return output
def test_step(self, batch, batch_idx):
output = self.__step(batch, batch_idx)
self.test_step_outputs.append(output)
return output
def on_train_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.training_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.training_step_outputs]).mean()
self.training_step_outputs.clear()
self.log('loss', avg_loss)
self.log('accuracy', avg_acc)
self.history['loss'].append(avg_loss)
self.history['acc'].append(avg_acc)
# Logs scalars
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Train",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Train",
avg_acc,
self.current_epoch)
# Logs histograms
if self.log_gradients:
self.custom_histogram_adder()
def on_validation_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.validation_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.validation_step_outputs]).mean()
self.validation_step_outputs.clear()
self.log('val_loss', avg_loss)
self.log('val_accuracy', avg_acc)
self.history['val_loss'].append(avg_loss)
self.history['val_acc'].append(avg_acc)
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Validation",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Validation",
avg_acc,
self.current_epoch)
def on_test_epoch_end(self):
avg_loss = torch.tensor([
x['loss'] for x in self.test_step_outputs]).mean()
avg_acc = torch.tensor([
x['acc'] for x in self.test_step_outputs]).mean()
self.test_step_outputs.clear()
if self.logger is not None:
self.logger.experiment.add_scalar("Loss/Test",
avg_loss,
self.current_epoch)
self.logger.experiment.add_scalar("Accuracy/Test",
avg_acc,
self.current_epoch)