Output of torch.autograd.grad

What is the return value of torch.autograd.grad function? The common way is to just use it like this:
gradients = torch.autograd.grad(loss, model.parameters())

which is fine. But I have also seen people using this:
(gradients,) = torch.autograd.grad(loss, model.parameters())

I just want to know the exact return values of the grad function.

The autograd.grad function returns an object that match the inputs argument.
When you do (foo,) you unpack a tuple of size 1 into the variable foo: (foo,) = (2,) will make foo == 2.

1 Like

Thanks Alban. hope you are doing good.

1 Like