MCPcopy Create free account
hub / github.com/Mattdl/ContinualPrototypeEvolution / Net

Class Net

model/finetune.py:15–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15class Net(torch.nn.Module):
16
17 def __init__(self,
18 n_inputs,
19 n_outputs,
20 n_tasks,
21 args):
22 super(Net, self).__init__()
23 self.net = args.net
24
25 # setup optimizer
26 self.opt = torch.optim.SGD(self.parameters(), lr=args.lr)
27
28 # setup losses
29 self.bce = torch.nn.CrossEntropyLoss()
30
31 self.nc_per_task = n_outputs
32 self.n_outputs = n_outputs
33 self.n_iter = args.n_iter
34 self.batch_size = args.batch_size # How many to process per update
35
36 def forward(self, x, t):
37 output = self.net(x)
38 return output
39
40 def observe(self, x, t, y):
41 self.train()
42 for batch_iter in range(self.n_iter): # How many times reprocess same batch
43 permutation = torch.randperm(x.size()[0]) # Shuffle
44
45 for i in range(0, x.size()[0], self.batch_size): # Iterate mini-batches
46 self.zero_grad()
47 indices = permutation[i:i + self.batch_size]
48 batch_x, batch_y = x[indices], y[indices]
49 ptloss = self.bce(self.forward(
50 batch_x, t),
51 batch_y)
52 ptloss.backward()
53 self.opt.step()
54
55 def get_hyperparam_list(self, args):
56 return []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected