Speed

One Sample Gradient

Compute the loss and gradient on a single row (x1 = 2, y1 = 4) and update w immediately without waiting for the rest of the dataset.

The problem

Full-batch gradient descent waits until all N rows have been evaluated before changing w even once. What if we go to the opposite extreme? In stochastic gradient descent (SGD, with batch size 1), we pick a single sample (x_i, y_i), compute its squared loss L_i(w) = (w * x_i - y_i)^2 and its gradient g_i = 2 * (w * x_i - y_i) * x_i, and update the weight right away:

w_new = w - lr * g_i

Why would anyone update w from a single row when g_i is only a noisy one-sample estimate of the full-batch gradient g_batch? Because computing g_i costs 1 / N as much arithmetic as g_batch: in the time full-batch gradient descent takes to compute one step across N rows, stochastic gradient descent has already updated w N times!

Work through the numbers

Select row 1 (x_1 = 2, y_1 = 4) from our four-row dataset [(1, 2), (2, 4), (3, 5), (4, 9)] at the starting weight w = 1 with learning rate lr = 0.05:

Single-sample forward pass:

  • yhat_1 = w * x_1 = 1 * 2 = 2
  • L_1 = (yhat_1 - y_1)^2 = (2 - 4)^2 = (-2)^2 = 4

Single-sample gradient:

  • g_1 = 2 * (w * x_1 - y_1) * x_1 = 2 * (-2) * 2 = -8

Immediate stochastic update:

  • w_new = 1 - 0.05 * (-8) = 1 + 0.4 = 1.4

Compare this one-sample update (g_1 = -8, w_new = 1.4) against the full-batch update from the previous page (g_batch = -15.5, w_new = 1.775). Both move w upward from 1 toward 2, yet row 1's step is smaller because its input x_1 = 2 has less leverage than row 3 (x_3 = 4, y_3 = 9, where g_3 = -40).

The live model

The chart plots [L_1, g_1, w_new] for row 1 (2, 4) as you drag the lr slider from 0.02 to 0.10. At the default lr = 0.05, the three values are [4, -8, 1.4].

Watch how both L_1 = 4 and g_1 = -8 stay fixed at w = 1 while w_new = 1 - lr * g_1 scales linearly with lr: at lr = 0.02, w_new moves to 1.16, and at lr = 0.10, w_new reaches 1.8 (where full-batch reached 2.55).

Single-sample loss L1=4, gradient g1=-8, and updated weight w_new=1.4 on row 1 (2, 4).
let lr = scene.parameter("lr", 0.05, 0.02, 0.10, 0.01);
let row = input.select(0, 1);
let x1 = row.select(0, 0);
let y1 = row.select(0, 1);
let w0 = x1 * 0.0 + 1.0;
let w = w0.detach().clone().requires_grad(true);
let loss = (w * x1 - y1).square();
loss.backward();
let g = w.grad();
let w_new = w - g * lr;
let list = torch.list().add(loss.reshape(1)).add(g.reshape(1));
let answer = torch.cat(list.add(w_new.reshape(1)), 0);
out.set("loss", loss.item()).set("g1", g.item()).set("w_new", w_new.item());
{
  "loss": 4,
  "g1": -8,
  "w_new": 1.4
}

Your turn

On row 1 (x_1 = 2, y_1 = 4) at w = 1, the squared loss is 4, the single-sample gradient is -8, and the updated weight at lr = 0.05 is 1.4. The starter model reports [4, -4, 1.2]: its gradient -4 is only the slope with respect to the prediction yhat_1, missing the leverage of x_1 = 2. Return [L_1, g_1, w_new] = [4, -8, 1.4].

Starter model omits multiplying by x1=2 in the sample gradient g, returning -4 instead of -8.
let lr = 0.05;
let row = input.select(0, 1);
let x1 = row.select(0, 0);
let y1 = row.select(0, 1);
let w0 = x1 * 0.0 + 1.0;
let w = w0.detach().clone().requires_grad(true);
let loss = (w * x1 - y1).square();
let g = (w * x1 - y1) * 2.0;
let w_new = w - g * lr;
let list = torch.list().add(loss.reshape(1)).add(g.reshape(1));
let answer = torch.cat(list.add(w_new.reshape(1)), 0);
out.set("loss", loss.item()).set("g1", g.item()).set("w_new", w_new.item());
{
  "loss": 4,
  "g1": -4,
  "w_new": 1.2
}

Compute the full derivative with respect to w on row 1 so answer holds [L_1, g_1, w_new] = [4, -8, 1.4].

Read the result carefully

Look at how a single-sample gradient behaves: g_1 = 2 * (w * x_1 - y_1) * x_1 depends only on row 1 (x_1 = 2, y_1 = 4) and knows nothing about rows 0, 2, or 3.

The word stochastic means the row is drawn at random. If each of the four rows is equally likely, the average of the four possible one-sample gradients at w = 1, which are -2, -8, -12 and -40, is -15.5: exactly the full-batch gradient. A single draw is noisy (it can be anything from -2 to -40), but it is right on average. This book walks the rows in a fixed order so that every number can be checked by hand.

On its own, one stochastic step is myopic—it pulls w exclusively toward y_1 / x_1 = 2. What makes SGD work across a whole dataset is chaining these single-row updates one after another so each new row evaluates its gradient at the already-updated weight.

Recap

  • A one-sample gradient g_i = 2 * (w * x_i - y_i) * x_i differentiates a single row's loss L_i(w) = (w * x_i - y_i)^2.
  • On row 1 (2, 4) at w = 1 with lr = 0.05, L_1 = 4, g_1 = -8, and w_new = 1.4.
  • Unlike full-batch gradient descent, SGD updates w immediately after inspecting a single sample.