1234$3$6$9$120cups →billtestweight w0cups bill1 $32 $63 $91pred = w * x2err = pred - y3loss = mean(err ** 2)4grad = mean(2 * err * x)5w = w - 0.075 * gradinputshiddenoutputx1yx2?Thecoffeeishotreadycold$3$6$993681−28−8.4−2.52−0.76−0.23−0.07hot
Back: the chart, the weight w and the loss gauge with its lampMiddle: the receipts (examples) and one training step in PythonTokens: receipts, squared errors and gradients hopping between the partsBack: a chart of three café receipts (cups across, bill up) with the model's line, the weight w, and the loss gauge whose lamp glows brighter the more wrong the model is. The model predicts w × cups; the errors are squared and averaged into the loss (42 at w = 0); gradient descent nudges w against the gradient (w = 0 − 0.075 × (−28) = 2.1) and repeats until w is about 3, $3 a cup. Front: the same idea scaled up, a neural network of weighted sums with ReLU, and a language model predicting the next token. The receipts are toy data from a made-up café that charges exactly $3 a cup, so the rule to discover is known. Real data is noisy, and real models also learn a bias (a starting value, as in bill = w × cups + b).

Learning from examples

  1. Normal code follows rules a person wrote. Machine learning works the other way round: give the computer examples with the right answers, and it finds the rule itself. Here the examples are café receipts. Nobody tells it what a cup costs.

Training

Bigger models

What happens

;; step 1/10 · rules-to-examples
normal program     rules + input  → answer
machine learning   examples + answers → rules

the task: from receipts, find the price of a cup
nobody writes  bill = 3 × cups  into the code

The receipts are toy data from a made-up café that charges exactly $3 a cup, so the rule to discover is known. Real data is noisy, and real models also learn a bias (a starting value, as in bill = w × cups + b). Every number in the figure is computed: run the program on the code card with NumPy and it prints the same weights and losses (w: 2.1, 2.73, 2.92; loss: 42, 3.78, 0.34, 0.031).