-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathewc.py
104 lines (84 loc) · 3.29 KB
/
ewc.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
# refer from https://github.com/moskomule/ewc.pytorch
from copy import deepcopy
import torch
from torch import nn
from torch.nn import functional as F
from torch.autograd import Variable
import torch.utils.data
def variable(t: torch.Tensor, use_cuda=False, **kwargs):
if torch.cuda.is_available() and use_cuda:
t = t.cuda()
return Variable(t, **kwargs)
class EWC(object):
def __init__(self, model, input, target, ids, task):
self.model = model
self.task = task
self.dataset = [input, target, ids]
self.params = {n: p for n, p in self.model.named_parameters() if p.requires_grad}
self._means = {}
self._precision_matrices = self._diag_fisher()
for n, p in deepcopy(self.params).items():
self._means[n] = variable(p.data)
def _diag_fisher(self):
precision_matrices = {}
for n, p in deepcopy(self.params).items():
p.data.zero_()
precision_matrices[n] = variable(p.data)
self.model.eval()
ids = self.dataset[2]
input = self.dataset[0][ids]
target = self.dataset[1][ids]
self.model.zero_grad()
input = variable(input)
output = self.model(input)
if self.task == "classification":
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
else:
criterion = nn.MSELoss()
output = output.view(-1)
loss = criterion(output, target)
loss.backward()
for n, p in self.model.named_parameters():
precision_matrices[n].data += p.grad.data ** 2 / len(ids)
precision_matrices = {n: p for n, p in precision_matrices.items()}
return precision_matrices
def penalty(self, model: nn.Module):
loss = 0
for n, p in model.named_parameters():
_loss = self._precision_matrices[n] * (p - self._means[n]) ** 2
loss += _loss.sum()
return loss
def normal_train(model: nn.Module, optimizer: torch.optim, data_loader: torch.utils.data.DataLoader):
model.train()
epoch_loss = 0
for input, target in data_loader:
input, target = variable(input), variable(target)
optimizer.zero_grad()
output = model(input)
loss = F.cross_entropy(output, target)
epoch_loss += loss.data[0]
loss.backward()
optimizer.step()
return epoch_loss / len(data_loader)
def ewc_train(model: nn.Module, optimizer: torch.optim, data_loader: torch.utils.data.DataLoader,
ewc: EWC, importance: float):
model.train()
epoch_loss = 0
for input, target in data_loader:
input, target = variable(input), variable(target)
optimizer.zero_grad()
output = model(input)
loss = F.cross_entropy(output, target) + importance * ewc.penalty(model)
epoch_loss += loss.data[0]
loss.backward()
optimizer.step()
return epoch_loss / len(data_loader)
def test(model: nn.Module, data_loader: torch.utils.data.DataLoader):
model.eval()
correct = 0
for input, target in data_loader:
input, target = variable(input), variable(target)
output = model(input)
correct += (F.softmax(output, dim=1).max(dim=1)[1] == target).data.sum()
return correct / len(data_loader.dataset)