PyTorch 2 · Autograd and models

Compute gradients

Track operations and compute derivatives with backpropagation.

w = torch.tensor(2.0, requires_grad=True)
loss = (w * 3 - 1) ** 2
loss.backward()
print(w.grad)

Official PyTorch 2 documentation

More autograd and models commands

All autograd and models commands · PyTorch 2 cheatsheet

Was this page helpful?