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

Function store_grad

model/gem.py:26–42  ·  view source on GitHub ↗

This stores parameter gradients of past tasks. pp: parameters grads: gradients grad_dims: list with number of parameters per layers tid: task id

(pp, grads, grad_dims, tid)

Source from the content-addressed store, hash-verified

24
25
26def store_grad(pp, grads, grad_dims, tid):
27 """
28 This stores parameter gradients of past tasks.
29 pp: parameters
30 grads: gradients
31 grad_dims: list with number of parameters per layers
32 tid: task id
33 """
34 # store the gradients
35 grads[:, tid].fill_(0.0)
36 cnt = 0
37 for param in pp():
38 if param.grad is not None:
39 beg = 0 if cnt == 0 else sum(grad_dims[:cnt])
40 en = sum(grad_dims[:cnt + 1])
41 grads[beg: en, tid].copy_(param.grad.data.view(-1))
42 cnt += 1
43
44
45def overwrite_grad(pp, newgrad, grad_dims):

Callers 1

observeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected