Tiny Linear Data
Evaluate a single-weight model yhat = w * x across four noisy data rows, computing each sample's squared loss and their average.
The problem
In the previous book we traced gradients on one example (x, y) at a time. When you have only one example (x = 2, y = 4) and a linear model yhat = w * x, there is a single weight w = 2 that drives the loss to 0. Real training datasets are different: they contain multiple rows whose individual targets pull the weight w toward slightly different values.
Take four data rows (x, y): row 0 is (1, 2), row 1 is (2, 4), row 2 is (3, 5), and row 3 is (4, 9). Rows 0 and 1 sit on the line y = 2x (preferring w = 2), row 2 sits on y = (5/3)x (preferring w = 1.667), and row 3 sits on y = 2.25x (preferring w = 2.25).
No single weight w can make all four per-sample losses L_i = (w * x_i - y_i)^2 zero simultaneously; instead, the dataset objective averages them into the mean squared error L = (L_0 + L_1 + L_2 + L_3) / 4.
Work through the numbers
Evaluate the four per-sample squared losses L_i = (w * x_i - y_i)^2 at the starting weight w = 1:
- Row
0(1, 2):(1 * 1 - 2)^2 = (-1)^2 = 1 - Row
1(2, 4):(1 * 2 - 4)^2 = (-2)^2 = 4 - Row
2(3, 5):(1 * 3 - 5)^2 = (-2)^2 = 4 - Row
3(4, 9):(1 * 4 - 9)^2 = (-5)^2 = 25
Sum the four sample losses and divide by 4 to obtain the dataset mean loss:
mean_loss = (1 + 4 + 4 + 25) / 4 = 34 / 4 = 8.5
Notice how unevenly the error is distributed at w = 1: row 3 (4, 9) contributes 25 out of the total sum 34, more than the other three rows combined (1 + 4 + 4 = 9).
The live model
The chart plots the four per-sample losses [L0, L1, L2, L3] (blue) alongside their dataset average mean_loss (red). Drag the weight slider from 1 to 2.5 in steps of 0.5.
At weight = 1, the vector is [1, 4, 4, 25, 8.5]. At weight = 1.5, all four misses shrink ([0.25, 1, 0.25, 9, 2.625]). At weight = 2, rows 0 and 1 hit 0 loss while rows 2 and 3 each have loss 1, giving [0, 0, 1, 1, 0.5]. At weight = 2.5, row 2 overshoots to loss 6.25 and the mean loss rises to 2.125.
![Per-sample squared losses [1, 4, 4, 25] and mean loss 8.5 at weight w=1.](../assets/ml/tiny-linear-data--demo.png)
let weight = scene.parameter("weight", 1.0, 1.0, 2.5, 0.5);
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let losses = (x * weight - y).square();
let mean_loss = losses.mean();
let answer = torch.cat(torch.list().add(losses).add(mean_loss.reshape(1)), 0);
out.set("losses", losses).set("mean_loss", mean_loss.item());
{
"losses": [
1,
4,
4,
25
],
"mean_loss": 8.5
}Your turn
At w = 1, the four per-sample squared errors are [1, 4, 4, 25], and their dataset mean is 34 / 4 = 8.5. The starter model reports [1, 4, 4, 25, 34]: its fifth entry is 4 times too large because it totals the four row losses instead of averaging them. Return [L0, L1, L2, L3, mean_loss] = [1, 4, 4, 25, 8.5].

let w = 1.0;
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let losses = (x * w - y).square();
let mean_loss = losses.sum();
let answer = torch.cat(torch.list().add(losses).add(mean_loss.reshape(1)), 0);
out.set("losses", losses).set("mean_loss", mean_loss.item());
{
"losses": [
1,
4,
4,
25
],
"mean_loss": 34
}Compute the dataset mean squared error as the fifth entry of answer so it holds [1, 4, 4, 25, 8.5].
Read the result carefully
Because row 2 wants w < 2 and row 3 wants w > 2, any gradient update that uses only one row will pull w toward that single row's preferred slope, whereas an update that uses all four rows balances their competing pulls.
The next pages use this four-row dataset [(1, 2), (2, 4), (3, 5), (4, 9)] starting from w = 1 for the full-batch, single-sample (stochastic), and mini-batch gradients; the pages that run a whole epoch of single-row steps switch to four rows with smaller inputs, because a row with x = 4 would overshoot at the same learning rate.
Recap
- Each training row
(x_i, y_i)defines its own per-sample lossL_i(w) = (w * x_i - y_i)^2. - When rows have slightly different ratios
y_i / x_i, no single weightwcan make everyL_i(w) = 0at once. - The full-dataset objective
L(w)is the mean(L_0 + L_1 + L_2 + L_3) / 4of the individual sample losses.