What We Computed by Hand
Audit every scalar cell and arithmetic step in a four-row linear prediction table before dimensions grow.
The problem
Introductory machine learning lessons work on tiny tables so that no number hides inside a loop. Consider four observations with two input features and a target : rows , , , and . We score each row with the fixed linear rule , starting at intercept , and measure each row's squared residual .
Before asking why paper methods fail on large networks, we should count what a paper calculation actually holds. This entire table contains eight feature cells, four target cells, two weights, and one bias: fifteen scalars in total. Every intermediate product, residual, and squared error fits on four lines of scratch paper.
Work through the numbers
Evaluate row by row. Row 0 gives against , leaving residual and squared error . Row 1 gives against , leaving residual and squared error . Row 2 gives against , leaving residual and squared error .
Row 3 gives against , leaving residual and squared error . Across the four rows, predictions are [0, 5, 6, 11], signed residuals are [-1, 0, 1, 1], and squared errors are [1, 0, 1, 1], whose sum of squared errors is and mean squared error is .
Notice why squaring cannot be skipped when aggregating errors. Adding raw residuals [-1, 0, 1, 1] yields because the under-prediction on row 0 cancels the over-prediction on row 2. Even absolute values match only when every miss sits in ; as soon as a miss is or , and diverge.
The live model
The diagram compares each row's target and prediction on the left and plots each row's squared error on the right. At default bias , total squared error is . Dragging the bias slider down to shifts predictions to [-2, 3, 4, 9] and raises total squared error to ; dragging bias up to shifts predictions to [2, 7, 8, 13] and raises total squared error to . At bias , squared errors become [2.25, 0.25, 0.25, 0.25], tying total .
![Four rows comparing targets [1, 5, 5, 10] with predictions [0, 5, 6, 11] at bias 1, and squared errors [1, 0, 1, 1] summing to 3.](../assets/ml/what-we-computed-by-hand--demo.png)
let bias = scene.parameter("bias", 1.0, -1.0, 3.0, 0.5);
let x = input.slice(1, 0, 2);
let y = input.select(1, 2);
let w = torch.tensor("[3.0, -2.0]", DType.F64);
let pred = x.matmul(w.reshape(2, 1)).squeeze(1) + bias;
let residual = pred - y;
let answer = residual.square();
out.set("predictions", pred).set("squared_errors", answer);
out.set("sse", answer.sum());
{
"predictions": [
0,
5,
6,
11
],
"squared_errors": [
1,
0,
1,
1
],
"sse": 3
}Watch the parameter change
Your turn
In the exercise below, answer must hold the 1D vector of per-row squared residuals for linear weights and intercept bias. On the visible four-row table at bias , that vector is [1, 0, 1, 1] with sum . Check evaluates unseen three-row tables at fractional intercepts such as and where misses take values outside .
![Four rows with targets [1, 5, 5, 10], predictions [0, 5, 6, 11], and un-squared starter residuals [-1, 0, 1, 1].](../assets/ml/what-we-computed-by-hand--exercise.png)
let bias = scene.parameter("bias", 1.0, -1.0, 3.0, 0.5);
let x = input.slice(1, 0, 2);
let y = input.select(1, 2);
let w = torch.tensor("[3.0, -2.0]", DType.F64);
let pred = x.matmul(w.reshape(2, 1)).squeeze(1) + bias;
let residual = pred - y;
let answer = residual;
out.set("predictions", pred).set("squared_errors", answer);
out.set("sse", answer.sum());
{
"predictions": [
0,
5,
6,
11
],
"squared_errors": [
-1,
0,
1,
1
],
"sse": 1
}Compute the squared residual for every row instead of returning the signed residual. Check evaluates new feature tables and fractional intercepts.
Read the result carefully
Working four rows by hand proves that matrix multiplication and elementwise squaring execute the exact algebra written on paper. What this fifteen-number check does not show is cost. Here four dot products of length took multiplications, inner additions, bias additions, subtractions, and squarings: scalar operations in all.
When rows, features, hidden layers, and training steps grow from single digits to thousands, the algebra does not change, yet human inspection of every cell becomes impossible. The remaining pages of this book count each growth factor separately so you can predict memory and compute before running a model.
Recap
- A linear evaluation touches feature cells, targets, parameters, and scalar operations.
- Signed residuals cancel opposite-sign misses, while squared residuals keep every miss nonnegative and weight large misses quadratically.
- Tensor operations reproduce exact hand arithmetic cell for cell while scaling to dimensions that cannot fit on paper.