-
Notifications
You must be signed in to change notification settings - Fork 0
/
heads.py
701 lines (548 loc) · 21.6 KB
/
heads.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
import numpy as np
import torch
import time
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import TensorDataset, DataLoader
from sklearn import svm
from sklearn import mixture
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import Ridge, LogisticRegression
###########################################################
# #
# Class protypes and definitions for classification heads #
# #
###########################################################
class ClassificationHead:
"""
Abstract class for classification heads.
"""
def __init__(
self,
n_way,
feat_dim,
seed=42,
):
"""
Instanciate a classification head model. Classification heads are used in the Meta-training loop to calculate the logits for the query set.
This is meant to be an abstract class, and should not be instanciated directly.
Classification heads should be compatible with BioMetaOptNet, and should implement the following methods:
- get_logits(self, query_features): Get the logits for the query set.
- fit(self, support_features, support_labels): Fit the support set to the support labels.
- test(self, X_test, y_test): Test the performance of the classifier.
n_way (int): Number of classes in the classification task.
feat_dim (int): Dimension of the feature vectors.
seed (int, optional): Random seed. Defaults to 42.
"""
super(ClassificationHead, self).__init__()
self.n_way = n_way
self.feat_dim = feat_dim
self.seed = seed
self.fit_time = None
self.test_time = None
def get_logits(self, query_features):
"""
Performs a pass through the head model to get logits for the query features.
Args:
query_features (tensor): tensor of shape (n_way * n_query, feat_dim)
Returns:
logits (tensor): tensor of shape (n_way * n_query, n_way)
"""
pass
def fit(self, support_features, support_labels):
"""
Complete training routine for the head model.
It should fit the support features to the support labels.
Args:
support_features (tensor): Tensor of shape (n_way * n_support, feat_dim)
support_labels (_type_): Tensor of shape (n_way * n_support)
"""
pass
def test(self, X_test, y_test):
"""
Test the performance of the classifier.
It should return a tuple (loss, accuracy,).
Args:
X_test (tensor): Input tensor of shape (n_way * n_query, feat_dim)
y_test (tensor): Labels tensor of shape (n_way * n_query)
"""
pass
class TorchClassificationHead(ClassificationHead, nn.Module):
"""
Torch classification head used mostly for Neural Network classifiers.
It inherits from both ClassificationHead and nn.Module.
"""
def __init__(self, n_way, feat_dim, seed=42, batch_size=32, epochs=5):
"""
Instanciate a Torch classification head model.
This is meant to be an abstract class, and should not be instanciated directly.
Torch models should be compatible with BioMetaOptNet, and should implement the following methods:
- forward(self, x): Forward pass through the model.
- train_model(self, support_features, support_labels): Complete training routine for the head model.
- test(self, X_test, y_test): Test the performance of the classifier.
- methods inherited from ClassificationHead
Additional args:
- batch_size (int, optional): Batch size for training. Defaults to 32.
- epochs (int, optional): Number of epochs for training. Defaults to 2.
- device (str, optional): Device to use for training. Defaults to "cpu".
"""
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.batch_size = batch_size
self.epochs = epochs
self.final_train_loss = None
self.final_train_acc = None
self.criterion = None
self.optimizer = None
def forward(self, x):
"""
Forward pass through the model.
Args:
x (tensor): Tensor of shape (n_way * n_{query,support}, feat_dim)
"""
pass
def train_model(self, support_features, support_labels):
"""
Complete training routine for the head model.
Args:
support_features (tensor): Tensor of shape (n_way * n_support, feat_dim)
support_labels (tensor): Tensor of shape (n_way * n_support)
"""
start_time = time.time()
dataset = TensorDataset(support_features, support_labels)
loader = torch.utils.data.DataLoader(
dataset, batch_size=self.batch_size, shuffle=True
)
for epoch in range(self.epochs):
self.train()
for X, y in loader:
if torch.cuda.is_available():
X = X.cuda()
y = y.cuda()
self.optimizer.zero_grad()
logits = self.forward(X)
loss = self.criterion(logits, y)
loss.backward(retain_graph=True)
self.optimizer.step()
t_acc = self.test(support_features, support_labels)
self.final_train_acc = t_acc
#self.final_train_loss = t_loss
self.fit_time = time.time() - start_time
return t_acc
def test(self, X_test, y_test):
dataset = TensorDataset(X_test, y_test)
loader = torch.utils.data.DataLoader(
dataset, batch_size=self.batch_size, shuffle=False
)
self.eval()
total_loss = 0
true_labels = []
predicted_labels = []
with torch.no_grad():
for X, y in loader:
if torch.cuda.is_available():
X = X.cuda()
y = y.cuda()
logits = self.forward(X)
loss = self.criterion(logits, y)
total_loss += loss.item()
pred_class = torch.argmax(logits, dim=1)
predicted_labels += list(pred_class.cpu().detach().numpy())
true_labels += list(y.cpu().detach().numpy())
f_loss = total_loss / len(loader)
f_acc = np.mean(np.array(predicted_labels) == np.array(true_labels))
return f_acc
def get_logits(self, query_features):
return self.forward(query_features)
def fit(self, support_features, support_labels):
self.train_model(support_features, support_labels)
class ClassicClassificationHead(ClassificationHead):
"""
Classic classification head.
"""
def __init__(self, n_way, feat_dim, seed=42):
"""
Initialize a classic classification head model.
This is meant to be an abstract class, and should not be instanciated directly.
Classic models should be compatible with BioMetaOptNet, and most of them are implemented with sklearn.
"""
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = None
def get_logits(self, query_features):
x_test = query_features.cpu().detach().numpy()
probabilities = np.array(self.model.predict_proba(x_test))
# Generate logits from probabilities:
scores_raw = self._get_logit_from_probs(probabilities)
# Transform to trainable tensor:
# scores = torch.from_numpy(scores_raw)
scores = to_torch(x=scores_raw, requires_grad=True)
return scores
def fit(self, support_features, support_labels):
X_train = support_features.cpu().detach().numpy()
y_train = support_labels.cpu().detach().numpy()
start_time = time.time()
self.model.fit(X_train, y_train)
self.fit_time = time.time() - start_time
def test(self, X_test, y_test):
X_test = X_test.cpu().detach().numpy()
y_test = y_test.cpu().detach().numpy()
return self.model.score(X_test, y_test)
def _get_logit_from_probs(self, probabilities):
"""
Get the logits from the probabilities.
Many sklearn models do not return logits, but probabilities.
This method should be used to transform the probabilities into logits.
Given a probability p e (0, 1), the logit is defined as: (Get ready for the show)
logit(p) = log(p / (1 - p))
(https://en.wikipedia.org/wiki/Logit)
Args:
probabilities (np.array): np.array of shape (n_way * size, n_way)
Returns:
np.array: np.array of shape (n_way * size, n_way), representing the logits.
"""
c = 0.00000001
nom = probabilities + c
denom = 1 - probabilities + c
return np.log(nom / denom)
######################################################################
# #
# Classification heads: Classic Algorithms implemented with sklearn #
# #
######################################################################
class SVM_Head(ClassicClassificationHead):
"""
Multi-class Support Vector Machine classification head.
"""
def __init__(
self,
n_way,
feat_dim,
seed=42,
kernel="linear",
C=1,
probability=True,
):
"""
Instanciate a SVM classification head model.
Args:
kernel (str, optional): Which kernel to be used (linear, poly, sigmoid, ...). Defaults to 'linear'.
C (int, optional): L2-Regularization parameter. Defaults to 1.
probability (bool, optional): _description_. Defaults to True.
"""
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = svm.SVC(
kernel=kernel, C=C, probability=probability, random_state=seed
)
def get_logits(self, query_features):
x_test = query_features.cpu().detach().numpy()
scores_raw = self.model.decision_function(x_test)
# Transform to trainable tensor:
scores = to_torch(x=scores_raw, requires_grad=True)
# print(f"Scores shape: {scores.shape}")
# print(scores)
return scores
class RidgeRegression_Head(ClassicClassificationHead):
"""
Ridge-Regression classification head.
"""
def __init__(
self,
n_way,
feat_dim,
seed=42,
alpha=1.0,
fit_intercept=True,
solver="lsqr",
):
"""
Instanciate a Ridge-Regression classification head.
Args:
alpha(float,optional): Whether to fit the intercept for this model. If False X and y are expected to be centered.
it_intercept(bool,optional): Whether to fit the intercept for this model. If False X and y are expected to be centered.
solver (string, optional): Constant that multiplies the L2 term, controlling regularization strength.
"""
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = Ridge(alpha=alpha, fit_intercept=fit_intercept, solver=solver)
def get_logits(self, query_features):
x_test = query_features.cpu().detach().numpy()
scores_raw = self.model.predict(x_test)
# Transform to trainable tensor:
scores = to_torch(x=scores_raw, requires_grad=True)
# print(f"Scores shape: {scores.shape}") -> (samples, 1)
# print(scores)
return scores
class NaiveBayes_Head(ClassicClassificationHead):
"""
Naive Bayes classification head.
"""
def __init__(self, n_way, feat_dim, seed=42):
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = GaussianNB()
class KNN_Head(ClassicClassificationHead):
def __init__(self, n_way, feat_dim, seed=42, n_neighbors=3):
super().__init__(
n_way=n_way,
feat_dim=feat_dim,
seed=seed,
)
self.n_neighbors = n_neighbors
self.model = KNeighborsClassifier(n_neighbors=n_neighbors)
class DecisionTree_Head(ClassicClassificationHead):
"""
Decision Tree classification head.
"""
def __init__(self, n_way, feat_dim, seed=42):
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = DecisionTreeClassifier(random_state=seed)
class RandomForest_Head(ClassicClassificationHead):
def __init__(self, n_way, feat_dim, seed=42, n_estimators=100):
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = RandomForestClassifier(
n_estimators=n_estimators, random_state=seed
)
class GMM_Head(ClassicClassificationHead):
def __init__(
self,
n_way,
feat_dim,
seed=42,
covar_type="full",
init_params="kmeans",
):
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = mixture.GaussianMixture(
n_components=n_way,
covariance_type=covar_type,
init_params=init_params,
random_state=seed,
)
def test(self, X_test, y_test):
X_test = X_test.cpu().detach().numpy()
y_test = y_test.cpu().detach().numpy()
y_test_pred = self.model.predict(X_test)
test_accuracy = np.mean(y_test_pred.ravel() == y_test.ravel())
return test_accuracy
def fit(self, support_features, support_labels):
X_train = support_features.cpu().detach().numpy()
start_time = time.time()
self.model.fit(X_train)
self.fit_time = time.time() - start_time
class LogisticRegression_Head(ClassicClassificationHead):
"""
Multi-class Logistic Regression classification head.
"""
def __init__(
self,
n_way,
feat_dim,
seed=42,
max_iter=100,
C=1.0,
):
super().__init__(n_way=n_way, feat_dim=feat_dim, seed=seed)
self.model = LogisticRegression(random_state=seed, max_iter=max_iter, C=C)
##########################################
# #
# Classification heads: Neural Networks #
# #
##########################################
class MLP_Head(TorchClassificationHead):
"""
Multi-class Neural Network classification head.
"""
def __init__(
self,
n_way,
feat_dim,
seed=42,
batch_size=32,
epochs=5,
hidden_dims=[],
activations=None,
dropouts=None,
lr=0.001,
weight_decay=0.00001,
):
super().__init__(
n_way=n_way,
feat_dim=feat_dim,
seed=seed,
batch_size=batch_size,
epochs=epochs,
)
self.hidden_dims = hidden_dims
self.output_dim = n_way
self.lr = lr
self.weight_decay = weight_decay
self.network = nn.Sequential()
curr_size = feat_dim
for i, hidden_dim in enumerate(hidden_dims):
self.network.append(nn.Linear(curr_size, hidden_dim))
if activations is not None and activations[i] is not None:
if type(activations[i]) == str:
if activations[i] == "relu":
self.network.append(nn.ReLU())
elif activations[i] == "sigmoid":
self.network.append(nn.Sigmoid())
elif activations[i] == "tanh":
self.network.append(nn.Tanh())
else:
self.network.append(activations[i])
if dropouts is not None and dropouts[i] is not None:
self.network.append(nn.Dropout(dropouts[i]))
curr_size = hidden_dim
self.network.append(nn.Linear(curr_size, self.output_dim))
if torch.cuda.is_available():
self.to("cuda")
self.criterion = nn.CrossEntropyLoss()
self.optimizer = torch.optim.Adam(
self.parameters(), lr=self.lr, weight_decay=self.weight_decay
)
def forward(self, x):
return self.network(x)
class LogisticRegressionTorch_Head(TorchClassificationHead):
def __init__(
self,
n_way,
feat_dim,
seed=42,
batch_size=32,
epochs=5,
lr=0.001,
):
super().__init__(
n_way=n_way,
feat_dim=feat_dim,
seed=seed,
batch_size=batch_size,
epochs=epochs,
)
self.lr = lr
self.model = nn.Linear(feat_dim, n_way)
if torch.cuda.is_available():
self.to("cuda")
self.criterion = nn.CrossEntropyLoss()
self.optimizer = torch.optim.SGD(self.parameters(), lr=self.lr)
def forward(self, x):
return torch.sigmoid(self.model(x))
##############################
# #
# Dispatcher and other utils #
# #
##############################
# Note: When adding a new model, be sure to update the dispatcher.
DISPATCHER = {
"MLP": MLP_Head,
"LogisticRegression": LogisticRegression_Head,
"SVM": SVM_Head,
"NaiveBayes": NaiveBayes_Head,
"KNN": KNN_Head,
"DecisionTree": DecisionTree_Head,
"RandomForest": RandomForest_Head,
"GMM": GMM_Head,
"RR": RidgeRegression_Head,
}
def to_torch(x, requires_grad=True):
"""
Transform a numpy array to a torch tensor.
Args:
x (np.array): Numpy array to transform.
Returns:
Tensor: Pytorch tensor.
"""
ten = torch.from_numpy(x)
if torch.cuda.is_available():
ten = ten.cuda()
if requires_grad:
ten.requires_grad = True
# Variable(ten.cuda()) # deprecated
return ten
##########################################
# #
# Classification heads: Neural Networks #
# #
##########################################
if __name__ == "__main__":
print("==== Generating random data =====")
n_way = 5
n_query = 10
n_support = 5
emb_dim = 512
z_support = torch.rand(n_way * n_support, emb_dim)
print(f"z_support shape: ", z_support.shape)
z_query = torch.rand(n_way * n_query, emb_dim)
print(f"z_query shape: ", z_query.shape)
z_labels = torch.from_numpy(np.repeat(range(n_way), n_support))
print(f"z_labels shape: ", z_labels.shape)
z_query_labels = torch.from_numpy(np.repeat(range(n_way), n_query))
print(f"z_query_labels shape: ", z_query_labels.shape)
print("\n==== Testing heads =====")
# MLP Head
head = MLP_Head(
n_way=n_way,
feat_dim=emb_dim,
seed=42,
batch_size=32,
epochs=500,
hidden_dims=[512, 256, 64, 32],
activations=[nn.ReLU(), nn.ReLU(), nn.ReLU(), nn.ReLU()],
dropouts=[0.4, 0.4, 0.4, 0.4],
lr=0.001,
weight_decay=0.00001,
)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for MLP logits."
print(f"MLP test: {head.test(z_query, z_query_labels)}")
print(">>MLP Ok!\n")
# Logistic Regression Head
head = LogisticRegression_Head(n_way=n_way, feat_dim=emb_dim, seed=42)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for LR logits."
print(f"LR test: {head.test(z_query, z_query_labels)}")
print(">>LR Ok!\n")
# SVM Head
head = SVM_Head(n_way=n_way, feat_dim=emb_dim, seed=42)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for SVM logits."
print(f"SVM test: {head.test(z_query, z_query_labels)}")
print(">>SVM Ok!\n")
# Decision Tree Head
head = DecisionTree_Head(n_way=n_way, feat_dim=emb_dim, seed=42)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for DecTree logits."
print(f"DecTree test: {head.test(z_query, z_query_labels)}")
print(">>DecTree Ok!\n")
# Naive Bayes Head
head = NaiveBayes_Head(n_way=n_way, feat_dim=emb_dim, seed=42)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for NB logits."
print(f"NB test: {head.test(z_query, z_query_labels)}")
print(">>Naive Bayes Ok!\n")
# KNN
head = KNN_Head(n_way=n_way, feat_dim=emb_dim, seed=42, n_neighbors=3)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for KNN logits."
print(f"KNN test: {head.test(z_query, z_query_labels)}")
print(">>KNN Ok!\n")
# Random Forest
head = RandomForest_Head(n_way=n_way, feat_dim=emb_dim, seed=42, n_estimators=100)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for RF logits."
print(f"RF test: {head.test(z_query, z_query_labels)}")
print(">>RF Ok!\n")
# GMM
head = GMM_Head(n_way=n_way, feat_dim=emb_dim, seed=42)
head.fit(z_support, z_labels)
logits = head.get_logits(z_query)
assert logits.shape == (n_way * n_query, n_way), "Wrong shape for GMM logits."
print(f"GMM test: {head.test(z_query, z_query_labels)}")
print(">>GMM Ok!")