-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel.py
More file actions
464 lines (362 loc) · 16 KB
/
model.py
File metadata and controls
464 lines (362 loc) · 16 KB
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
import torch
import torch.nn.functional as F
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, TensorDataset
from utils.cov_matrices_operations import (calc_single_covariance,
matrix_shrinkage,
normalize_covariance_matrix,
tukeys_transformation)
from utils.metric_functions import euclidean, mahalanobis
from utils.other import (get_single_class_examples,
get_smallest_values_per_class)
class FeNeC:
def __init__(self, config, device="cpu"):
"""
Initializes the FeNeC continual learning model.
Args:
config (dict): Configuration dictionary containing model parameters
such as metric type, KNN settings, shrinkage factors,
and whether to use logits or not.
device (str): Device to run the model on ('cpu', 'cuda' or 'mps').
Returns:
None
"""
self.device = device
self.current_task = -1
self.metric = config["metric"]
self.weight = config["weight"]
self.knn_k = config["knn_k"]
self.num_of_shrinkages = config["num_of_shrinkages"]
self.shrinkage_alpha_0 = config["shrinkage_alpha_0"]
self.shrinkage_alpha_1 = config["shrinkage_alpha_1"]
self.norm_in_mahalanobis = config["norm_in_mahalanobis"]
self.use_tukey = config["use_tukey"]
if not self.use_tukey:
self.tukey_lambda = 1
else:
self.tukey_lambda = config["tukey_lambda"]
self.use_kmeans = config["use_kmeans"]
self.kmeans_k = config["kmeans_k"]
self.sklearn_seed = config["sklearn_seed"]
self.X_train = None
self.y_train = None
self.covMatrices = None
self.use_logits = config["use_logits"]
if self.use_logits:
self.train_only_task_0 = config["train_only_task_0"]
self.logits_n_samples = config["logits_n_samples"]
self.logits_train_epochs = config["logits_train_epochs"]
self.logits_batch_size = config["logits_batch_size"]
self.logits_learning_rate = config["logits_learning_rate"]
self.logits_regularization_strength = config[
"logits_regularization_strength"
]
self.logits_patience = config["logits_patience"]
self.parameters = torch.nn.ParameterDict(
{
"a": torch.nn.Parameter(torch.randn(1, device=self.device)),
"b": torch.nn.Parameter(torch.randn(1, device=self.device)),
}
)
def fit(self, X_train, y_train):
"""
Fits the model to the current task's training data.
Args:
X_train (torch.Tensor): Feature tensor of the current task. (num_train_samples, num_features)
y_train (torch.Tensor): Label tensor of the current task. (num_train_samples)
Returns:
None
Notes:
- Computes and stores centroids (optionally using KMeans).
- Applies optional Tukey transformation.
- Calculates and stores class-wise covariance matrices if using Mahalanobis distance.
- Optionally trains a logits-based predictor.
"""
self.current_task += 1
if self.X_train is None or self.y_train is None:
if self.metric == "mahalanobis":
self.covMatrices = (
self._calc_covariances(
tukeys_transformation(X_train, self.tukey_lambda), y_train
)
.float()
.to(self.device)
)
else:
if self.metric == "mahalanobis":
self.covMatrices = (
torch.cat(
(
self.covMatrices,
self._calc_covariances(
tukeys_transformation(X_train, self.tukey_lambda),
y_train,
),
)
)
.float()
.to(self.device)
)
if self.use_kmeans:
uniqes = torch.unique(y_train, sorted=True).to(self.device)
for i in uniqes:
single_class_examples = get_single_class_examples(
X_train.to(self.device), y_train.to(self.device), i, self.device
)
if i == uniqes[0]:
new_X_train = self._kmeans(single_class_examples).to(self.device)
new_y_train = torch.full((self.kmeans_k,), i.item()).to(self.device)
else:
new_X_train = torch.cat(
(
new_X_train,
self._kmeans(single_class_examples).to(self.device),
)
)
new_y_train = torch.cat(
(
new_y_train,
torch.full((self.kmeans_k,), i.item()).to(self.device),
)
)
# Tukey transformation with lambda < 0 can't handle negative values
if self.use_tukey:
print(
"Warning!!! All values smaller than 0 were set to 0, bacuse tukey transformation can't handle negative values!"
)
new_X_train[new_X_train < 0] = 0
X_train_centroids = new_X_train.float()
y_train_centroids = new_y_train
if self.X_train is None or self.y_train is None:
self.X_train = X_train_centroids.to(self.device)
self.y_train = y_train_centroids.to(self.device)
if self.use_logits:
self._train_logits(X_train, y_train)
else:
self.X_train = torch.cat((self.X_train, X_train_centroids.to(self.device)))
self.y_train = torch.cat((self.y_train, y_train_centroids.to(self.device)))
if self.use_logits and not self.train_only_task_0:
self._train_logits(X_train, y_train)
def predict(self, X_test):
"""
Predicts class labels for given test samples.
Args:
X_test (torch.Tensor): Test samples to classify. (num_test_samples, num_features)
Returns:
torch.Tensor: Predicted class labels.
Notes:
- Uses either logits-based prediction or KNN with majority voting depending on configuration.
"""
if self.use_logits:
return self._predict_with_logits(X_test.to(self.device))
return self._predict_with_majority_voting(X_test.to(self.device))
def _predict_with_majority_voting(self, X_test):
if self.metric == "euclidean":
distances = euclidean(self.X_train, X_test, self.device)
elif self.metric == "mahalanobis":
distances = mahalanobis(
self.X_train,
self.y_train,
X_test,
self.covMatrices,
self.tukey_lambda,
self.device,
self.norm_in_mahalanobis,
)
_, knn_indices = torch.topk(
distances, self.knn_k, largest=False, dim=1, sorted=True
)
nearest_neighbours_matrix = self.y_train[knn_indices].squeeze()
if self.knn_k == 1:
return nearest_neighbours_matrix
if len(nearest_neighbours_matrix.size()) < 2:
nearest_neighbours_matrix = nearest_neighbours_matrix.unsqueeze(0)
batch_size, _ = nearest_neighbours_matrix.shape
number_of_classes = torch.max(self.y_train) + 1
counts = torch.zeros(batch_size, number_of_classes, dtype=torch.float).to(
self.device
)
if self.weight == "uniform":
weights_matrix = torch.ones_like(
nearest_neighbours_matrix, dtype=torch.float
).to(self.device)
elif self.weight == "distance":
weights_matrix = 1 / torch.gather(distances, 1, knn_indices).to(self.device)
counts.scatter_add_(
dim=1, index=nearest_neighbours_matrix, src=(weights_matrix)
)
most_frequent = torch.argmax(counts, dim=1)
def is_draw(tensor):
sorted_tensor, _ = tensor.sort(dim=0, descending=True)
max_values = sorted_tensor[0]
second_max_values = sorted_tensor[1]
return max_values == second_max_values
for i, line in enumerate(counts):
if is_draw(line):
most_frequent[i] = nearest_neighbours_matrix[i][0]
return most_frequent
def _calc_covariances(self, X_train, y_train):
X_train = X_train.to(self.device)
y_train = y_train.to(self.device)
classes_list = torch.unique(y_train, sorted=True).to(self.device)
for i in classes_list:
cov = calc_single_covariance(X_train, y_train, i, self.device)
for _ in range(self.num_of_shrinkages):
cov = matrix_shrinkage(
cov, self.shrinkage_alpha_0, self.shrinkage_alpha_1, self.device
)
cov = normalize_covariance_matrix(cov)
if i == classes_list[0]:
covariances = cov.clone().detach()
else:
covariances = torch.cat((covariances, cov.clone().detach()))
return covariances
def _kmeans(self, X_train):
kmeans = KMeans(n_clusters=self.kmeans_k, random_state=self.sklearn_seed)
kmeans.fit(X_train.cpu().numpy())
cluster_centers = kmeans.cluster_centers_
cluster_centers_tensor = torch.tensor(cluster_centers, dtype=X_train.dtype)
return cluster_centers_tensor
def _train_logits(self, X_train, y_train):
prev_task_params = {
"a": self.parameters["a"].clone().item(),
"b": self.parameters["b"].clone().item(),
}
if self.metric == "euclidean":
distances = euclidean(self.X_train, X_train, self.device)
elif self.metric == "mahalanobis":
distances = mahalanobis(
self.X_train,
self.y_train,
X_train,
self.covMatrices,
self.tukey_lambda,
self.device,
self.norm_in_mahalanobis,
)
closest_distances = get_smallest_values_per_class(
distances, self.y_train, self.logits_n_samples
)
train_data, val_data, train_labels, val_labels = train_test_split(
closest_distances, y_train, test_size=0.2, random_state=self.sklearn_seed
)
trainloader = DataLoader(
TensorDataset(train_data, train_labels),
batch_size=self.logits_batch_size,
shuffle=True,
)
valloader = DataLoader(
TensorDataset(val_data, val_labels), batch_size=self.logits_batch_size
)
optimizer = torch.optim.Adam(
self.parameters.parameters(), lr=self.logits_learning_rate
)
criterion = torch.nn.CrossEntropyLoss()
self._epochs_without_improvement = 0
self._best_val_loss = float("inf")
for epoch in range(self.logits_train_epochs):
running_loss, regularizaion_loss = self._logits_training_loop(
trainloader, prev_task_params, optimizer, criterion
)
val_loss, accuracy = self._logits_validation_loop(valloader, criterion)
print(
f"Epoch [{epoch+1}|{self.logits_train_epochs}] Loss: {sum(running_loss) / len(running_loss)} (in this regularization: {sum(regularizaion_loss) / len(regularizaion_loss)}) ||| Validation Loss: {sum(val_loss) / len(val_loss)}, Val accuracy: {accuracy}"
)
if self.early_stopping(val_loss):
print(f"Early stopping at epoch {epoch+1}")
break
def early_stopping(self, val_loss):
val_loss_value = sum(val_loss) / len(val_loss)
if val_loss_value < self._best_val_loss:
self._best_val_loss = val_loss_value
self._epochs_without_improvement = 0
else:
self._epochs_without_improvement += 1
if self._epochs_without_improvement == self.logits_patience:
return True
return False
def _logits_training_loop(
self, trainloader, prev_task_params, optimizer, criterion
):
running_loss = []
regularizaion_loss = []
for data, target in trainloader:
optimizer.zero_grad()
output = self._calculate_logits(data)
loss = criterion(output, target.to(self.device).long())
reg_loss = self._calc_logits_regularization(prev_task_params)
loss += reg_loss * self.logits_regularization_strength
loss.backward()
optimizer.step()
running_loss.append(loss.item())
regularizaion_loss.append(
reg_loss.item() * self.logits_regularization_strength
)
return running_loss, regularizaion_loss
def _logits_validation_loop(self, valloader, criterion):
val_loss = []
accuracy = []
with torch.no_grad():
for data, target in valloader:
output = self._calculate_logits(data)
val_loss.append(criterion(output, target.to(self.device).long()).item())
accuracy.append(
[
self._count_correct_guesses(
output, target.to(self.device).long()
),
len(target),
]
)
acc = torch.sum(
torch.tensor(accuracy, dtype=torch.float)[:, 0], dim=0
) / torch.sum(torch.tensor(accuracy, dtype=torch.float)[:, 1], dim=0)
return val_loss, acc
def _calc_logits_regularization(self, prev_task_params):
# Applies L2 regularization
if self.current_task == 0:
return torch.tensor(0.0, device=self.device)
regularizaion_loss = torch.tensor(0.0, device=self.device)
for param_key in self.parameters:
regularizaion_loss += torch.sum(
(self.parameters[param_key] - prev_task_params[param_key]) ** 2
)
return regularizaion_loss
def _calculate_logits(self, data):
"""
Predict class probabilities using logits.
Args:
data (torch.Tensor): Tensor of shape (batch_size, num_classes, n_closest_samples) containing input data.
Returns:
torch.Tensor: Tensor of shape (batch_size, num_classes) containing class probabilities.
"""
data_transformed = self.parameters["a"] + self.parameters["b"] * torch.log(
data + 1e-10
)
data_activated = F.leaky_relu(data_transformed, negative_slope=0.01)
logits = data_activated.sum(dim=-1)
return logits
def _predict_with_logits(self, X_test):
if self.metric == "euclidean":
distances = euclidean(self.X_train, X_test, self.device)
elif self.metric == "mahalanobis":
distances = mahalanobis(
self.X_train,
self.y_train,
X_test,
self.covMatrices,
self.tukey_lambda,
self.device,
self.norm_in_mahalanobis,
)
closest_distances = get_smallest_values_per_class(
distances, self.y_train, self.logits_n_samples
)
logits = self._calculate_logits(closest_distances)
prediction = logits.argmax(dim=1)
return prediction
def _count_correct_guesses(self, logits, targets):
predicted_classes = torch.argmax(logits, dim=1)
correct_guesses = (predicted_classes == targets).sum().item()
return correct_guesses