← Home

Backpropagation from Scratch: Build an Autograd Engine

How does backpropagation work? Build a working autograd engine from scratch in ~80 lines of Python: computation graphs, chain rule, reverse-mode autodiff.

View as Markdown

When I call .backward() in PyTorch, gradients flow backward through millions of operations, and I never write a derivative by hand. I used to treat that as unexplained machinery. It is the chain rule, a computation graph, and a walk through the graph in reverse order.

This post is one continuous walkthrough. It covers why training needs gradients, the chain rule, and how expressions become graphs. Then I derive backprop by hand on $z = (x+y) \cdot y$ with $x=2$, $y=3$, giving every gradient step its own line. Finally I write the ~80-line Python Value class that automates the same process. It pairs with numerical gradient checking: work out the maths yourself before you trust the engine. This is part of the ML Foundations series; if linear algebra is still fuzzy, start there.

By the end I have a working autograd engine that trains a neural network on XOR.

Autograd computation graph — nodes pulse orange on forward pass, blue on backward pass


Why do neural networks need derivatives?

Training a neural network means minimizing a loss function $L$ by nudging parameters in the direction that decreases $L$. That direction is the gradient: a vector of partial derivatives $\frac{\partial L}{\partial w_i}$ for every weight $w_i$.

A network with millions of parameters rules out computing these derivatives by hand, so a machine has to do it. Three approaches exist:

Method How Problem
Symbolic diff Algebra: manipulate expressions Expression explosion
Numerical diff Finite differences: $\frac{f(x+h) - f(x)}{h}$ Slow, numerically unstable
Automatic diff Track ops at runtime, apply chain rule This is what we build

Automatic differentiation (autograd) is exact up to floating point, fast, and works with any Python control flow: loops, conditionals, recursion. Once the engine below is built, I verify every backward() with numerical gradient checking before trusting it on anything harder than XOR.

graph LR
  subgraph "Differentiation methods"
    S["Symbolic\ndiff"] -->|"Expression\nexplosion"| X1["✗"]
    N["Numerical\ndiff"] -->|"Slow +\nunstable"| X2["✗"]
    A["Automatic\ndiff"] -->|"Exact +\nfast"| OK["✓"]
  end
  style A fill:#d4edda,stroke:#009900,color:#111
  style OK fill:#d4edda,stroke:#009900,color:#111
  style X1 fill:#f8d7da,stroke:#990000,color:#111
  style X2 fill:#f8d7da,stroke:#990000,color:#111

What is the chain rule, and why is it enough?

Every neural network computation decomposes into elementary operations: add, multiply, exponentiate, ReLU, and so on. The chain rule composes the derivatives of those operations along the sequence.

If $L = f(g(x))$, then:

$$ \frac{dL}{dx} = \frac{dL}{df} \cdot \frac{df}{dg} \cdot \frac{dg}{dx} $$

Backprop reads this right to left: start at the output and multiply by each local derivative on the way to the inputs.

More generally, for a node $v$ in a computation graph with successors $s_1, s_2, \ldots$:

$$ \bar{v} = \sum_i \bar{s_i} \cdot \frac{\partial s_i}{\partial v} $$

where $\bar{v} = \frac{\partial L}{\partial v}$ is called the adjoint of $v$.

The sum matters. If one variable feeds two later operations (here $y$ appears in both $x+y$ and a multiply), its gradient receives contributions from both branches. I add them and never overwrite.

This adjoint equation is the whole mathematical foundation. Everything below is implementation.

flowchart LR
  L["Output L"] -->|"local ∂"| N3["Node 3"]
  N3 -->|"local ∂"| N2["Node 2"]
  N2 -->|"local ∂"| N1["Node 1"]
  N1 -->|"local ∂"| X["Input x"]
  style L fill:#d4edda,stroke:#009900,color:#111

What is a computation graph?

Every expression I write builds a directed acyclic graph (DAG) of operations. Nodes are values; edges are dependencies. The forward pass walks from leaves to root, computing each node’s value. The backward pass walks from root to leaves, distributing gradients via the chain rule.

Here is a slightly larger example before the main one:

a = 2.0
b = 3.0
c = a * b   # c = 6
d = c + b   # d = 9
L = d * 2   # L = 18

The graph looks like:

graph LR
  a("a = 2.0") --> mul["× multiply"]
  b("b = 3.0") --> mul
  mul --> c("c = 6.0")
  c --> add["+ add"]
  b --> add
  add --> d("d = 9.0")
  d --> mul2["× 2"]
  mul2 --> L("L = 18.0")

  style L fill:#d4edda,stroke:#009900,color:#111
  style a fill:#fff,stroke:#dddddd,color:#111
  style b fill:#fff,stroke:#dddddd,color:#111

Leaf nodes (a, b) are inputs. The root (L) is the output. Every interior node must remember three things:

  1. Its value (forward pass result)
  2. Which children (parents in the dataflow sense) created it
  3. How to backprop gradients to those children: the local derivative rule for that operation

b appears twice in this graph. When gradients flow backward, b accumulates contributions from both the multiply that produced c and the add that produced d. That accumulation is the chain rule at work, not a bug.


How do I backpropagate $z = (x+y) \cdot y$ by hand?

This is the example I wish someone had walked me through before I wrote any code. I skip no steps.

Setup. Let $x = 2$, $y = 3$. Define an intermediate sum and the final output:

$$ s = x + y $$
$$ z = s \cdot y $$

I treat $z$ as the scalar “loss” at the top of the graph: the quantity I want derivatives of with respect to $x$ and $y$.

Forward pass — every value on its own line

Substitute $x = 2$:

$$ x = 2 $$

Substitute $y = 3$:

$$ y = 3 $$

Compute the sum:

$$ s = x + y $$
$$ s = 2 + 3 $$
$$ s = 5 $$

Compute the product:

$$ z = s \cdot y $$
$$ z = 5 \cdot 3 $$
$$ z = 15 $$

The forward pass is done. As a graph:

graph LR
  x("x = 2") --> add["+ add"]
  y("y = 3") --> add
  add --> s("s = 5")
  s --> mul["× multiply"]
  y --> mul
  mul --> z("z = 15")

  style z fill:#d4edda,stroke:#009900,color:#111
  style x fill:#fff,stroke:#dddddd,color:#111
  style y fill:#fff,stroke:#dddddd,color:#111

y feeds two operations, and that determines how the backward pass accumulates.

Backward pass — every gradient on its own line

Reverse mode starts at the output. Seed the top node:

$$ \frac{\partial z}{\partial z} = 1 $$

I write $\bar{v} = \frac{\partial z}{\partial v}$ for short.

Step 1 — multiply node $z = s \cdot y$.

Local partial of $z$ with respect to $s$ (treat $y$ as constant):

$$ \frac{\partial z}{\partial s} = y $$
$$ \frac{\partial z}{\partial s} = 3 $$

Local partial of $z$ with respect to $y$ (treat $s$ as constant):

$$ \frac{\partial z}{\partial y}\bigg|_{\text{mul}} = s $$
$$ \frac{\partial z}{\partial y}\bigg|_{\text{mul}} = 5 $$

Chain rule from $z$ into $s$:

$$ \bar{s} = \frac{\partial z}{\partial z} \cdot \frac{\partial z}{\partial s} $$
$$ \bar{s} = 1 \cdot 3 $$
$$ \bar{s} = 3 $$

Chain rule from $z$ into $y$ through the multiply branch only:

$$ \bar{y}_{\text{mul}} = \frac{\partial z}{\partial z} \cdot \frac{\partial z}{\partial y}\bigg|_{\text{mul}} $$
$$ \bar{y}_{\text{mul}} = 1 \cdot 5 $$
$$ \bar{y}_{\text{mul}} = 5 $$

Step 2 — add node $s = x + y$.

Local partial of $s$ with respect to $x$:

$$ \frac{\partial s}{\partial x} = 1 $$

Local partial of $s$ with respect to $y$:

$$ \frac{\partial s}{\partial y} = 1 $$

Chain rule from $s$ into $x$:

$$ \bar{x} = \bar{s} \cdot \frac{\partial s}{\partial x} $$
$$ \bar{x} = 3 \cdot 1 $$
$$ \bar{x} = 3 $$

Chain rule from $s$ into $y$ through the add branch:

$$ \bar{y}_{\text{add}} = \bar{s} \cdot \frac{\partial s}{\partial y} $$
$$ \bar{y}_{\text{add}} = 3 \cdot 1 $$
$$ \bar{y}_{\text{add}} = 3 $$

Step 3 — accumulate $\bar{y}$ from both branches.

$y$ appeared in the add and the multiply. Sum the adjoints:

$$ \bar{y} = \bar{y}_{\text{mul}} + \bar{y}_{\text{add}} $$
$$ \bar{y} = 5 + 3 $$
$$ \bar{y} = 8 $$

Final answers

$$ \frac{\partial z}{\partial x} = 3 $$
$$ \frac{\partial z}{\partial y} = 8 $$

Sanity check with calculus. Expand $z = (x+y)y = xy + y^2$:

$$ \frac{\partial z}{\partial x} = y = 3 \quad \checkmark $$
$$ \frac{\partial z}{\partial y} = x + 2y = 2 + 6 = 8 \quad \checkmark $$

My Value.backward() automates this walk: seed the root with 1, visit nodes in reverse topological order, apply each operation’s local rule, and accumulate with += when a variable appears more than once.

This table holds every stage of the backward pass on this example. Each column lists the adjoints known by then.

Table of six adjoints across four stages of the backward pass. The seed gives z-bar 1. The multiply node gives s-bar 3 and y-bar 5. The add node gives x-bar 3 and y-bar 3. Accumulation gives y-bar 8.
Each column adds the adjoints known after that stage. The final answers are $\partial z/\partial x = 3$ and $\partial z/\partial y = 8$.

The same formulas hold for any inputs: $\partial z/\partial x = y$ and $\partial z/\partial y = x + 2y$. This table covers three choices of $x$ and $y$. The first row is the example I just worked.

Table of three cases of x and y. For x 2 and y 3, x-bar is 3 and y-bar is 8, which equals x plus 2y. For x minus 1 and y 2 it is 2 and 3. For x 4 and y minus 2 it is minus 2 and 0.
In every row, the backward-pass value $\bar{y} = s + y$ equals the closed form $x + 2y$.
graph LR
  subgraph "Backward order"
    direction RL
    z2["z\n∂z/∂z = 1"] --> mul2["×\n∂z/∂s=3, ∂z/∂y|mul=5"]
    mul2 --> s2["s\n∂s/∂x=1, ∂s/∂y=1"]
    s2 --> x2["x\n∂z/∂x = 3"]
    s2 --> y2["y\n∂z/∂y = 5+3 = 8"]
    mul2 --> y2
  end
  style z2 fill:#d4edda,stroke:#009900,color:#111

How do I build the Value class?

Now I wrap every scalar in a Value object. Each Value stores its data, its gradient, its parent nodes, and a closure _backward that distributes gradients to those parents. The closure encodes the local rules I just derived by hand.

classDiagram
  class Value {
    +float data
    +float grad
    +set _prev
    +string _op
    +_backward()
    +__add__(other)
    +__mul__(other)
    +__pow__(exp)
    +relu()
    +backward()
  }
  note for Value "grad starts at 0.0\n_backward is a closure\nstored at creation time"
class Value:
    def __init__(self, data, _children=(), _op=''):
        self.data = float(data)
        self.grad = 0.0
        self._backward = lambda: None  # no-op by default
        self._prev = set(_children)
        self._op = _op  # for debugging

    def __repr__(self):
        return f"Value(data={self.data:.4f}, grad={self.grad:.4f})"

The grad field starts at zero. It accumulates contributions from all downstream nodes during the backward pass, as in the $\bar{y} = 5 + 3$ pattern above.

Reproduce the hand example in code:

x = Value(2.0)
y = Value(3.0)

s = x + y       # s.data == 5
z = s * y       # z.data == 15

z.backward()

print(x.grad)   # 3.0 ✓
print(y.grad)   # 8.0 ✓  (5 from mul + 3 from add)

If either number is wrong, I stop there and run numerical gradient checking on that op until it matches to $10^{-5}$.


How does each operation define its local gradient?

For each operation, I do two things:

  1. Compute the output value (forward pass)
  2. Define a _backward closure that accumulates gradients into the inputs using the chain rule

The closures are the code version of the hand derivation above.

Addition

Mathematically: if $c = a + b$, then $\frac{\partial c}{\partial a} = 1$ and $\frac{\partial c}{\partial b} = 1$.

So when the backward pass tells me $\frac{\partial L}{\partial c}$ (stored in c.grad), I accumulate:

$$ \frac{\partial L}{\partial a} \mathrel{+}= \frac{\partial L}{\partial c} \cdot 1 $$
$$ \frac{\partial L}{\partial b} \mathrel{+}= \frac{\partial L}{\partial c} \cdot 1 $$

That is exactly the $\bar{y}_{\text{add}} = \bar{s} \cdot 1$ step from the worked example.

def __add__(self, other):
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data + other.data, (self, other), '+')

    def _backward():
        self.grad  += 1.0 * out.grad
        other.grad += 1.0 * out.grad
    out._backward = _backward
    return out

Note the +=. A node can be used several times in a computation graph, so gradients accumulate instead of overwriting.

Multiplication

If $c = a \cdot b$, then $\frac{\partial c}{\partial a} = b$ and $\frac{\partial c}{\partial b} = a$.

That is the $\bar{s} = \bar{z} \cdot y$ and $\bar{y}_{\text{mul}} = \bar{z} \cdot s$ step.

def __mul__(self, other):
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data * other.data, (self, other), '*')

    def _backward():
        self.grad  += other.data * out.grad
        other.grad += self.data  * out.grad
    out._backward = _backward
    return out

Power

If $y = x^n$, then $\frac{\partial y}{\partial x} = n \cdot x^{n-1}$.

def __pow__(self, exponent):
    assert isinstance(exponent, (int, float))
    out = Value(self.data ** exponent, (self,), f'**{exponent}')

    def _backward():
        self.grad += exponent * (self.data ** (exponent - 1)) * out.grad
    out._backward = _backward
    return out

ReLU

$$ \text{ReLU}(x) = \max(0, x) \qquad \frac{\partial \text{ReLU}}{\partial x} = \begin{cases} 1 & x > 0 \\ 0 & x \leq 0 \end{cases} $$
def relu(self):
    out = Value(max(0, self.data), (self,), 'ReLU')

    def _backward():
        self.grad += (out.data > 0) * out.grad
    out._backward = _backward
    return out

Convenience wrappers

def __neg__(self):         return self * -1
def __sub__(self, other):  return self + (-other)
def __truediv__(self, other): return self * other**-1
def __radd__(self, other): return self + other
def __rmul__(self, other): return self * other
def __rsub__(self, other): return other + (-self)

The r variants handle expressions like 2 * v where the left operand is a Python number.


How does the backward pass visit nodes in the right order?

The hand derivation needed $\bar{s}$ before it could compute $\bar{x}$ and $\bar{y}_{\text{add}}$, so the order matters.

The right order is one where every node that depends on a node has its gradient computed before that node is processed. That is reverse topological order.

graph LR
  subgraph "Forward order (topo)"
    direction LR
    A["a"] --> C["c=a*b"] --> E["L=c+b"]
    B["b"] --> C
    B --> E
  end
  subgraph "Backward order (reversed)"
    direction RL
    E2["L"] --> C2["c"] --> A2["a"]
    E2 --> B2["b"]
    C2 --> B2
  end
  style E fill:#d4edda,stroke:#009900,color:#111
  style E2 fill:#d4edda,stroke:#009900,color:#111

For $z = (x+y) \cdot y$, backward visits: $z \to$ multiply $\to$ $s \to$ add $\to$ $x$, $y$. Both the multiply and the add write to y.grad with +=, so the order among those writes does not matter. What matters is that z.grad is set before the multiply closure reads it, and that s.grad is final before the add closure reads it.

I find the order with a depth-first search, then walk it in reverse:

def backward(self):
    topo = []
    visited = set()

    def build_topo(v):
        if v not in visited:
            visited.add(v)
            for child in v._prev:
                build_topo(child)
            topo.append(v)

    build_topo(self)

    # Seed: dL/dL = 1
    self.grad = 1.0
    for node in reversed(topo):
        node._backward()

The DFS appends a node after recursing into its children, so topo comes out in topological order (leaves first, root last). I reverse it to start at the root and work toward the leaves.


What does the full autograd engine look like?

All the pieces in one place, 82 lines:

class Value:
    def __init__(self, data, _children=(), _op=''):
        self.data = float(data)
        self.grad = 0.0
        self._backward = lambda: None
        self._prev = set(_children)
        self._op = _op

    def __add__(self, other):
        other = other if isinstance(other, Value) else Value(other)
        out = Value(self.data + other.data, (self, other), '+')
        def _backward():
            self.grad  += out.grad
            other.grad += out.grad
        out._backward = _backward
        return out

    def __mul__(self, other):
        other = other if isinstance(other, Value) else Value(other)
        out = Value(self.data * other.data, (self, other), '*')
        def _backward():
            self.grad  += other.data * out.grad
            other.grad += self.data  * out.grad
        out._backward = _backward
        return out

    def __pow__(self, exponent):
        out = Value(self.data ** exponent, (self,), f'**{exponent}')
        def _backward():
            self.grad += exponent * (self.data ** (exponent - 1)) * out.grad
        out._backward = _backward
        return out

    def relu(self):
        out = Value(max(0, self.data), (self,), 'ReLU')
        def _backward():
            self.grad += (out.data > 0) * out.grad
        out._backward = _backward
        return out

    def backward(self):
        topo, visited = [], set()
        def build_topo(v):
            if v not in visited:
                visited.add(v)
                for child in v._prev: build_topo(child)
                topo.append(v)
        build_topo(self)
        self.grad = 1.0
        for node in reversed(topo):
            node._backward()

    def __neg__(self):          return self * -1
    def __sub__(self, other):   return self + (-other)
    def __truediv__(self, other): return self * other**-1
    def __radd__(self, other):  return self + other
    def __rmul__(self, other):  return self * other
    def __repr__(self):
        return f"Value(data={self.data:.4f}, grad={self.grad:.4f})"

That is the whole engine. It has three parts:

  1. Value wraps a scalar and stores grad and a _backward closure.
  2. The operation overloads each compute an output and register their local gradient rule.
  3. backward() does a topological sort, then a chain rule traversal from root to leaves.

PyTorch, JAX and other frameworks use the same idea, scaled up to tensors and compiled to run on GPUs.


How do I verify the engine against calculus?

The $z = (x+y) \cdot y$ example was the teaching walkthrough. This second check has enough operations to catch a sign error or a forgotten +=.

Let $L = (a \cdot b + c)^2$ with $a=2$, $b=3$, $c=1$.

Forward pass:

$$ ab = 2 \cdot 3 = 6 $$
$$ ab + c = 6 + 1 = 7 $$
$$ L = 7^2 = 49 $$

By hand:

$$ \frac{\partial L}{\partial a} = 2(ab+c) \cdot b $$
$$ \frac{\partial L}{\partial a} = 2 \cdot 7 \cdot 3 $$
$$ \frac{\partial L}{\partial a} = 42 $$
$$ \frac{\partial L}{\partial b} = 2(ab+c) \cdot a $$
$$ \frac{\partial L}{\partial b} = 2 \cdot 7 \cdot 2 $$
$$ \frac{\partial L}{\partial b} = 28 $$
$$ \frac{\partial L}{\partial c} = 2(ab+c) \cdot 1 $$
$$ \frac{\partial L}{\partial c} = 14 $$
a = Value(2.0)
b = Value(3.0)
c = Value(1.0)

L = (a * b + c) ** 2
L.backward()

print(a.grad)  # 42.0 ✓
print(b.grad)  # 28.0 ✓
print(c.grad)  # 14.0 ✓

I computed the three gradients from the hand formulas $2(ab+c)b$, $2(ab+c)a$ and $2(ab+c)$. The left case is the one above. In the right case $ab + c = 0$.

Table for a 2, b 3, c 1. The value ab plus c is 7, L is 49, and the gradients are 42, 28 and 14.

$a = 2$, $b = 3$, $c = 1$: the gradients are 42, 28 and 14.

Table for a 2, b minus 3, c 6. The value ab plus c is 0, so L and all three gradients are 0.

$a = 2$, $b = -3$, $c = 6$: $ab + c = 0$, so all three gradients vanish.

Every gradient scales with $2(ab+c)$.

Before I stack layers on top, I run numerical gradient checking on each new op (pow, relu, then the neuron). That way I never debug a training loop when the real bug is a single wrong partial three ops down.


How do I build a neural network on top?

With gradients working, I can build neurons and layers. A single neuron computes $y = \text{ReLU}(w \cdot x + b)$ for a weight vector $w$ and bias $b$.

graph TD
  subgraph "MLP(2, [4, 4, 1])"
    I1((x₁)) & I2((x₂))

    subgraph "Layer 1 — 4 neurons"
      N1((n)) & N2((n)) & N3((n)) & N4((n))
    end

    subgraph "Layer 2 — 4 neurons"
      N5((n)) & N6((n)) & N7((n)) & N8((n))
    end

    subgraph "Output — 1 neuron"
      OUT((y))
    end

    I1 & I2 --> N1 & N2 & N3 & N4
    N1 & N2 & N3 & N4 --> N5 & N6 & N7 & N8
    N5 & N6 & N7 & N8 --> OUT
  end

  style OUT fill:#d4edda,stroke:#009900,color:#111
import random

class Neuron:
    def __init__(self, n_inputs):
        self.w = [Value(random.uniform(-1, 1)) for _ in range(n_inputs)]
        self.b = Value(0.0)

    def __call__(self, x):
        activation = sum(wi * xi for wi, xi in zip(self.w, x)) + self.b
        return activation.relu()

    def parameters(self):
        return self.w + [self.b]


class Layer:
    def __init__(self, n_inputs, n_outputs):
        self.neurons = [Neuron(n_inputs) for _ in range(n_outputs)]

    def __call__(self, x):
        return [n(x) for n in self.neurons]

    def parameters(self):
        return [p for n in self.neurons for p in n.parameters()]


class MLP:
    def __init__(self, n_inputs, layer_sizes):
        sizes = [n_inputs] + layer_sizes
        self.layers = [Layer(sizes[i], sizes[i+1]) for i in range(len(layer_sizes))]

    def __call__(self, x):
        for layer in self.layers:
            x = layer(x)
        return x[0] if len(x) == 1 else x

    def parameters(self):
        return [p for layer in self.layers for p in layer.parameters()]

Each Neuron.__call__ builds a fresh computation graph. Every Value in that graph remembers its parents, and loss.backward() walks the whole graph.


How do I train the network?

XOR is the classic sanity check. It is not linearly separable, so a single neuron fails, but a small MLP with ReLU should learn it.

# XOR dataset
X = [[0,0], [0,1], [1,0], [1,1]]
y = [0,     1,     1,     0    ]

model = MLP(2, [4, 4, 1])

for step in range(100):
    # Forward pass
    preds = [model(x) for x in X]

    # Mean squared error loss
    loss = sum((pred - target)**2 for pred, target in zip(preds, y))

    # Backward pass
    for p in model.parameters():
        p.grad = 0.0   # zero gradients before backward
    loss.backward()

    # Gradient descent
    for p in model.parameters():
        p.data -= 0.05 * p.grad

    if step % 10 == 0:
        print(f"step {step:3d} | loss {loss.data:.4f}")

Output (approximate):

step   0 | loss 2.1847
step  10 | loss 1.3204
step  20 | loss 0.8951
step  30 | loss 0.5623
...
step  90 | loss 0.0412

The loss drops, so the engine is computing usable gradients. It applies the same chain rule I traced by hand on $z = (x+y) \cdot y$, now inside a training loop.


How is PyTorch different from this engine?

My engine works on scalars. PyTorch operates on tensors (n-dimensional arrays): the same idea, vectorised with C++/CUDA underneath. The concepts map directly:

graph LR
  subgraph "Our engine"
    V["Value\n(scalar float)"] --> VD["value.data"]
    V --> VG["value.grad"]
    V --> VB["value._backward\n(closure)"]
    V --> VP["value._prev\n(set of parents)"]
  end
  subgraph "PyTorch"
    T["Tensor\n(n-dim array)"] --> TD["tensor.data"]
    T --> TG["tensor.grad"]
    T --> TB["tensor.grad_fn\n(C++ Function obj)"]
    T --> TP["grad_fn.next_functions\n(tuple of parents)"]
  end
  V -. "same concept\nscaled up" .-> T
  style V fill:#fff,stroke:#dddddd,color:#111
  style T fill:#d4edda,stroke:#009900,color:#111
Our engine PyTorch
Value.data tensor.data
Value.grad tensor.grad
Value._backward tensor.grad_fn
Value._prev tensor.grad_fn.next_functions
loss.backward() loss.backward()
p.grad = 0.0 optimizer.zero_grad()

The key difference is that PyTorch’s DAG stores functions (AddBackward, MulBackward, etc.) rather than values. The topological traversal and chain rule application are the same.

When I eventually move to tensors in my own projects, I still gradient-check every custom backward() before I trust it at scale.


What pitfalls should I watch for?

Forgetting to zero gradients. Because grad accumulates with +=, calling .backward() twice without zeroing inflates every gradient, and not by a clean factor of two. On $z = (x+y) \cdot y$, the second call leaves x.grad at 9 and y.grad at 19, because the stale s.grad is added again. I always zero before each backward pass. I tabulated how the gradients drift as I call it more times.

Table of x.grad and y.grad after one to five calls to backward without zeroing. One call gives 3 and 8. Two calls give 9 and 19. Five calls give 45 and 70.
Each extra call adds $3n$ to `x.grad`, because $\bar{s}$ also carries over.

Reusing leaf nodes. If y appears in both $x+y$ and $s \cdot y$, its gradient receives contributions from both branches via accumulation. That is the $\bar{y} = 5 + 3$ pattern, and it is intended behaviour.

In-place operations. Modifying .data directly after using a value in a computation breaks the graph. Never do v.data = new_val mid-computation.

Circular graphs. The topological sort assumes a DAG. Feedback loops are not supported. Recurrent networks work because they unroll through time into a DAG.

Skipping verification. The hand maths on $z = (x+y) \cdot y$ is a required check. If I cannot get $\partial z / \partial y = 8$ by hand, a wrong __mul__ or __add__ may go unnoticed until the loss curve looks “almost right.” Numerical gradient checking catches that in seconds.


How does a longer backward pass look by hand?

The $z = (x+y) \cdot y$ example covered add, multiply, and accumulation. Here is a second complete pass with a power node: the same ingredients, more nodes.

Let $a=2$, $b=-3$, $c=10$, and

$$ d = a \cdot b $$
$$ d = 2 \cdot (-3) $$
$$ d = -6 $$
$$ e = d + c $$
$$ e = -6 + 10 $$
$$ e = 4 $$
$$ L = e^2 $$
$$ L = 4^2 $$
$$ L = 16 $$

Backward — every gradient on its own line.

Seed:

$$ \frac{\partial L}{\partial L} = 1 $$

Power node $L = e^2$:

$$ \frac{\partial L}{\partial e} = 2e $$
$$ \frac{\partial L}{\partial e} = 2 \cdot 4 $$
$$ \frac{\partial L}{\partial e} = 8 $$

Add node $e = d + c$:

$$ \frac{\partial L}{\partial d} = \frac{\partial L}{\partial e} \cdot \frac{\partial e}{\partial d} $$
$$ \frac{\partial L}{\partial d} = 8 \cdot 1 $$
$$ \frac{\partial L}{\partial d} = 8 $$
$$ \frac{\partial L}{\partial c} = \frac{\partial L}{\partial e} \cdot \frac{\partial e}{\partial c} $$
$$ \frac{\partial L}{\partial c} = 8 \cdot 1 $$
$$ \frac{\partial L}{\partial c} = 8 $$

Multiply node $d = a \cdot b$:

$$ \frac{\partial L}{\partial a} = \frac{\partial L}{\partial d} \cdot \frac{\partial d}{\partial a} $$
$$ \frac{\partial L}{\partial a} = 8 \cdot b $$
$$ \frac{\partial L}{\partial a} = 8 \cdot (-3) $$
$$ \frac{\partial L}{\partial a} = -24 $$
$$ \frac{\partial L}{\partial b} = \frac{\partial L}{\partial d} \cdot \frac{\partial d}{\partial b} $$
$$ \frac{\partial L}{\partial b} = 8 \cdot a $$
$$ \frac{\partial L}{\partial b} = 8 \cdot 2 $$
$$ \frac{\partial L}{\partial b} = 16 $$

Reverse mode suits neural networks because one scalar loss yields gradients for millions of parameters in roughly one backward traversal. Its main cost is memory: forward intermediates must be retained or recomputed.


Order Post What it covers
1 Linear algebra foundations Vectors, matrices, eigenvalues — the notation autograd operates on
2 This post Chain rule, computation graphs, Value, backprop
3 Numerical gradient checking Finite-difference verification of every backward()
4 Byte pair encoding Tokenizer pipeline before the model
5 Token embeddings Discrete tokens → continuous vectors
6 Positional encoding Order information in transformers

The whole autograd idea in one sentence: wrap every number in an object that remembers how it was created, then walk the creation history in reverse, applying the chain rule.

I did not understand .backward() until I could get $\partial z / \partial y = 8$ on paper for $z = (x+y) \cdot y$. After that, the 82-line engine was straightforward to write.


Watch it built live

Andrej Karpathy’s lecture inspired this post. In it he builds micrograd from scratch in real time:


References

Primary sources — code and implementations

  1. Karpathy, A. (2020). micrograd: A tiny scalar-valued autograd engine. GitHub. https://github.com/karpathy/micrograd

  2. Karpathy, A. (2022). The spelled-out intro to neural networks and backpropagation: building micrograd. YouTube, Neural Networks: Zero to Hero series. https://www.youtube.com/watch?v=VMj-3S1tku0

  3. Smit, J. (2022). DIY auto-grad engine: A step-by-step guide to calculating derivatives automatically. jordismit.com. https://jordismit.com/blog/diy-auto-grad-engine-a-step-by-step-guide-to-calculating-derivatives-automatically/

  4. Loh, J. (2024). Reverse-mode autodiff from scratch. jlohding.github.io. https://jlohding.github.io/posts/autodiff/

  5. Sankhla, B. (2023). DIY Deep Learning: Crafting your own autograd engine from scratch. Medium / SFU CSPMP. https://medium.com/sfu-cspmp/diy-deep-learning-crafting-your-own-autograd-engine-from-scratch-for-effortless-backpropagation-ddab167faaf5

Official documentation

  1. PyTorch. Automatic Differentiation with torch.autograd. PyTorch Tutorials. https://docs.pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html

  2. JAX. Forward- and reverse-mode autodiff in JAX. JAX Documentation. https://docs.jax.dev/en/latest/jacobian-vector-products.html

  3. Dive into Deep Learning. 2.5 Automatic Differentiation. d2l.ai. https://d2l.ai/chapter_preliminaries/autograd.html

Theory

  1. Wikipedia. Automatic differentiation. Wikimedia Foundation. https://en.wikipedia.org/wiki/Automatic_differentiation

  2. University of Amsterdam. Module 3: The Reverse Mode of Automatic Differentiation. auto-ed.readthedocs.io. https://auto-ed.readthedocs.io/en/latest/mod3.html

Support the writing

If this post helped, a coffee keeps the deep dives coming.

Buy me a coffee