Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Gradient descent
- Repeatedly moving parameters a small step against the slope of the loss.
- Learning rate Also η
- How far each step moves, as a multiple of the slope.
- Curvature
- How quickly the slope changes; for this loss it is a = (2/n)Σx², and it sets which learning rates are safe.
- Oscillation
- Overshooting the minimum on every step, so the error changes sign; it may still shrink.
- Divergence
- Steps so large that the error grows each time instead of shrinking.
Why this matters
Many models are fitted by stepping its parameters downhill on a loss. The learning rate is the one setting nearly everyone tunes by trial, and its safe range depends on the data: a rate that works for one dataset diverges on another. Knowing why turns a guess into a check.
This lesson assumes derivatives and geometric sequences.
What a learning rate controls
Gradient descent updates a model parameter by subtracting its loss derivative multiplied by a learning rate, η. The derivative gives the local slope; η sets how far that slope moves the parameter on each step.
For the one-parameter quadratic loss used here, curvature measures how quickly the slope changes as the parameter moves. The same learning rate can produce different update behavior on data with different curvature. The decision is whether repeated updates approach the best fit within the available number of steps.
01Step against the slope
Step against the slope
A phone’s battery estimate personalises itself overnight: it refits how much battery an hour of screen time costs this owner, with a few steps of gradient descent. A release raised the learning rate to make the fit converge faster. On most phones it did. On some, the estimate now swings wildly from night to night.
The model says drain ≈ w × screen-on hours. Its loss is the mean squared error over the last days, and the slope of that loss with respect to w is a·w − c, where a = (2/n)Σx² and c = (2/n)Σxy. Gradient descent repeats w ← w − η(a·w − c). For this owner’s four days, Σx² = 30 and Σxy = 253, so a = 15, c = 126.5 and the best fit is w = c/a = 8.433. Here x is hours, y is battery percentage points and w is percentage points per hour. Assume at least one nonzero x, so a > 0.
const days = [
[1, 9],
[2, 17],
[3, 26],
[4, 33]
]; // screen-on hours, battery percent used
const a = (2 / days.length) * days.reduce((sum, [x]) => sum + x! * x!, 0);
const c = (2 / days.length) * days.reduce((sum, [x, y]) => sum + x! * y!, 0);
for (const eta of [0.01, 0.05, 0.1, 0.14]) {
let w = 5;
for (let k = 0; k < 10; k++) w -= eta * (a * w - c);
console.log(eta, (1 - eta * a).toFixed(2), w.toFixed(3)); // rate, factor per step, w after 10 steps
}
02Each step multiplies the error
Each step multiplies the error
Subtract the best fit from both sides of the update and the error w − w* is multiplied by r = 1 − η·a on every step. The middle column of the example is r. When r is between 0 and 1 the error shrinks steadily; between −1 and 0 it changes sign each step but still shrinks; at −1 it flips sign without shrinking; below −1 it grows. The lab’s label diverges includes both nonconverging cases.
| η | 1 − η·a | Behaviour | Steps |
|---|---|---|---|
| 0.01 | 0.85 | slow and smooth | 51 |
| 0.05 | 0.25 | fast and smooth | 6 |
| 0.1 | −0.50 | oscillating | 12 |
| 0.14 | −1.10 | diverges | never |
TensorFlow.js optimizers such as tf.train.sgd take the learning rate as their only required argument; nothing in them checks it against your data.
03The stability bound
The stability bound
For positive rates and positive curvature, the error shrinks while |1 − η·a| < 1, that is 0 < η < 2/a. For this owner that is 0.1333. An owner who uses the phone for longer sessions has larger x values, larger a and a smaller safe rate, which is why the release diverged on some phones and not others.
04Fastest is not largest
Fastest is not largest
The error shrinks fastest when |1 − η·a| is smallest, which is near η = 1/a. Among 0.06, 0.075 and 0.12, the first takes 4 steps and the last 37, though all three are stable: 0.12 overshoots nearly as far as it moves.
05One direction, many directions
One direction, many directions
06Tune the nightly refit
Tune the nightly refit
The nightly refit first needs the mean-loss update to match its configured rate. Use the error factor to classify that rate for this owner’s data, then compare candidate step counts against tonight’s budget. A tie chooses the numerically smaller rate even if candidates arrive out of order.
A battery-life refit
Run the refit, classify a learning rate, then pick tonight’s rate.
In the frontend A rate check on a tuning screen in Next.js and Svelte Optional
An internal tuning screen can compute a for the selected data and refuse rates at or above 2/a, showing the expected steps for the rest.
"use client";
// An internal tuning screen: refuse a learning rate above 2/a for the selected owner's data.
import { useState } from "react";
export function RateInput({ days }: { days: [number, number][] }) {
const a = (2 / days.length) * days.reduce((sum, [x]) => sum + x * x, 0);
const [eta, setEta] = useState(0.05);
const factor = Math.abs(1 - eta * a); // the error is multiplied by this each step
const steps =
factor === 0
? 1
: factor > 0 && factor < 1
? Math.ceil(Math.log(1e-3) / Math.log(factor))
: Infinity;
return (
<label>
Learning rate{" "}
<input
type="number"
step="0.01"
value={eta}
onChange={(e) => setEta(Number(e.target.value))}
/>
{eta <= 0 || eta * a >= 2 ? (
<span> Use a positive rate below {(2 / a).toFixed(4)}.</span>
) : (
<span> About {steps} steps to shrink the error 1,000-fold.</span>
)}
</label>
);
}
Check this idea later
Sources
- Goodfellow, Bengio and Courville: numerical computation (gradient-based optimization)
- Stanford CS231n: optimization
Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The battery model and the owner’s days are a worked model.
Sign in to keep your read marks on every device. Sign in
Was this helpful?