Speed

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 x1,x2x_1, x_2 and a target yy: rows (1,2,1)(1, 2, 1), (2,1,5)(2, 1, 5), (3,2,5)(3, 2, 5), and (4,1,10)(4, 1, 10). We score each row with the fixed linear rule y^=3x1−2x2+b\hat{y} = 3 x_1 - 2 x_2 + b, starting at intercept b=1b = 1, and measure each row's squared residual (y^−y)2(\hat{y} - y)^2.

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 y^=3x1−2x2+1\hat{y} = 3 x_1 - 2 x_2 + 1 row by row. Row 0 gives 3⋅1−2⋅2+1=03 \cdot 1 - 2 \cdot 2 + 1 = 0 against y=1y = 1, leaving residual −1-1 and squared error (−1)2=1(-1)^2 = 1. Row 1 gives 3⋅2−2⋅1+1=53 \cdot 2 - 2 \cdot 1 + 1 = 5 against y=5y = 5, leaving residual 00 and squared error 00. Row 2 gives 3⋅3−2⋅2+1=63 \cdot 3 - 2 \cdot 2 + 1 = 6 against y=5y = 5, leaving residual 11 and squared error 11.

Row 3 gives 3⋅4−2⋅1+1=113 \cdot 4 - 2 \cdot 1 + 1 = 11 against y=10y = 10, leaving residual 11 and squared error 11. 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 ∑i(y^i−yi)2=3\sum_i (\hat{y}_i - y_i)^2 = 3 and mean squared error is 3/4=0.753 / 4 = 0.75.

Notice why squaring cannot be skipped when aggregating errors. Adding raw residuals [-1, 0, 1, 1] yields −1+0+1+1=1-1 + 0 + 1 + 1 = 1 because the under-prediction on row 0 cancels the over-prediction on row 2. Even absolute values ∣y^i−yi∣|\hat{y}_i - y_i| match (y^i−yi)2(\hat{y}_i - y_i)^2 only when every miss sits in {−1,0,1}\{-1, 0, 1\}; as soon as a miss is 0.50.5 or −2-2, ∣y^i−yi∣|\hat{y}_i - y_i| and (y^i−yi)2(\hat{y}_i - y_i)^2 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 =1= 1, total squared error is 33. Dragging the bias slider down to −1-1 shifts predictions to [-2, 3, 4, 9] and raises total squared error to 1515; dragging bias up to 33 shifts predictions to [2, 7, 8, 13] and raises total squared error to 2323. At bias =0.5= 0.5, squared errors become [2.25, 0.25, 0.25, 0.25], tying total 33.

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.
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 (y^i−yi)2(\hat{y}_i - y_i)^2 for linear weights [3,−2][3, -2] and intercept bias. On the visible four-row table at bias =1= 1, that vector is [1, 0, 1, 1] with sum 33. Check evaluates unseen three-row tables at fractional intercepts such as 0.50.5 and 1.51.5 where misses take values outside {−1,0,1}\{-1, 0, 1\}.

Four rows with targets [1, 5, 5, 10], predictions [0, 5, 6, 11], and un-squared starter residuals [-1, 0, 1, 1].
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 Xw+bX w + b 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 22 took 4⋅2=84 \cdot 2 = 8 multiplications, 44 inner additions, 44 bias additions, 44 subtractions, and 44 squarings: 2424 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 4×24 \times 2 linear evaluation touches 88 feature cells, 44 targets, 33 parameters, and 2424 scalar operations.
  • Signed residuals y^i−yi\hat{y}_i - y_i cancel opposite-sign misses, while squared residuals (y^i−yi)2(\hat{y}_i - y_i)^2 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.