Weird optimizer behaviour

why is this code not crashing

import torch
x=2.0
y =2.0
param  =  torch.nn.Parameter(torch.tensor(x))
opt = torch.optim.SGD([param],lr =.1)
loss = torch.nn.Parameter(torch.tensor(y))
loss.backward()
opt.step()
print('done')

Should not the optimizer complain saying param was not used in the computation ?
Thanks in advance!

Hi,

Optimizers don’t do that in general no. They will ignore Tensors whose .grad field is None and use the gradient that is there if there is one.

1 Like