Skip to content

Commit 1cc9108

Browse files
author
Arthur Douillard
committed
[common] Add beginning of lib.
1 parent c91dbf2 commit 1cc9108

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

inclearn/lib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import callbacks, metrics

inclearn/lib/callbacks.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import torch
2+
3+
4+
class GaussianNoiseAnnealing:
5+
"""Add gaussian noise to the gradients.
6+
7+
Add gaussian noise to the gradients with the given mean & std. The std will
8+
decrease at each batch up to 0.
9+
10+
# References:
11+
- Adding Gradient Noise Improves Learning for Very Deep Networks
12+
- https://arxiv.org/abs/1511.06807
13+
14+
:param eta: TODO
15+
:param gamma: Decay rate.
16+
"""
17+
def __init__(self, parameters, eta=0.3, gamma=0.55):
18+
self._parameters = parameters
19+
self._eta = eta
20+
self._gamma = gamma
21+
22+
self._iteration = 0
23+
24+
def add_noise(self):
25+
variance = self._eta / ((1 + self._iteration) ** self._gamma)
26+
27+
for param in self._parameters:
28+
# L2 regularization on gradients
29+
param.grad.add_(0.0001, torch.norm(param.grad, p=2))
30+
31+
# Noise on gradients:
32+
noise = torch.randn(param.grad.shape, device=param.grad.device) * variance
33+
param.grad.add_(noise)
34+
35+
param.grad.clamp_(min=-5, max=5)
36+
37+
def step(self):
38+
self._iteration += 1

inclearn/lib/metrics.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
3+
4+
class MetricLogger:
5+
def __init__(self):
6+
self._accuracy_per_task = []
7+
self._accuracy = []
8+
self._incremental_accuracy = []
9+
10+
def add_task(ypred, ytrue, task_name):
11+
pass
12+
13+
14+
15+
def compute_metrics(ypred, ytrue, task_size=10):
16+
metrics = {}
17+
18+
metrics["accuracy"] = accuracy(ypred, ytrue, task_size=task_size)
19+
metrics["incremental_accuracy"] = incremental_accuracy(metrics["accuracy"])
20+
21+
22+
def accuracy(ypred, ytrue, task_size=10):
23+
"""Computes accuracy for the whole test & per task.
24+
25+
:param ypred: The predictions array.
26+
:param ytrue: The ground-truth array.
27+
:param task_size: The size of the task.
28+
:return: A dictionnary.
29+
"""
30+
all_acc = {}
31+
32+
all_acc["total"] = round((ypred == ytrue).sum() / len(ytrue), 3)
33+
34+
for class_id in range(0, np.max(ytrue), task_size):
35+
idxes = np.where(
36+
np.logical_and(ytrue >= class_id, ytrue < class_id + task_size)
37+
)[0]
38+
39+
label = "{}-{}".format(
40+
str(class_id).rjust(2, "0"),
41+
str(class_id + task_size - 1).rjust(2, "0")
42+
)
43+
all_acc[label] = round((ypred[idxes] == ytrue[idxes]).sum() / len(idxes), 3)
44+
45+
return all_acc
46+
47+
48+
49+
def incremental_accuracy(acc_dict):
50+
"""Computes the average incremental accuracy as described in iCaRL.
51+
52+
It is the average of the current task accuracy (tested on 0-X) with the
53+
previous task accuracy.
54+
55+
:param acc_dict: A dict TODO
56+
"""
57+
v, c = 0., 0
58+
59+
pass

0 commit comments

Comments
 (0)