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

Class Net

model/gem.py:88–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86
87
88class Net(nn.Module):
89 def __init__(self,
90 n_inputs,
91 n_outputs,
92 n_tasks,
93 args):
94 super(Net, self).__init__()
95 self.margin = args.memory_strength
96 self.net = args.net
97
98 self.ce = nn.CrossEntropyLoss()
99 self.n_outputs = n_outputs
100
101 self.opt = optim.SGD(self.parameters(), args.lr)
102 self.n_iter = args.n_iter
103 assert args.n_memories > 0, "SET args.n_memories for GEM > 0"
104 self.n_memories = args.n_memories
105 self.gpu = args.cuda
106
107 # allocate episodic memory
108 self.memory_data = torch.FloatTensor(
109 n_tasks, self.n_memories, n_inputs)
110 self.memory_labs = torch.LongTensor(n_tasks, self.n_memories)
111 if args.cuda:
112 self.memory_data = self.memory_data.cuda()
113 self.memory_labs = self.memory_labs.cuda()
114
115 # allocate temporary synaptic memory
116 self.grad_dims = []
117 for param in self.parameters():
118 self.grad_dims.append(param.data.numel())
119 self.grads = torch.Tensor(sum(self.grad_dims), n_tasks)
120 if args.cuda:
121 self.grads = self.grads.cuda()
122
123 # allocate counters
124 self.observed_tasks = []
125 self.old_task = -1
126 self.mem_cnt = 0
127 self.nc_per_task = n_outputs # shared_head
128
129 def forward(self, x, t):
130 output = self.net(x)
131 return output
132
133 def observe(self, x, t, y):
134 # update memory
135 if t != self.old_task:
136 self.observed_tasks.append(t)
137 self.old_task = t
138 print("task number ", t)
139 # Update ring buffer storing examples from current task
140 bsz = y.data.size(0)
141 endcnt = min(self.mem_cnt + bsz, self.n_memories)
142 effbsz = endcnt - self.mem_cnt
143 self.memory_data[t, self.mem_cnt: endcnt].copy_(
144 x.data[: effbsz])
145 if bsz == 1:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected