TactivergeLite

Lite / Independent Engineering Research / Week 23

Gradient descent on a one-neuron network

A network with millions of weights learns by asking one question of each weight: “if I nudge you, does the error go up or down?” How is that question answered?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Training is just repeated small corrections. The perceptron's rule only works for a hard step; real networks use smooth activations so that calculus can say exactly how much each weight is to blame. Do it once by hand for one neuron and backpropagation stops being magic: it is the chain rule, applied layer by layer.

Predict

You double the learning rate of a model that was training slowly but steadily. What is the risk?

  1. None: it will just train twice as fast
  2. The steps may overshoot the minimum and the loss can grow
  3. The model will forget its data
Guess first, then reveal

Larger steps can jump over the bottom of the loss valley. For the loss (w − 3)² any learning rate above 1 makes each step land further from the minimum than the last, and the weights blow up.

Discover

Start at w₀ = −1. Take steps with η = 0.1, then 0.5, then 0.9, then 1.05. For each, count steps until the readout says converged, or note when it never does.

What the interactive shows (text version)

The gradient-descent diagram plots the loss L(w) = (w − 3)² + 1 as a U-shaped curve with its lowest point at w = 3. Each press of Take one step moves w by −η × 2(w − 3) and marks the new point. From w₀ = −1: with η = 0.1 the points creep towards 3; with η = 0.5 the first step lands exactly on 3; with η = 0.9 they zig-zag across the minimum but settle; with η = 1.05 each zig-zag is wider and the readout warns that training is diverging.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch for the cost as a landscape and the gradient as the direction of steepest climb; training walks the other way.

The clearest picture of rolling downhill on a cost surface, and of why a step can land you in a local valley.

“Gradient descent, how neural networks learn | Deep Learning Chapter 2” by 3Blue1Brown (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

A smooth neuron and its loss

Replace the hard step with the sigmoid σ(z) = 1/(1 + e^(−z)), which glides from 0 to 1. One neuron computes y = σ(wx + b). To measure how wrong it is on an example with target t, use the squared-error loss L = ½(y − t)². The ½ is only there to cancel a 2 when we differentiate. Training means choosing w and b to make L small across the data.

Blame by the chain rule

L depends on w only through z and y, so ∂L/∂w = (∂L/∂y)(∂y/∂z)(∂z/∂w) = (y − t) · y(1 − y) · x. Each factor has a meaning: how wrong the output is, how sensitive the sigmoid is at this point (at most 0.25, at z = 0), and how big the input was. A zero input gets no blame. The same idea, repeated backwards through layers, is backpropagation.

One step downhill

Gradient descent updates w ← w − η·∂L/∂w: move against the gradient, scaled by the learning rate η. Take w = 0.5, b = 0, x = 2, t = 1. Then z = 1, y ≈ 0.7311, ∂L/∂w ≈ (−0.2689)(0.1966)(2) ≈ −0.1058. With η = 1, w becomes ≈ 0.6058 and the loss falls from ≈ 0.0362 to ≈ 0.0263. Negative gradient, so w went up.

Choosing the learning rate

For the bowl L(w) = (w − 3)², each step multiplies the distance from the minimum by (1 − 2η). With η = 0.1 the distance shrinks by 0.8 per step: safe but slow. η = 0.5 lands exactly on the minimum. Between 0.5 and 1 it overshoots but still settles; above 1 the distance grows every step and training diverges. Real loss surfaces are not perfect bowls, which is why η is tuned and often decreased over time.

Explain it another way

Intuitive

You are on a foggy hillside and want the valley floor. You can only feel the slope under your feet, so you step downhill, a distance proportional to how steep it is. Tiny steps take forever; giant leaps can carry you clean over the valley and up the other side.

Technical

Full-batch gradient descent averages ∂L/∂w over all examples before each update; stochastic (SGD) and mini-batch variants use one or a few examples per step, trading noise for speed. For a quadratic with curvature λ, steps converge if 0 < η < 2/λ. Momentum and adaptive methods (such as Adam) change the effective step per weight.

Mathematical

y = σ(z), z = wx + b, L = ½(y − t)². σ'(z) = σ(z)(1 − σ(z)). Chain rule: ∂L/∂w = (y − t)σ'(z)x, ∂L/∂b = (y − t)σ'(z). For L = a(w − c)², dL/dw = 2a(w − c) and w − c is multiplied by (1 − 2aη) each step, so convergence needs |1 − 2aη| < 1.

Applied

On-device training is rare because each step needs the forward pass, the gradients and memory for both, but fine-tuning only the last layer (a few hundred weights) is feasible on a microcontroller. Most embedded ML trains on a computer with exactly this procedure, then ships only the final weights.

Derivation: ∂L/∂w for one sigmoid neuron
  1. Define z = wx + b, y = σ(z) = 1/(1 + e^(−z)), and L = ½(y − t)².

  2. Outer link: ∂L/∂y = (y − t), because the ½ cancels the 2 from differentiating a square.

  3. Middle link: dσ/dz = e^(−z)/(1 + e^(−z))² = σ(z)·(1 − σ(z)) = y(1 − y).

  4. Inner link: ∂z/∂w = x (and ∂z/∂b = 1).

  5. Multiply the links (chain rule): ∂L/∂w = (y − t) · y(1 − y) · x, and ∂L/∂b = (y − t) · y(1 − y).

  6. Update: w ← w − η(y − t)y(1 − y)x. Check with numbers: w = 0.5, b = 0, x = 2, t = 1 gives ∂L/∂w ≈ −0.1058; η = 1 gives w ≈ 0.6058, and the loss drops from ≈ 0.0362 to ≈ 0.0263.

Worked example 1

For L(w) = (w − 3)², start at w = 5 with η = 0.25. Take one step.

dL/dw = 2(5 − 3) = 4. New w = 5 − 0.25 × 4 = 4. The distance to the minimum halved, from 2 to 1, because 1 − 2η = 0.5.

Worked example 2

A sigmoid neuron has z = 0. What is σ'(z), and why does it matter?

σ(0) = 0.5, so σ'(0) = 0.5 × 0.5 = 0.25: the largest slope the sigmoid ever has. Far from z = 0 the slope tends to 0, so a saturated neuron barely learns (the vanishing-gradient problem).

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 4. During training the loss jumps around and then grows larger every epoch. What is the most likely cause?
Practice 5. For one neuron, ∂L/∂w = (y − t)·y(1 − y)·x. An example has input x = 0. How much does it change w?

Apply it

Train the one-neuron example by hand for three steps (w = 0.5, b = 0, x = 2, t = 1, η = 1), updating both w and b with ∂L/∂b = (y − t)y(1 − y). Tabulate w, b, y and L after each step and confirm the loss falls every time. Then repeat the first step with η = 20 and describe what happens to y.

Reflect

In your own words: what does each of the three factors in (y − t)·y(1 − y)·x contribute to how much a weight is blamed for an error?

Write your answer on paper or in your own notes. Lite never stores what you write.

← The perceptron: one neuron draws one lineCross-validation: grading a model fairly →