-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoint_gcl.py
executable file
·62 lines (49 loc) · 2.22 KB
/
joint_gcl.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
# 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 math
import torch
from torch.optim import SGD
from models.utils.continual_model import ContinualModel
from utils.args import add_management_args, add_experiment_args, ArgumentParser
from utils.status import progress_bar
def get_parser() -> ArgumentParser:
parser = ArgumentParser(description='Joint training: a strong, simple baseline.')
add_management_args(parser)
add_experiment_args(parser)
return parser
class JointGCL(ContinualModel):
NAME = 'joint_gcl'
COMPATIBILITY = ['general-continual']
def __init__(self, backbone, loss, args, transform):
super(JointGCL, self).__init__(backbone, loss, args, transform)
self.old_data = []
self.old_labels = []
self.current_task = 0
def end_task(self, dataset):
# reinit network
self.net = dataset.get_backbone()
self.net.to(self.device)
self.net.train()
self.opt = SGD(self.net.parameters(), lr=self.args.lr)
# gather data
all_data = torch.cat(self.old_data)
all_labels = torch.cat(self.old_labels)
# train
for e in range(1): # range(self.args.n_epochs):
rp = torch.randperm(len(all_data))
for i in range(math.ceil(len(all_data) / self.args.batch_size)):
inputs = all_data[rp][i * self.args.batch_size:(i + 1) * self.args.batch_size]
labels = all_labels[rp][i * self.args.batch_size:(i + 1) * self.args.batch_size]
inputs, labels = inputs.to(self.device), labels.to(self.device)
self.opt.zero_grad()
outputs = self.net(inputs)
loss = self.loss(outputs, labels.long())
loss.backward()
self.opt.step()
progress_bar(i, math.ceil(len(all_data) / self.args.batch_size), e, 'J', loss.item())
def observe(self, inputs, labels, not_aug_inputs):
self.old_data.append(inputs.data)
self.old_labels.append(labels.data)
return 0