Speed

Same Samples, Different Order

Run one two-sample epoch in order A then B versus order B then A to see why shuffling changes the final weight.

The problem

In full-batch gradient descent, addition is commutative: averaging the gradients of row A and row B gives the exact same number (gA + gB) / 2 regardless of which row appears first in the table. Does single-sample stochastic gradient descent have that same symmetry?

No! Because the second step of SGD evaluates its gradient at the intermediate weight produced by the first step, the two updates compose like nested functions rather than a simple sum. Processing row A then row B (A -> B) lands at a different weight after one epoch than processing row B then row A (B -> A). That order dependence is one reason SGD shuffles the training rows before each epoch: with a fixed order, the same rows would get the last word in every pass.

Work through the numbers

Take two samples A = (1, 2) and B = (2, 5), starting from w0 = 1 with learning rate lr = 0.05. Trace both orderings step by step:

Order A -> B ((1, 2) first, (2, 5) second):

  • Step 1 on A = (1, 2) at w = 1: gA = 2 * (1 * 1 - 2) * 1 = -2, so w1 = 1 - 0.05 * (-2) = 1.1
  • Step 2 on B = (2, 5) at w = 1.1: gB = 2 * (1.1 * 2 - 5) * 2 = -11.2, so wAB = 1.1 - 0.05 * (-11.2) = 1.66

Order B -> A ((2, 5) first, (1, 2) second):

  • Step 1 on B = (2, 5) at w = 1: gB = 2 * (1 * 2 - 5) * 2 = -12, so w1 = 1 - 0.05 * (-12) = 1.6
  • Step 2 on A = (1, 2) at w = 1.6: gA = 2 * (1.6 * 1 - 2) * 1 = -0.8, so wBA = 1.6 - 0.05 * (-0.8) = 1.64

Same starting weight w0 = 1, same learning rate lr = 0.05, and the exact same two training points—yet order A -> B finishes the epoch at wAB = 1.66, while order B -> A finishes at wBA = 1.64.

The live model

The chart compares the final weight wAB (order A -> B) against wBA (order B -> A). Move the lr slider across 0.05, 0.10, 0.15, 0.20 to watch the ordering gap widen as the step size grows.

At lr = 0.05, the two orderings finish at [wAB, wBA] = [1.66, 1.64] (a gap of 0.02). At lr = 0.10, they diverge to [2.24, 2.16] (a gap of 0.08). At lr = 0.15, they reach [2.74, 2.56], and at lr = 0.20, they separate to [3.16, 2.84] (a gap of 0.32).

Order A then B finishes at wAB = 1.66 while order B then A finishes at wBA = 1.64.
let lr = scene.parameter("lr", 0.05, 0.05, 0.20, 0.05);
let row0 = input.select(0, 0);
let row1 = input.select(0, 1);
let w0 = row0.select(0, 0) * 0.0 + 1.0;
let wa1 = w0.detach().clone().requires_grad(true);
let la1 = (wa1 * row0.select(0, 0) - row0.select(0, 1)).square();
la1.backward();
let wab1 = wa1 - wa1.grad() * lr;
let wa2 = wab1.detach().clone().requires_grad(true);
let la2 = (wa2 * row1.select(0, 0) - row1.select(0, 1)).square();
la2.backward();
let wAB = wa2 - wa2.grad() * lr;
let wb1 = w0.detach().clone().requires_grad(true);
let lb1 = (wb1 * row1.select(0, 0) - row1.select(0, 1)).square();
lb1.backward();
let wba1 = wb1 - wb1.grad() * lr;
let wb2 = wba1.detach().clone().requires_grad(true);
let lb2 = (wb2 * row0.select(0, 0) - row0.select(0, 1)).square();
lb2.backward();
let wBA = wb2 - wb2.grad() * lr;
let list = torch.list().add(wAB.reshape(1)).add(wBA.reshape(1));
let answer = torch.cat(list, 0);
out.set("wAB", wAB.item()).set("wBA", wBA.item());
{
  "wAB": 1.6600000000000001,
  "wBA": 1.6400000000000001
}

Your turn

Order A -> B finishes the epoch at 1.66, whereas order B -> A finishes at 1.64. The starter model reports [1.66, 1.66] with a gap of 0 because its second pass visits the two samples in the same order as the first pass instead of reversing them. Reverse the sample order in the second pass so answer holds [1.66, 1.64].

Starter model runs both passes in A to B order (1.66 and 1.66) instead of reversing the second pass to B to A (1.64).
let lr = 0.05;
let row0 = input.select(0, 0);
let row1 = input.select(0, 1);
let w0 = row0.select(0, 0) * 0.0 + 1.0;
let wa1 = w0.detach().clone().requires_grad(true);
let la1 = (wa1 * row0.select(0, 0) - row0.select(0, 1)).square();
la1.backward();
let wab1 = wa1 - wa1.grad() * lr;
let wa2 = wab1.detach().clone().requires_grad(true);
let la2 = (wa2 * row1.select(0, 0) - row1.select(0, 1)).square();
la2.backward();
let wAB = wa2 - wa2.grad() * lr;
let wb1 = w0.detach().clone().requires_grad(true);
let lb1 = (wb1 * row0.select(0, 0) - row0.select(0, 1)).square();
lb1.backward();
let wba1 = wb1 - wb1.grad() * lr;
let wb2 = wba1.detach().clone().requires_grad(true);
let lb2 = (wb2 * row1.select(0, 0) - row1.select(0, 1)).square();
lb2.backward();
let wBA = wb2 - wb2.grad() * lr;
let list = torch.list().add(wAB.reshape(1)).add(wBA.reshape(1));
let answer = torch.cat(list, 0);
out.set("wAB", wAB.item()).set("wBA", wBA.item());
{
  "wAB": 1.6600000000000001,
  "wBA": 1.6600000000000001
}

Run the second two-step pass in reversed order (B then A) so answer holds [wAB, wBA] = [1.66, 1.64].

Read the result carefully

Why does the gap wAB - wBA grow with lr^2? Expand one linear step w <- w - lr * 2 * (w * x - y) * x = (1 - 2 * lr * x^2) * w + 2 * lr * x * y. Each update multiplies the previous weight by the contraction factor (1 - 2 * lr * x^2).

Because the second update multiplies the first sample's contribution 2 * lr * x_first * y_first by (1 - 2 * lr * x_second^2), the sample that runs last escapes being damped by the other sample's curvature x^2.

Recap

  • Single-sample SGD updates do not commute: changing the sample order changes the trajectory and the end-of-epoch weight.
  • On A = (1, 2) and B = (2, 5) at lr = 0.05, order A -> B gives wAB = 1.66 while order B -> A gives wBA = 1.64.
  • Shuffling the dataset before each epoch prevents a fixed row ordering from permanently biasing the final updates of every pass.