Gradient Descent and Stochastic Gradient Descent
A gradient at w produces the first-order Taylor approximation of f around w.
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…
Choosing the Next Parameter
Suppose the goal is to minimize a function f by changing its parameter. Looking at the entire function may be difficult when deciding what to do next. Gradient descent begins with a narrower question: what does f look like near the parameter value we currently have?
The central idea is local decision-making. Use information at the current parameter to estimate which nearby direction could produce a lower function value.
What do you think happens?
If an approximation is created at the current parameter, should it be trusted equally far away from that parameter?
Reveal answer
No, because the approximation is local and can become loose farther away.
The approximation is formed around the current point. Its usefulness is local, so gradient descent also needs a reason to limit how far the candidate parameter moves.
The Local Taylor View
Let w be the parameter around which the approximation is built, and let u be a nearby candidate parameter. The gradient at w produces a first-order Taylor approximation of f around w. The approximation starts with the current function value f(w), then adds the gradient-based term 〈u − w, ∇f(w)〉. In symbols, the estimate is f(u) ≈ f(w) + 〈u − w, ∇f(w)〉.
- 1w is the point where the approximation is formed.
- 2u is a nearby candidate parameter whose function value is being estimated.
- 3f(w) is the function value at the current point.
- 4∇f(w) is the gradient at the current point.
- 5〈u − w, ∇f(w)〉 is the gradient-based change term.
Reading the Approximation
The approximation gives a local way to compare candidate parameters. Starting from f(w), it estimates how the function changes when moving from w toward u. A candidate that receives a lower estimate according to this local expression is a candidate for reducing the function value.
Interpreting a Candidate Move
Use the local approximation to describe what is being estimated when the current parameter is w and the candidate parameter is u.
- 1Start at the current value
The estimate begins with f(w), the function value at the parameter where the gradient was measured.
- 2Measure the parameter change
The difference u − w describes how the candidate differs from the current parameter.
- 3Use the local gradient
The term 〈u − w, ∇f(w)〉 estimates the change associated with that candidate movement using the gradient at w.
- 4Form the local estimate
Add the gradient-based term to f(w) to obtain the first-order estimate of f(u).
The expression estimates f(u) using only the current value f(w), the candidate displacement u − w, and the gradient ∇f(w) measured at w.
Limiting the Step
A tempting strategy would be to minimize the local approximation directly. The difficulty is that the approximation can become loose when the candidate w is far from the current parameter w(t). A low value predicted by the approximation is therefore not enough by itself to justify an arbitrarily distant move.
- 1w(t) is the current parameter value.
- 2w is a candidate parameter.
- 3The gradient ∇f(w(t)) is measured at the current parameter.
- 4The approximation is intended for candidates close to w(t).
- 5Moving farther from w(t) increases the concern that the local estimate may be loose.
Balancing Distance and Descent
Gradient descent addresses the problem by balancing two goals. It seeks a lower value according to the local approximation, while also keeping the new parameter close to the current parameter. The update is motivated by jointly minimizing the distance from the current parameter and the approximation of f around that parameter.
The parameter η controls the tradeoff between these two terms. In the usual gradient-descent form, the resulting move is represented as w(t + 1) = w(t) − η∇f(w(t)). The negative gradient direction is the locally downhill direction, while η controls how strongly the update responds to that local gradient. The important motivation is not to trust the approximation arbitrarily far away; it is to use the approximation while limiting the movement.
The update rule is best understood as a compromise: improve the local estimate, but do not leave the neighborhood in which that estimate was formed without a distance-based constraint.
Convexity and Reliability
Convex functions have a special role in this motivation. For a convex function, the first-order Taylor approximation lower bounds the function. This connects minimizing the local approximation with searching for a lower value of the original function.
Gradient Descent Variants
| Approach | Gradient information described in the source pack | What the comparison highlights |
|---|---|---|
| Gradient descent | The gradient at the current parameter is used to form the local approximation. | The update is motivated by balancing the local approximation with distance from the current parameter. |
| Stochastic gradient descent | The source pack names this approach but does not specify its sampling or update procedure. | Do not infer operational details from the name alone; those details require additional material. |
This article establishes the local-approximation motivation for gradient descent. It does not provide enough source material to explain the mechanics of stochastic sampling.
Common Reasoning Mistakes
Common mistake Treating the first-order approximation as an exact description of the whole function
What it looks likeUsing f(u) ≈ f(w) + 〈u − w, ∇f(w)〉 as though it were equally reliable for every possible u.
Why it failsThe approximation is useful locally but can become loose when the candidate is far from the point where the gradient was formed.
Do this insteadInterpret the expression as a local estimate and keep the candidate update near the current parameter.
Common mistake Minimizing the local approximation without considering distance
What it looks likeChoosing a distant candidate only because the approximation predicts a lower value there.
Why it failsThe prediction may no longer be reliable far from the current parameter.
Do this insteadBalance lowering the local approximation with staying close to the current parameter.
Common mistake Ignoring the role of the gradient's evaluation point
What it looks likeReading ∇f(w(t)) as though it were the gradient at the candidate w.
Why it failsThe gradient in the local approximation is measured at the current parameter w(t).
Do this insteadTrack the evaluation point: the approximation around w(t) uses ∇f(w(t)).
Common mistake Assuming convexity is irrelevant to the motivation
What it looks likeDescribing the local approximation as a lower bound for every function.
Why it failsThe source specifically gives the lower-bound property for convex functions.
Do this insteadAssociate the lower-bound connection with convex functions while still remembering that locality matters.
Practice the Motivation
Explain, in your own words, why gradient descent does not simply minimize the first-order Taylor approximation without a distance constraint.
Hints
- Identify where the gradient was measured.
- State what can happen when the candidate parameter moves far from that point.
- Explain what η controls in the update motivation.
Complete the reasoning chain: current parameter → local gradient → first-order approximation → distance-aware update. For each arrow, state what information is passed to the next step.
Hints
- The current parameter identifies where the approximation is formed.
- The gradient supplies the gradient-based term.
- The approximation estimates candidate function values locally.
- The distance-aware update limits how far the next parameter moves.
Key Takeaways
- 1The gradient at w produces the first-order Taylor approximation of f around w.
- 2The approximation estimates a candidate value by combining f(w) with a gradient-based change term.
- 3The estimate is local and can become loose when the candidate moves far from the point where the gradient was measured.
- 4Gradient descent is motivated by jointly lowering the local approximation and keeping the new parameter close to the current parameter.
- 5For a convex function, the first-order approximation lower bounds the function, which connects local minimization with the search for a lower original function value.
At a glance
- A gradient provides a first-order local approximation around the current parameter.
- The approximation helps identify a locally downhill direction, but it should not be trusted arbitrarily far away.
- Gradient descent balances improvement according to the local approximation with distance from the current parameter.
- The parameter η controls the tradeoff in the update.
- Convexity is important because the first-order approximation lower bounds a convex function.