What I am trying to do is to put the the old parameters after the update
here is a demo code.
for param in net.parameters():
d_p = param.grad.data
param.data.sub_(d_p * lr)
outputs = net(inputs)
loss = criterion(outputs, labels)
for param in net.parameters():
param.data.add_(d_p * lr)
First, don’t use data – it is deprecated and breaks things.
You may modify param inplace by wrapping the operation in a with torch.no_grad(): block:
with torch.no_grad():
for param in net.parameters():
d_p = param.grad
param.sub_(d_p * lr)
outputs = net(inputs)
loss = criterion(outputs, labels)
with torch.no_grad():
for param in net.parameters():
d_p = param.grad
param.add_(d_p * lr)
(If you don’t want to backpropagate though your outputs and loss
computations, you could wrap the whole thing in a single no_grad()
block.)
Note that due to round-off error, sub_() followed by add_() won’t
necessarily take you back to exactly where you started. If you need
to restore param exactly, save a copy of param and then write it back
after the modification, e.g.
with torch.no_grad():
param.copy_ (copy_of_original_param)