AI & ML Quick Path Progress

Part 4: Deep learning Lesson 2 of 15 in this part

Backpropagation and automatic differentiation

What you will learn. Follow the chain rule through a small calculation by hand, see how automatic differentiation does the same thing for any model, and recognise why gradients can vanish in deep stacks of layers.

  • Intermediate
  • Study time about 75 min
  • Maths Some algebra
  • Python: example run
  • C#: compiled and run

Before this lesson: Neural networks in plain words. Already know these? Carry on.

In plain words

Training a network means asking, for every weight: if I nudge this weight up a little, does the loss go up or down, and by how much? That answer is the gradient. A network may have millions of weights, so we need a fast way to get all of them at once. Backpropagation is that way. First run the network forward and record every step. Then walk backwards from the loss, and at each step use the chain rule: how much the output changes with this step, times how much this step changes with the one before. Automatic differentiation (autograd) is the same idea built into libraries such as PyTorch, so you never write the gradients yourself. A loss that must pass its signal back through many layers can fade to almost nothing. That is the vanishing gradient problem, and it explains why activation choice and skip connections matter.

Diagram Forward pass, then backward pass
  1. InputForward: compute each step and remember itFrom inputs to the loss
  2. ProcessBackward: start with gradient one at the lossOne unit of blame
  3. ModelMultiply by each step's slope going backThe chain rule
  4. OutputRead the gradient at every weightUsed to update the weights

The forward pass records. The backward pass uses the record to hand out blame.

Diagram Three ways to get a gradient

By hand

  • Write the chain rule yourself
  • Good for learning, error-prone for big models

Numerical nudge

  • Change a weight a tiny amount and watch the loss
  • Slow: one run per weight

Automatic differentiation

  • Library records steps, applies the chain rule
  • Fast and exact, used for training

Use the numerical check to test the other two. Never use it to train.

Diagram Why gradients vanish
  1. InputThe loss sends back a gradientStrength one at the end
  2. RiskEach sigmoid layer scales it by under a quarterThe signal shrinks
  3. ProblemAfter many layers almost nothing is leftEarly layers barely learn

Each sigmoid layer passes on at most a quarter of the gradient.

Real-world example

An engineer builds a deep network for a language task and finds that its first layers never improve, while the last layers learn quickly. She prints the size of the gradient at each layer, and it shrinks by a large factor at every step backwards. The cause is a stack of sigmoid layers. Switching to ReLU layers with skip connections, which pass the gradient along without shrinking it, fixes the problem. Her check was a printout of gradient sizes, and the explanation is the chain rule.

How it works

  1. Forward pass. Compute the output of every step and keep the values.
  2. Local slopes. Each step knows its own slope: how its output changes if one input changes. For a sum the slope is 1. For a product it is the other factor. For a sigmoid it is the output times one minus the output.
  3. Backward pass. Start at the loss with a gradient of 1. Going back through each step, multiply the gradient so far by that step's slope. Where several paths meet, add their gradients.
  4. Update. Use each weight's gradient to move it a small step downhill.

Live simulation

Backpropagation is the chain rule applied to a calculation graph. Here is the smallest graph that still shows every kind of step.

Simulation Backpropagation on one neuron

The question. How does a network work out, for every weight, which way to change it?

What to watch

  • The forward pass fills each box with a value, from x and w on the left to the loss on the right.
  • Each backward step multiplies the gradient coming from the right by the local slope of one box. An add passes the gradient on; a product scales it by the other input.
  • The nudge check changes w by a tiny amount and measures the loss change directly. It should match the backward result.

This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.

Try this

  • Show all four backward steps and confirm that the nudge check agrees with the gradient of w.
  • With x = 2 and target y = 1, press "Update w and b once" until the loss is below 0.02.

What this leaves out. One neuron and one training example, where a real network has many layers and millions of weights. Frameworks work out these gradients automatically with the same rule; this page shows the rule by hand.

Python program that prints the same headline numbers: examples/sim-backprop/graph.py.

Python example

Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.

A tiny autograd engine in about 30 lines. Each number remembers how it was made, and backward() applies the chain rule. It computes the same gradients as the hand calculation, checks one by a nudge, and then shows how the gradient fades through stacks of sigmoid layers. It needs no libraries. PyTorch was not installed for this lesson, so the PyTorch equivalent (loss.backward()) is described, not run.

Python — autograd.py
"""A tiny automatic-differentiation engine (about 30 lines) and a vanishing-gradient demonstration.

Each Value remembers how it was made, so calling backward() can walk back through the calculation and
apply the chain rule. No libraries are needed.
Run:  python autograd.py
"""
import math

class Value:
    def __init__(self, data, parents=(), local=()):
        self.data, self.grad = data, 0.0
        self.parents, self.local = parents, local        # local[i] = d(this) / d(parents[i])
    def __add__(self, o):
        o = o if isinstance(o, Value) else Value(o)
        return Value(self.data + o.data, (self, o), (1.0, 1.0))
    def __mul__(self, o):
        o = o if isinstance(o, Value) else Value(o)
        return Value(self.data * o.data, (self, o), (o.data, self.data))
    def __sub__(self, o):
        return self + (o * -1.0 if isinstance(o, Value) else Value(-o))
    def sigmoid(self):
        s = 1 / (1 + math.exp(-self.data))
        return Value(s, (self,), (s * (1 - s),))
    def backward(self):
        order, seen = [], set()
        def visit(v):                                    # parents before children
            if id(v) not in seen:
                seen.add(id(v))
                for p in v.parents:
                    visit(p)
                order.append(v)
        visit(self)
        self.grad = 1.0
        for v in reversed(order):                        # the chain rule, one step at a time
            for p, d in zip(v.parents, v.local):
                p.grad += d * v.grad

def loss_of(w, b):
    x, y = 2.0, 1.0
    err = (w * x + b).sigmoid() - y
    return err * err

w, b = Value(0.5), Value(-0.3)
loss = loss_of(w, b)
loss.backward()
print("loss %.4f  d loss / d w = %.4f  d loss / d b = %.4f" % (loss.data, w.grad, b.grad))
h = 1e-6                                                 # check against a tiny nudge of w
numeric = (loss_of(Value(0.5 + h), Value(-0.3)).data - loss_of(Value(0.5 - h), Value(-0.3)).data) / (2 * h)
print("numerical check of d loss / d w: %.4f" % numeric)

print("layers  gradient reaching the first layer (sigmoid layers, weight 1)  (ReLU layers, weight 1)")
for n in (1, 5, 10, 20):
    g_sig, z = 1.0, 0.5
    for _ in range(n):
        s = 1 / (1 + math.exp(-z))
        g_sig *= s * (1 - s)                             # each sigmoid layer multiplies the gradient by at most 0.25
        z = s
    print("%6d  %57.2e  %22.1f" % (n, g_sig, 1.0))
Output — Output of autograd.py (executed)
loss 0.1101  d loss / d w = -0.2943  d loss / d b = -0.1471
numerical check of d loss / d w: -0.2943
layers  gradient reaching the first layer (sigmoid layers, weight 1)  (ReLU layers, weight 1)
     1                                                   2.35e-01                     1.0
     5                                                   6.08e-04                     1.0
    10                                                   3.48e-07                     1.0
    20                                                   1.14e-13                     1.0

The loss is 0.1101 and the gradients are -0.2943 and -0.1471, matching the hand calculation. The numerical check gives -0.2943 as well. In the last table, a gradient of strength 1 reaching one sigmoid layer shrinks to 0.235. After 5 layers it is 0.000608, after 10 it is 0.000000348, and after 20 it is about 0.000000000000114. With ReLU layers and weight 1 it stays at 1.0.

A last remark on what networks can represent. The universal approximation result says that a network with one hidden layer and enough neurons can imitate any reasonable function to any accuracy. It tells you the model class is rich enough, not that training will find the right weights or that the network will be small.

.NET (C#) example

C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.

TorchSharp offers autograd in C# in the same way PyTorch does, but it needs a native library. To show the idea without it, this version builds the same tiny engine from scratch in C#. It needs no packages and prints the same numbers as the Python run.

C# — Program.cs
// A tiny automatic-differentiation engine in C#. Same numbers as autograd.py. No packages.
using System.Globalization;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

Value LossOf(Value w, Value b)
{
    double x = 2.0, y = 1.0;
    Value err = (w * x + b).Sigmoid() - y;
    return err * err;
}

var w = new Value(0.5); var b = new Value(-0.3);
Value loss = LossOf(w, b);
loss.Backward();
Console.WriteLine($"loss {loss.Data:F4}  d loss / d w = {w.Grad:F4}  d loss / d b = {b.Grad:F4}");
double h = 1e-6;                                                  // check against a tiny nudge of w
double numeric = (LossOf(new Value(0.5 + h), new Value(-0.3)).Data - LossOf(new Value(0.5 - h), new Value(-0.3)).Data) / (2 * h);
Console.WriteLine($"numerical check of d loss / d w: {numeric:F4}");

Console.WriteLine("layers  gradient reaching the first layer (sigmoid layers, weight 1)  (ReLU layers, weight 1)");
foreach (int n in new[] { 1, 5, 10, 20 })
{
    double gSig = 1.0, z = 0.5;
    for (int i = 0; i < n; i++)
    {
        double s = 1 / (1 + Math.Exp(-z));
        gSig *= s * (1 - s);                                      // each sigmoid layer multiplies the gradient by at most 0.25
        z = s;
    }
    Console.WriteLine($"{n,6}  {gSig.ToString("0.00e+00").PadLeft(57)}  {1.0.ToString("F1").PadLeft(22)}");
}

class Value
{
    public double Data, Grad;
    readonly Value[] parents; readonly double[] local;             // local[i] = d(this) / d(parents[i])
    public Value(double data, Value[]? parents = null, double[]? local = null)
    { Data = data; this.parents = parents ?? Array.Empty<Value>(); this.local = local ?? Array.Empty<double>(); }
    public static Value operator +(Value a, Value b) => new(a.Data + b.Data, new[] { a, b }, new[] { 1.0, 1.0 });
    public static Value operator *(Value a, Value b) => new(a.Data * b.Data, new[] { a, b }, new[] { b.Data, a.Data });
    public static Value operator +(Value a, double b) => a + new Value(b);
    public static Value operator *(Value a, double b) => a * new Value(b);
    public static Value operator -(Value a, double b) => a + new Value(-b);
    public Value Sigmoid() { double s = 1 / (1 + Math.Exp(-Data)); return new Value(s, new[] { this }, new[] { s * (1 - s) }); }
    public void Backward()
    {
        var order = new List<Value>(); var seen = new HashSet<Value>();
        void Visit(Value v)                                       // parents before children
        {
            if (!seen.Add(v)) return;
            foreach (var p in v.parents) Visit(p);
            order.Add(v);
        }
        Visit(this);
        Grad = 1.0;
        for (int i = order.Count - 1; i >= 0; i--)                // the chain rule, one step at a time
            for (int k = 0; k < order[i].parents.Length; k++)
                order[i].parents[k].Grad += order[i].local[k] * order[i].Grad;
    }
}

Maths behind it

  • The chain rule (calculus)
  • Derivative of a sum, a product and the sigmoid (calculus)
  • The numerical derivative as a difference quotient (calculus)
  • Repeated multiplication of numbers below 1 (arithmetic)

Common mistakes

  • Forgetting that a weight used in two places gets the sum of two gradients.
  • Training with a numerical gradient. It is only for checking.
  • Using many sigmoid or tanh layers in a deep network and wondering why early layers do not learn.
  • Not checking a custom layer's gradient against a nudge test.
  • Reading "universal approximation" as a promise that a small network will train well.

Quick check

  1. In one sentence, what does backpropagation compute?

    Show answer

    The gradient of the loss with respect to every weight, by running the chain rule backwards through the steps recorded in the forward pass.

  2. Why is the numerical nudge test used only for checking?

    Show answer

    It needs extra runs for each weight, so it is far too slow for training, but it is a reliable way to catch bugs in a hand-written gradient.

  3. A network of 20 sigmoid layers trains very slowly in its early layers. What is the likely cause, and what can you try?

    Show answer

    Vanishing gradients: each sigmoid passes on at most a quarter of the gradient. Use ReLU-type activations, skip connections or normalisation layers.

  4. A weight is used in two places in the network. How is its gradient found?

    Show answer

    Work out the gradient along each path and add them together.

Completion

Evidence of completion. A hand-worked gradient for a one-neuron model that matches a program's gradient and a tiny numerical check, plus a sentence on why sigmoid stacks lose gradient.

Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.