Starting Python… (first visit downloads ≈ 15 MB, then it is cached)
import torch x = torch.tensor(2.0) w = torch.tensor(3.0, requires_grad=True) b = torch.tensor(1.0, requires_grad=True) y = torch.tensor(10.0) pred = w * x + b loss = (pred - y) ** 2 loss.backward() w.grad, b.grad
ArrayLab computes these numbers with NumPy in float32, writing out the chain rule that loss.backward() applies (see Code that ran). The grad_fn names and error messages were recorded with PyTorch 2.14.0. Run it with real PyTorch in Colab.
import numpy as np # Leaves. PyTorch: torch.tensor(3.0, requires_grad=True) — float32 by default x = np.float32(2.0) w = np.float32(3.0) # requires_grad=True b = np.float32(1.0) # requires_grad=True y = np.float32(10.0) # Forward: compute the loss, one operation at a time m = w * x pred = m + b diff = pred - y loss = diff ** 2 # Backward: what loss.backward() computes — the chain rule, from loss to the leaves grad_loss = np.float32(1.0) grad_diff = grad_loss * 2 * diff grad_pred = grad_diff * 1 grad_m = grad_pred * 1 grad_b = grad_pred * 1 grad_w = grad_m * x loss, grad_w, grad_b
Output
Output appears here.