Backward() with & without arguments: PyTorch Gradients


I am not able to grasp the concept of backward with and without arguments. In the first case, the backward was called on the sum of resultant function, and in the second case, it was called directly with ‘a’ as an argument. Can anyone clarify this confusion and when to use what form?

Hi @kaustubhgupta1828,
It it due to the fact that in the above case, s is a scalar quantity, whereas in the latter one, r is a vector.
Note that, when we call s.backward(), the default parameter 1 is passed automatically.
But when we have the resultant output tensor as a vector itself, we explicitly need to pass a tensor of all ones.

If you’re somehow not able to get me, please refer to all the examples in The “gradient” argument in Pytorch’s “backward” function — explained by examples, and then try to catch what i meant to convey.

1 Like

That article helped!