Hi,
For this to work you need to pass the create_graph=True
option to the first .backward()
to let it know that you need to be able to call .backward()
on the grad itself.
Also if you need the gradient wrt a specific variable, you can use the autograd.grad(outputs, inputs)
function to get the derivative of the output(s) wrt the input(s). For example out.backward()
is equivalent to autograd.grad(out, model.parameters())
.