-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathconvCBP.py
62 lines (52 loc) · 2.12 KB
/
convCBP.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
from torch import optim
from lop.algos.convGnT import ConvGnT
import torch.nn.functional as F
from lop.utils.AdamGnT import AdamGnT
class ConvCBP(object):
"""
The Continual Backprop algorithm
"""
def __init__(self, net, step_size=0.001, loss='mse', opt='sgd', beta=0.9, beta_2=0.999, replacement_rate=0.0001,
decay_rate=0.9, init='kaiming', util_type='contribution', maturity_threshold=100, device='cpu',
momentum=0, weight_decay=0):
self.net = net
# define the optimizer
if opt == 'sgd':
self.opt = optim.SGD(self.net.parameters(), lr=step_size, momentum=momentum, weight_decay=weight_decay)
elif opt == 'adam':
self.opt = AdamGnT(self.net.parameters(), lr=step_size, betas=(beta, beta_2), weight_decay=weight_decay)
# define the loss function
self.loss_func = {'nll': F.cross_entropy, 'mse': F.mse_loss}[loss]
# a placeholder
self.previous_features = None
# define the generate-and-test object for the given network
self.gnt = ConvGnT(
net=self.net.layers,
hidden_activation=self.net.act_type,
opt=self.opt,
replacement_rate=replacement_rate,
decay_rate=decay_rate,
init=init,
num_last_filter_outputs=net.last_filter_output,
util_type=util_type,
maturity_threshold=maturity_threshold,
device=device,
)
def learn(self, x, target):
"""
Learn using one step of gradient-descent and generate-&-test
:param x: input
:param target: desired output
:return: loss
"""
# do a forward pass and get the hidden activations
output, features = self.net.predict(x=x)
loss = self.loss_func(output, target)
self.previous_features = features
# do the backward pass and take a gradient step
loss.backward()
self.opt.step()
self.opt.zero_grad()
# take a generate-and-test step
self.gnt.gen_and_test(features=self.previous_features)
return loss.detach(), output