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

Method forward

model/icarl.py:61–95  ·  view source on GitHub ↗
(self, x, t)

Source from the content-addressed store, hash-verified

59 self.old_task = -1
60
61 def forward(self, x, t):
62 # nearest neighbor
63 nd = self.n_feat
64 ns = x.size(0)
65
66 if t * self.nc_per_batch not in self.mem_class_x.keys():
67 # no exemplar in memory yet, output uniform distr. over classes in
68 # task t above, we check presence of first class for this task, we
69 # should check them all
70 out = torch.Tensor(ns, self.n_classes).fill_(-10e10)
71 out[:, 0:self.n_classes].fill_(
72 1.0 / self.n_classes)
73 if self.gpu:
74 out = out.cuda()
75 return out
76 means = torch.ones(len(self.mem_class_x.keys()), nd) * float('inf')
77 if self.gpu:
78 means = means.cuda()
79
80 for cc in self.mem_class_x.keys():
81 means[cc] = self.net(self.mem_class_x[cc]).data.mean(0)
82 classpred = torch.LongTensor(ns)
83 preds = self.net(x).data.clone()
84 for ss in range(ns):
85 dist = (means - preds[ss].expand(len(self.mem_class_x.keys()), nd)).norm(2, 1)
86 _, ii = dist.min(0)
87 ii = ii.squeeze()
88 classpred[ss] = ii.item()
89
90 out = torch.zeros(ns, self.n_classes)
91 if self.gpu:
92 out = out.cuda()
93 for ss in range(ns):
94 out[ss, classpred[ss]] = 1
95 return out # return 1-of-C code, ns x nc
96
97 def forward_training(self, x, t):
98 output = self.net(x)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected