A formula like distance = speed × time holds three letters, and any one of them can be the unknown. Rather than learning three formulas, learn one and rearrange it. It's the balance from the last lesson again, with letters on the pans instead of numbers. In machine learning the same skill turns "how many steps will this take?" into "how many examples do I need for this many steps?" Along the way, you'll write each rearranged formula as a Python function and check every pair with a round trip, a test that catches a wrong rearrangement every time. Engineers use the same habit before trusting any formula they've rearranged by hand.
What you'll be able to do
Make any letter of a formula its subject, with the four moves done to both sides
Rearrange d = s·t, y = mx + b and a = c/n for each of their letters
Write one function per subject in Python, and check each pair by a round trip
Rearrange the formulas for accuracy and training steps to answer planning questions
Quick check
Have a guess before you start. A car covers distance d = s × t. Which formula gives the time t?
t = d ÷ s
t = s ÷ d
t = d × s
t = d − s
Show the answer
t = d ÷ s
t is multiplied by s, so divide both sides by s: d ÷ s = t. 150 miles at 50 miles an hour takes 3 hours.
The math: any letter can be the subject
Purpose. One formula, any unknown.
The subject of a formula is the letter on its own on one side: in d = s·t, it's d. To make another letter the subject, treat the formula as a balance and use the four moves, always on both sides:
add the same thing to both sides
take the same thing from both sides
multiply both sides by the same thing (not 0)
divide both sides by the same thing (not 0)
It's exactly what you did with numbers in the last lesson. The letters just stay as letters.
Quick check
From the last lesson: solve 3s = 150.
s = 450
s = 50
s = 147
s = 153
Show the answer
s = 50
Divide both sides by 3: s = 50. With letters, the same move turns d = s·t into s = d/t.
Watch the balance make s the subject of 150 = s × 3, the formula d = s·t with d = 150 and t = 3. The one move, dividing both pans by 3, is the same move that turns d = s·t into s = d/t for every d and t.
In the picture, the pans keep the letters: s × t on the left, d on the right. The line above the beam carries this flow's numbers. Watch the move work on the letters, then the check put the numbers back in for them.
Try it now
Change the time and the distance and press Use these numbers: the move is always the same, divide by t.
Symbols and formula. Three everyday formulas, each rearranged for every letter:
d=st⇒s=td,t=sdy=mx+b⇒x=my−b,b=y−mx
In plain words: undo whatever is done to the letter you want, in reverse order, on both sides.
And for a = c/n, where c is divided by n: multiply both sides by n to get c = a·n, then divide by a to get n = c/a.
A check that always works. Put numbers in. If d = 150, s = 50 and t = 3 fit the first form, they must fit every rearranged form too: 150/3 = 50 and 150/50 = 3. A rearrangement that breaks this has a slip.
Quick check
Rearrange y = mx + b to make x the subject.
x = (y + b) / m
x = y / m − b
x = m(y − b)
x = (y − b) / m
Show the answer
x = (y − b) / m
x was multiplied by m, then b was added. Undo in reverse: take b from both sides, then divide by m.
Quick check
In a = c/n, what is n?
n = c − a
n = a / c
n = c / a
n = a × c
Show the answer
n = c / a
Multiply both sides by n: a·n = c. Then divide both by a: n = c/a. With a = 4 and c = 12, n = 3.
In your own words
In your own words: why does rearranging a formula save you from learning three formulas?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: one function per subject
Purpose. In code, each rearrangement becomes its own function, and the functions check each other.
defdistance(s, t):
return s * t
defspeed(d, t):
return d / t
deftime(d, s):
return d / s
print(speed(distance(50, 3), 3), time(distance(50, 3), 50))
It runs right here in your browser, with nothing to install.
The last line is a round trip: work out a distance from s = 50 and t = 3, then feed it back into the rearranged formulas. You get back 50 and 3, where you started. If a rearrangement had a slip, the round trip would land somewhere else.
A round trip, one line at a time. Here is the round trip for s again, split over two lines, with the flow's numbers. distance turns s = 50 and t = 3 into d, and speed turns d and t back into s. Press Next to run one line at a time, and watch the number distance hands back go into speed.
The same round trip for y = mx + b. Go forward from x to y, then back from y to x:
defy_of(x, m, b):
return m * x + b
defx_of(y, m, b):
return (y - b) / m
print(x_of(y_of(4, 2.5, 1), 2.5, 1))
A named mistake: forgetting the brackets. Written as y - b / m, Python divides b alone, because dividing comes before subtracting. The round trip catches it at once: it no longer comes back to 4.
Worked example
Worked example: Fahrenheit to Celsius and back
F = 9C/5 + 32. Rearrange for C, write both directions, and check with a round trip.
To get C, undo F's steps in reverse: take 32 away, then multiply by 5, then divide by 9. The first line shows 25 degrees Celsius is 77.0 Fahrenheit, and back again. The round trip returns 25.0. The last number shows a rearranged formula working on a fresh input: 100 Fahrenheit is about 37.8 Celsius, a hot day.
Quick check
What does this print?
defx_of(y, m, b):
return (y - b) / m
print(x_of(18, 2, 3))
Which line is a round-trip check for speed(d, t) = d / t?
print(distance(50, 3))
print(speed(3, 50))
print(speed(distance(50, 3), 3))
print(speed(50, 3))
Show the answer
print(speed(distance(50, 3), 3))
Go forward with distance, then back with speed: getting 50 again shows the rearrangement is right.
When the letter appears twice. Sometimes the letter you want shows up in two places, as in y = x + x/4. Gather it first: x + x/4 is 5x/4, so y = 5x/4, and then x = 4y/5. Rearranging is always the same few moves, but gathering the letter into one place has to come first. When a letter can't be gathered, as in y = x + x², no simple rearrangement exists, and the last lesson's guess, check and adjust takes over. Computers solve many real problems exactly that way.
Drill
Two rearranged pairs
circle_c(r) returns a circle's circumference C = 2 × 3.14 × r. Write radius(c) that undoes it, and fix x_of so it undoes y_of. The round trips are checked.
defcircle_c(r):
return2 * 3.14 * r
defradius(c):
return c * 2 * 3.14defy_of(x, m, b):
return m * x + b
defx_of(y, m, b):
return y - b / m
print(radius(circle_c(5)), x_of(y_of(4, 2.5, 1), 2.5, 1))
In the lesson, your code is checked as you write it.
In your own words
Why does a round trip catch slips that a single calculation might hide?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: planning with rearranged formulas
Purpose. Training numbers come in formulas, and the question you have is rarely the one the formula answers.
Accuracy. accuracy = correct/total, so correct = accuracy × total. A model that's 92% accurate on 500 test images got 460 right.
Steps. In training, steps = epochs × n ÷ batch, with n examples. Rearranged for n:
steps=batchepochs×n⇒n=epochssteps×batch
In plain words: to find how many examples a plan needs, multiply the steps by the batch size and divide by the epochs.
Why rearrange instead of guessing? You could try different numbers of examples until the steps come out right, as in the last lesson's guess, check and adjust. But a rearranged formula answers in one line, and a round trip with the original formula confirms it:
The plan needs 10,000 examples, and putting them back into the steps formula gives exactly the 1,000 steps asked for.
Every letter, one formula. The training formula has four letters: steps, epochs, n and batch. Rearranged, it answers four planning questions: how long training takes, how many passes fit a budget, how much data a plan needs, and which batch size hits a target. One small function per question keeps each answer to one line, and a round trip through the original formula catches any slip. Teams planning large training runs do exactly this arithmetic before they spend money on computers.
In the real tools. In PyTorch, the library most models are built with, a DataLoader cuts n examples into batches, and len(loader) counts the steps in one epoch. Here is the plan from above turned round: 10,000 examples, 5 epochs and 1,000 steps, solved for the batch size, with a round trip to check it:
Quick check
Now the same line solved for epochs. Predict both answers: what does this print?
defepochs_for(steps, n, batch):
return steps * batch / n
print(epochs_for(1000, 10000, 50), epochs_for(3000, 48000, 64))
500.0 2250.0
5.0 4.0
5 4
0.2 0.25
Show the answer
5.0 4.0
epochs = steps × batch ÷ n: 1,000 × 50 ÷ 10,000 = 5.0, and 3,000 × 64 ÷ 48,000 = 4.0. The first is the plan above, and you'll plan the second in the drill below.
Drill
Plan a training run
Set right to the number of test images a 92%-accurate model gets right out of 500, n to the examples needed for 3,000 steps over 4 epochs in batches of 64, and back to the round trip of n through steps.
defsteps(epochs, n, batch):
return epochs * n / batch
right = 500 / 0.92
n = 3000 * 4 / 64
back = 0print(right, n, back)
In the lesson, your code is checked as you write it.
In your own words
Give an example of a planning question in training that needs a formula rearranged.
Write a sentence or two, then compare it with a good answer in the lesson.
Goes deeper in
Formulas: using and rearranging formulas in more Python.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write