Stochastic Gradient Descent
The gradient identifies the direction in which the function increases fastest around the current vector.
Try it: Gradient Descent: Full-Batch, Mini-Batch and Stochastic
How gradient descent, mini-batch gradient descent and stochastic gradient descent differ only in how many examples estimate the gradient — and how that trades noisy steps for many cheap updates.
How it works
- The loss is the mean squared error of the line ŷ = w·x + b over 12 points.
- At the start of every epoch, shuffle the examples (seeded) and cut them into batches.
- For each batch, average the gradient 2(ŵx + b − y)·(x, 1) over its examples.
- Update (w, b) ← (w, b) − learning rate × gradient; the path is drawn over the loss contours.
- Batch size 12 is full-batch gradient descent, 1 is SGD; too large a learning rate overshoots and diverges.
Default run (32 steps): mini-batch gradient descent (batch size 4): start at w = -1, b = 3 (MSE 9.823), learning rate 0.1, 3 updates per epoch. The least-squares optimum is w* = 1.595, b* = 0.404. … After 10 epochs (30 updates): w = 1.608, b = 0.468, MSE 0.5665 vs the optimum 0.5613 (distance 0.066). Averaged iterate w̄ = 1.106, b̄ = 0.996.
Simplified: A two-parameter linear-regression loss on 12 fixed points with a constant learning rate; real SGD runs on large datasets and often decays the learning rate.
Loading the simulation…
The Direction of Descent
Optimization begins with a current vector and asks a local question: which direction makes the function increase fastest here? The gradient answers that question. Because the goal is minimization, the algorithm reverses the gradient direction and takes a scaled step away from increasing values. It then treats the new vector as the current vector and repeats the process.
One Gradient Descent Update
For a differentiable function, the gradient at the current vector identifies the direction in which the function increases fastest around that vector. Gradient descent uses the opposite direction because it is seeking lower function values.
The current vector w(t) is the point where the gradient is evaluated. The gradient ∇f(w(t)) supplies the direction of fastest local increase. The positive parameter η, commonly called the learning rate, scales how far the algorithm moves. Subtraction reverses the gradient direction. The resulting vector w(t+1) becomes the current vector for the next iteration.
Tracing the Arithmetic
A Single Scalar Update
Suppose the current value is w(t) = 10, the gradient is 2, and the positive learning rate is η = 0.5. Find the next value.
- 1Start with the rule
Use w(t+1) = w(t) − η∇f(w(t)).
- 2Scale the gradient
The scaled movement is 0.5 × 2 = 1.
- 3Subtract the movement
The next value is 10 − 1 = 9.
The next value is w(t+1) = 9. The update moves opposite to the positive gradient direction.
The important pattern is not the particular numbers. The current value supplies the location, the gradient supplies the direction, the learning rate controls the scale of the movement, and subtraction produces the next value. In a vector update, the same operations apply component by component through vector arithmetic.
What Makes the Method Stochastic
Stochastic gradient descent is an optimization algorithm for minimizing a function f(w) using randomly selected update vectors. At iteration t, it uses the current parameters w(t), selects a random vector v(t) from an appropriate distribution, and updates the parameters by subtracting the learning-rate-scaled random vector.
The random vector v(t) does not need to equal the exact gradient at one iteration. Its conditional expected value must be a subgradient at the current vector. This requirement lets an individual update be random while preserving the required gradient-like direction in expectation. Repeated iterations use these random directions to form a sequence of parameter vectors.
Reading the SGD Rule
| Symbol | Role |
|---|---|
| w(t) | The current parameter vector at iteration t. |
| η | A positive parameter that scales the movement. |
| v(t) | The randomly selected update vector for iteration t. |
| w(t+1) | The newly calculated parameter vector. |
| t through T | The iteration range: repeat the update from t = 1 through the chosen final iteration T. |
Reading each part of the stochastic gradient descent update
The SGD Procedure
The stated SGD procedure follows a fixed cycle. It begins with an initial parameter vector. For each iteration from t = 1 through T, it uses the current vector to determine the condition the random choice must satisfy, selects v(t) from an appropriate distribution, and calculates the next vector with the update rule. After the final iteration, the procedure outputs the average of the iteration vectors w(t).
A Symbolic SGD Update
Updating a Parameter Vector
Let the current parameter vector be w(t) = (a, b), let the randomly selected update vector be v(t) = (p, q), and let the positive learning rate be η. Find w(t+1).
- 1Write the SGD rule
Use w(t+1) = w(t) − ηv(t).
- 2Substitute the vectors
Replace w(t) with (a, b) and v(t) with (p, q), giving w(t+1) = (a, b) − η(p, q).
- 3Distribute the scalar
The scaled random vector is (ηp, ηq).
- 4Subtract component by component
The next vector is (a − ηp, b − ηq).
w(t+1) = (a − ηp, b − ηq).
This symbolic update separates the four roles in the rule. The current parameters provide the starting point, the random vector provides the direction used for this iteration, the learning rate controls the movement's scale, and t identifies which update in the sequence is being performed.
What do you think happens?
If the current vector is (a, b) and the random vector is (p, q), what happens to the first coordinate after applying w(t+1) = w(t) − ηv(t)?
Reveal answer
It becomes a − ηp.
The random vector is first scaled by the learning rate and then subtracted from the current parameter vector.
Implementation Checks
Common mistake Adding the update vector instead of subtracting it.
What it looks likeUsing w(t+1) = w(t) + ηv(t).Why it failsThe stated update rule subtracts the learning-rate-scaled random vector, reversing the direction used for the update.
Do this insteadUse w(t+1) = w(t) − ηv(t).Common mistake Reusing the initial vector at every iteration.
What it looks likeComputing every update from w(1) rather than from the newest vector.
Why it failsEach newly produced vector becomes the current vector for the next iteration.
Do this insteadAfter calculating w(t+1), use it as the current vector in the next cycle.
Common mistake Assuming the random vector must equal the exact gradient.
What it looks likeRejecting an update because v(t) is not identical to the gradient.
Why it failsSGD allows an individual random vector to differ from the exact gradient; its conditional expected value must be a subgradient at the current vector.
Do this insteadCheck the required expected-value condition rather than demanding equality at every iteration.
Common mistake Returning the last vector when the specified procedure requires an average.
What it looks likeOutputting w(T) without averaging the iteration vectors.
Why it failsThe stated SGD procedure outputs the average of the iteration vectors, while last-vector output is only one possible choice in the broader discussion of gradient descent outputs.
Do this insteadPreserve the required final averaging step.
Common mistake Omitting initialization or changing the iteration range.
What it looks likeStarting without the initial parameter vector or stopping before the chosen T iterations.
Why it failsA correct implementation must preserve initialization, the iteration process, the update rule, and the final averaging step.
Do this insteadCheck all four parts against the required procedure.
When checking an SGD procedure, audit it in order: confirm the initial vector, verify the iteration range, verify that v(t) is selected under the required condition, check the subtraction update, confirm that each new vector becomes current, and inspect the final output rule.
Practice the Update
Let w(t) = (6, -2), η = 0.25, and v(t) = (4, -8). Apply the SGD update rule and write w(t+1). Then state which vector must be used as the current vector in the next iteration.
Hints
- First calculate ηv(t).
- Subtract the scaled vector from w(t).
- The newly calculated vector is the next current vector.
Practice Check
Using w(t) = (6, -2), η = 0.25, and v(t) = (4, -8), find w(t+1).
- 1Scale the random vector
ηv(t) = 0.25(4, -8) = (1, -2).
- 2Subtract from the current vector
w(t+1) = (6, -2) − (1, -2).
w(t+1) = (5, 0). The vector (5, 0) becomes the current vector for the next iteration.
Key Takeaways
- 1The gradient points in the direction of fastest local increase, so gradient descent moves in the opposite direction.
- 2The gradient descent update subtracts the learning-rate-scaled gradient from the current vector.
- 3SGD replaces the exact gradient at an individual iteration with a random update vector whose conditional expected value must be a subgradient at the current vector.
- 4Each new parameter vector becomes the current vector for the next iteration.
- 5The stated SGD procedure initializes the parameters, repeats the random selection and update through T iterations, and returns the average of the iteration vectors.
At a glance
- The gradient identifies the fastest local increase, and reversing it gives the direction used for descent.
- Gradient descent updates the current vector by subtracting a learning-rate-scaled gradient.
- SGD uses a random update vector whose conditional expected value has the required subgradient direction.
- The current vector, learning rate, random vector, and iteration count each have a distinct role in the update.
- A correct SGD procedure preserves initialization, iteration, update, state replacement, and final averaging.