-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathewc_on.py
executable file
·81 lines (63 loc) · 2.81 KB
/
ewc_on.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
# 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 torch.nn as nn
import torch.nn.functional as F
from models.utils.continual_model import ContinualModel
from utils.args import add_management_args, add_experiment_args, ArgumentParser
def get_parser() -> ArgumentParser:
parser = ArgumentParser(description='Continual learning via'
' online EWC.')
add_management_args(parser)
add_experiment_args(parser)
parser.add_argument('--e_lambda', type=float, required=True,
help='lambda weight for EWC')
parser.add_argument('--gamma', type=float, required=True,
help='gamma parameter for EWC online')
return parser
class EwcOn(ContinualModel):
NAME = 'ewc_on'
COMPATIBILITY = ['class-il', 'domain-il', 'task-il']
def __init__(self, backbone, loss, args, transform):
super(EwcOn, self).__init__(backbone, loss, args, transform)
self.logsoft = nn.LogSoftmax(dim=1)
self.checkpoint = None
self.fish = None
def penalty(self):
if self.checkpoint is None:
return torch.tensor(0.0).to(self.device)
else:
penalty = (self.fish * ((self.net.get_params() - self.checkpoint) ** 2)).sum()
return penalty
def end_task(self, dataset):
fish = torch.zeros_like(self.net.get_params())
for j, data in enumerate(dataset.train_loader):
inputs, labels, _ = data
inputs, labels = inputs.to(self.device), labels.to(self.device)
for ex, lab in zip(inputs, labels):
self.opt.zero_grad()
output = self.net(ex.unsqueeze(0))
loss = - F.nll_loss(self.logsoft(output), lab.unsqueeze(0),
reduction='none')
exp_cond_prob = torch.mean(torch.exp(loss.detach().clone()))
loss = torch.mean(loss)
loss.backward()
fish += exp_cond_prob * self.net.get_grads() ** 2
fish /= (len(dataset.train_loader) * self.args.batch_size)
if self.fish is None:
self.fish = fish
else:
self.fish *= self.args.gamma
self.fish += fish
self.checkpoint = self.net.get_params().data.clone()
def observe(self, inputs, labels, not_aug_inputs):
self.opt.zero_grad()
outputs = self.net(inputs)
penalty = self.penalty()
loss = self.loss(outputs, labels) + self.args.e_lambda * penalty
assert not torch.isnan(loss)
loss.backward()
self.opt.step()
return loss.item()