Deep Learning · TEE · section
A tiny dataset shows why the methods differ
Optimization algorithms
Question. Train ŷ = wx on samples (1,2) and (2,4), starting at w = 1, with η = 0.1 and mean half squared loss. Compare one batch update with one SGD epoch in the listed order.
Given
Model ŷ = wx; initial w = 1; η = 0.1.
Samples in order: (1,2), (2,4).
Mean half squared loss for the batch.
To find
Compare one batch update with a sequential SGD epoch.
Formula — which one and why
Batch mean versus sequential SGD. A batch update averages gradients evaluated at the same old parameter. Sequential SGD updates after each sample, so later samples use the changed parameter.
B = number of examples in the batch; gᵢ = sample gradient; η = learning rate. For the linear half squared objective, sample gradient is (wx−y)x.
g_{\mathrm{batch}}=\frac1B\sum_i g_ig_i=(wx_i-y_i)x_iw_{\mathrm{new}}=w-\eta gYaad rakhne ka cue: Batch: same old weight par average; SGD: har sample ke baad weight change.
Solution — step by step
One batch update
Step 1: predict sample 1 with the original weight.
\hat y_1=1(1)=1Step 2: calculate its error.
e_1=1-2=-1Step 3: multiply its error by its input.
g_1=(-1)(1)=-1Step 4: predict sample 2 with the same original weight.
\hat y_2=1(2)=2Step 5: calculate its error.
e_2=2-4=-2Step 6: multiply its error by its input.
g_2=(-2)(2)=-4Step 7: average the two gradients.
g=\frac{-1+(-4)}2=-2.5Step 8: update once.
w_{\mathrm{new}}=1-0.1(-2.5)=1.25One sequential SGD epoch
Step 1: use the first sample gradient.
g_1=-1Step 2: update after the first sample.
w_1=1-0.1(-1)=1.1Step 3: predict sample 2 using the changed weight.
\hat y_2=1.1(2)=2.2Step 4: find sample 2 error.
e_2=2.2-4=-1.8Step 5: find the new sample 2 gradient.
g_2=(-1.8)(2)=-3.6Step 6: update after sample 2.
w_2=1.1-0.1(-3.6)=1.46Final answer: batch weight = 1.25; sequential SGD weight = 1.46. SGD changes the weight between samples.
Larger mini-batches use more memory and often make better use of parallel hardware. Smaller batches provide noisier gradient estimates and more updates per epoch. Wall-clock speed and generalization depend on the implementation and problem, so “SGD always converges fastest” is not a safe claim.