Full-Batch Gradient
Differentiate every sample's squared loss at w = 1, average the four slopes into the full-batch gradient, and take one step.
The problem
Because the full-dataset loss L(w) = (L_0(w) + L_1(w) + L_2(w) + L_3(w)) / 4 is the average of the four per-sample squared losses L_i(w) = (w * x_i - y_i)^2, how do we compute its derivative dL/dw? By linearity of differentiation, the derivative of an average is the average of the derivatives:
g_batch = (g_0 + g_1 + g_2 + g_3) / 4, where g_i = dL_i/dw = 2 * (w * x_i - y_i) * x_i
In full-batch gradient descent, the optimizer evaluates every single row of the dataset at the current weight w = 1, averages their gradients into g_batch, and performs one weight update w_new = w - lr * g_batch. Not a single parameter changes until all four rows have been inspected.
Work through the numbers
Take the four rows [(1, 2), (2, 4), (3, 5), (4, 9)] at the starting weight w = 1. Compute each row's individual gradient g_i = 2 * (w * x_i - y_i) * x_i:
- Row
0(1, 2):g_0 = 2 * (1 * 1 - 2) * 1 = 2 * (-1) * 1 = -2 - Row
1(2, 4):g_1 = 2 * (1 * 2 - 4) * 2 = 2 * (-2) * 2 = -8 - Row
2(3, 5):g_2 = 2 * (1 * 3 - 5) * 3 = 2 * (-2) * 3 = -12 - Row
3(4, 9):g_3 = 2 * (1 * 4 - 9) * 4 = 2 * (-5) * 4 = -40
Average the four gradients to obtain the full-batch gradient, then update w = 1 with lr = 0.05:
g_batch = (-2 + (-8) + (-12) + (-40)) / 4 = -62 / 4 = -15.5w_new = 1 - 0.05 * (-15.5) = 1 + 0.775 = 1.775
Notice why dividing by 4 (using .mean() rather than .sum()) matters: the sum -62 grows linearly with dataset size, so forgetting to divide by N = 4 effectively multiplies your learning rate by 4 (w_new = 4.1, overshooting the target slope).
The live model
The chart plots g_batch = -15.5 (red) and the updated weight w_new (green) as you drag the lr slider from 0.02 to 0.10.
Notice that the red bar g_batch = -15.5 is a property of the loss surface at w = 1 and stays fixed as lr changes; only the step w_new = 1 - lr * g_batch scales with the slider. At lr = 0.02, w_new moves conservatively to 1.31; at the default lr = 0.05, w_new reaches 1.775; and at lr = 0.10, w_new jumps to 2.55.

let lr = scene.parameter("lr", 0.05, 0.02, 0.10, 0.01);
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let w0 = x.select(0, 0) * 0.0 + 1.0;
let w = w0.detach().clone().requires_grad(true);
let loss = (x * w - y).square().mean();
loss.backward();
let g = w.grad();
let w_new = w - g * lr;
let answer = torch.cat(torch.list().add(g.reshape(1)).add(w_new.reshape(1)), 0);
out.set("loss", loss.item()).set("g", g.item()).set("w_new", w_new.item());
{
"loss": 8.5,
"g": -15.5,
"w_new": 1.775
}Your turn
Across the four rows at w = 1, the mean gradient is -15.5, moving w to 1.775 at lr = 0.05. The starter model reports [-62, 4.1]: its slope is 4 times too steep because the four row losses were summed rather than averaged before differentiating, causing the update to overshoot past 2 to 4.1. Return [g_batch, w_new] = [-15.5, 1.775].

let lr = 0.05;
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let w0 = x.select(0, 0) * 0.0 + 1.0;
let w = w0.detach().clone().requires_grad(true);
let loss = (x * w - y).square().sum();
loss.backward();
let g = w.grad();
let w_new = w - g * lr;
let answer = torch.cat(torch.list().add(g.reshape(1)).add(w_new.reshape(1)), 0);
out.set("loss", loss.item()).set("g", g.item()).set("w_new", w_new.item());
{
"loss": 34,
"g": -62,
"w_new": 4.1
}Differentiate the mean squared error over all four rows so answer holds [g_batch, w_new] = [-15.5, 1.775].
Read the result carefully
Look at how much work went into that single update from w = 1 to w_new = 1.775: we had to evaluate g_0 = -2, g_1 = -8, g_2 = -12, and g_3 = -40 at the exact same weight w = 1 before moving w at all. On a dataset of four rows that is trivial, whereas on a dataset of four million rows, full-batch gradient descent must process all four million examples just to take one step.
Recap
- The full-batch gradient
g_batchaverages the per-sample gradientsg_i = 2 * (w * x_i - y_i) * x_iacross allNrows. - On
[(1, 2), (2, 4), (3, 5), (4, 9)]atw = 1, the four sample gradients[-2, -8, -12, -40]average tog_batch = -15.5. - Using
.mean()instead of.sum()keeps the gradient scale independent of the number of rowsN.