Skip to content
PyTorch

Autograd

Compute gradients automatically with backward().

#autograd#gradient

Code

pytorch
import torch

x = torch.tensor(2.0, requires_grad=True)
y = torch.tensor(3.0, requires_grad=True)

# Forward: z = 3x^2 + 2y
z = 3 * x ** 2 + 2 * y

# Backward to populate .grad
z.backward()
print(x.grad, y.grad)  # dz/dx=6x=12, dz/dy=2

# Detach from graph
detached = z.detach()

# No-grad context for inference
with torch.no_grad():
    out = x * 2 + y

# Custom gradient via Function (advanced)
class MyReLU(torch.autograd.Function):
    @staticmethod
    def forward(ctx, inp):
        ctx.save_for_backward(inp)
        return inp.clamp(min=0)
    @staticmethod
    def backward(ctx, grad_out):
        inp, = ctx.saved_tensors
        return grad_out * (inp > 0).float()