PyTorch Autograd Explained: What .backward() Actually Does A developer explains the inner workings of PyTorch's autograd engine, detailing how the .backward() method computes gradients by walking a computation graph in reverse. The post uses the example y = x² to illustrate that PyTorch applies the chain rule node-by-node without ever forming the derivative expression explicitly. most autograd tutorials show you Skip this section if you are comfortable with it, but most confusion about autograd is really confusion about what it is producing. Imagine standing on a hillside in fog. You cannot see the bottom, but you can feel that the ground tilts. Step in the direction it tilts downward and you get lower. That is a gradient: the slope of the ground under your feet. Now put that on a graph. The horizontal axis is one adjustable number inside the model, a parameter. The vertical axis is the loss. The curve shows how the loss changes as the parameter changes, and somewhere on it there is a lowest point. Take y = x² . Its slope at position x is 2x . At x = 3 the slope is 6, steep and positive, so the loss climbs to the right and you should step left. At x = -2 it is -4, so you step right. At x = 0 it is 0, and you have arrived. python import torch x = torch.tensor 3.0, requires grad=True y = x 2 y.backward print x.grad tensor 6. PyTorch produced the slope without being told the formula. That is autograd, and the rest of this article is how. Tensors do not track gradients by default. That would be wasted work on your input data, which is never adjusted. You opt in with requires grad=True : x = torch.tensor 3.0, requires grad=True print x.requires grad True From this moment every operation involving x gets recorded. Think of it as a receipt. Each multiplication, addition and power you apply gets written down, along with enough information to reverse it later. The receipt has a proper name, the computation graph. It is a chain of nodes where each node is an operation and each edge carries a tensor from one operation into the next. x = torch.tensor 3.0, requires grad=True y = x 2 node: power z = 2 y + 1 nodes: multiply, then add Three lines of Python, four values, three operation nodes: x leaf 2 ×2 +1 z 3.0 → 9.0 → 18.0 → 19.0 → 19.0 This is the forward pass. It runs left to right, computes the answer, and as a side effect builds the graph. Nothing has been differentiated yet. The graph exists purely so that something can walk it backwards. Two things are worth noticing here. First, x is a leaf — you created it directly rather than computing it from something else. Second, every result node remembers the operation that produced it. You can see this: print z.grad fn