-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmetrics.py
58 lines (43 loc) · 1.68 KB
/
metrics.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
# Copyright 2020-present, Pietro Buzzega, Matteo Boschini, Angelo Porrello, Davide Abati, Simone Calderara.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
import numpy as np
from datasets.utils.continual_dataset import ContinualDataset
from typing import Tuple
def backward_transfer(results):
n_tasks = len(results)
li = list()
for i in range(n_tasks - 1):
li.append(results[-1][i] - results[i][i])
return np.mean(li)
def forward_transfer(results, random_results):
n_tasks = len(results)
li = list()
for i in range(1, n_tasks):
li.append(results[i-1][i] - random_results[i])
return np.mean(li)
def forgetting(results):
n_tasks = len(results)
li = list()
for i in range(n_tasks - 1):
results[i] += [0.0] * (n_tasks - len(results[i]))
np_res = np.array(results)
maxx = np.max(np_res, axis=0)
for i in range(n_tasks - 1):
li.append(maxx[i] - results[-1][i])
return np.mean(li)
def mask_classes(outputs: torch.Tensor, dataset: ContinualDataset, k: int):
"""
Given the output tensor, the dataset at hand and the current task,
masks the former by setting the responses for the other tasks at -inf.
It is used to obtain the results for the task-il setting.
:param outputs: the output tensor
:param dataset: the continual dataset
:param k: the task index
"""
outputs[:, 0:k * dataset.N_CLASSES_PER_TASK] = -float('inf')
outputs[:, (k + 1) * dataset.N_CLASSES_PER_TASK:
dataset.N_TASKS * dataset.N_CLASSES_PER_TASK] = -float('inf')
return outputs