Where We Are
Chapter 019 gave us one partial derivative per input coordinate.
For
we can compute
and
These tell us what happens when we move along the x-axis or y-axis.
But optimization does not have to move only along coordinate axes.
From one point on a loss surface, we can move diagonally, sideways, or in any mixture of parameter directions.
So a new question appears:
If I know all the partial derivatives, can I predict the slope in an arbitrary direction?
And an even more important one:
Which direction makes the function increase fastest?
Both answers come from one vector: the gradient.
1. The Problem: A Million Separate Numbers Are Not Yet Geometry
Suppose at one point we find
and
What do we know?
If we nudge x while freezing y, the function rises about 3 units per x-unit.
If we nudge y while freezing x, it rises about 4 units per y-unit.
But what if we move along
What if we move along
What if our model has a billion parameters?
We need to convert the list of partial sensitivities into geometry.
2. Put the Partial Derivatives Into a Vector
For
define
The upside-down triangle
is called nabla or del.
The vector
is the gradient.
For n variables,
One coordinate sensitivity per coordinate direction.
3. Tiny Example
Let
Then
Therefore
At
we get
This vector points directly away from the origin.
That makes geometric sense: for the bowl
moving directly away from the center increases height fastest.
4. The Gradient Is Not the Function Value
At :
But
These objects answer different questions.
| Object | Meaning |
|---|---|
| height of the surface | |
| local sensitivity vector |
A scalar value says where you are vertically.
A gradient says how the surface changes around you.
5. From Axis Slopes to Any Direction
Suppose we take a tiny step
For a smooth function, the first-order local change is approximately
But that expression is exactly a dot product:
This is one of the most important equations in the course.
The gradient packages all coordinate sensitivities so a dot product can predict local change in any small direction.
6. Derive the Directional Derivative
Let
be a unit vector describing a direction.
Take a tiny step of size h:
Then
Divide by h:
As h shrinks, this becomes the directional derivative:
So once we know the gradient, we know the local slope in every unit direction.
A billion directional questions collapse into one vector plus a dot product.
7. Why the Gradient Points Uphill Fastest
From Chapter 007:
Apply that to the directional derivative:
Since is a unit vector,
Therefore
where is the angle between the gradient and the chosen direction.
Now ask: when is this largest?
Cosine reaches its maximum value 1 when
That means
points in exactly the same direction as the gradient.
Therefore:
And its magnitude
is the maximum local rate of increase per unit step.
This is not a memorized slogan.
It falls directly out of the dot product.
8. The Opposite Direction Goes Downhill Fastest
If the gradient points uphill fastest, then
points downhill fastest.
Because the angle between and is ,
So the directional derivative becomes
This is the steepest possible local decrease per unit movement.
That is the geometric reason gradient descent moves in the negative gradient direction.
9. Contour Lines
A contour line contains points with the same function value.
For
contours satisfy
These are circles.
↑ gradient
↗
( contour )
( • )
( )
Moving along a contour does not change locally.
So the directional derivative along the contour tangent is zero.
That means
Therefore the gradient is perpendicular to contour lines.
Intuition — A contour map on a mountain connects equal-height locations. Walking along a contour keeps height unchanged. The steepest uphill direction crosses contours at right angles.
10. Worked Example With a Non-Circular Surface
Let
Then
At
This does not point diagonally at 45°.
Why?
Because the function changes four times more strongly with y than x near comparable coordinates.
The contour ellipses are narrow in y.
The gradient points across them in the direction where height increases fastest.
11. Directional Derivative Example
At for
we have
Take unit direction
Then
So
But gradient magnitude is
So the diagonal direction rises quickly—but not as quickly as the actual gradient direction.
12. A Tempting Wrong Idea: “Choose the Largest Partial Derivative Axis”
Suppose
The largest component is 4, so maybe we should move only along y.
Slope along y is 4.
But gradient magnitude is
Moving along normalized gradient
gives directional derivative 5.
So combining coordinates can produce a steeper direction than any single axis.
A Tempting Wrong Idea
The steepest direction is not necessarily “the axis with the biggest partial.” It is the vector formed by all partial derivatives together.
13. Gradient of a Loss Function
Suppose our model has parameters
Loss is
The gradient is
Each component tells one coordinate sensitivity.
The whole vector tells the locally steepest uphill direction in parameter space.
Therefore
is the natural local downhill direction.
14. The Gradient Has the Same Shape as the Parameter Vector
If
and
then
One parameter coordinate → one gradient coordinate.
For 100 million parameters, the gradient has 100 million scalar entries.
That shape correspondence is fundamental to training.
15. Local Linear Approximation
The gradient gives more than a direction.
For a small parameter change
we can approximate
This is the multivariable version of “zoom in and a smooth curve looks like a line.”
A smooth surface locally looks like a plane.
The gradient stores that plane’s slopes.
16. Why This Approximation Matters for Optimization
Suppose we choose
where
is small.
Then local approximation gives
So
Since
this is non-positive.
Therefore, for a sufficiently small step in a smooth region, moving along negative gradient should reduce loss.
This is the mathematical seed of gradient descent.
17. But the Gradient Is Local
A crucial warning:
The gradient tells us what happens near the current point.
It does not guarantee what happens after a huge step.
Imagine standing on a curved mountain.
The steepest downhill direction at your feet may not remain downhill for 10 kilometres.
Likewise, a large learning rate can overshoot a valley even when the gradient direction is correct.
Direction and step size are different problems.
Chapter 024 will study that deeply.
18. Gradient Zero Does Not Always Mean Minimum
If
all first-order coordinate slopes vanish.
This can occur at:
Minimum
at .
Maximum
at .
Saddle
at .
The first-order gradient alone cannot distinguish them.
Curvature—second derivatives—becomes relevant.
19. Saddle Point Geometry
For
At the origin:
But along x:
rises away from zero.
Along y:
falls away from zero.
So the origin is neither minimum nor maximum.
This matters in deep learning because high-dimensional loss surfaces contain many saddle-like regions.
20. Gradients and Feature Scaling
Suppose one parameter direction naturally produces gradient component
and another produces
Then the gradient vector is extremely imbalanced.
This can happen because input features or curvature have very different scales.
Optimization may zig-zag or progress slowly.
This connects back to feature scaling from Chapter 006 and forward to:
- normalization,
- adaptive optimizers,
- conditioning,
- learning-rate schedules.
21. Gradient of a Simple Linear Model
Take
and one-example squared loss
Suppose
Prediction:
Error:
Soon the chain rule will give
Numerically:
So
The negative components say increasing both w and b locally lowers loss.
The w direction is three times more sensitive here because x=3.
22. Finite-Difference Gradient Check
We can estimate every gradient component numerically.
For parameter vector
estimate component j by
Repeat for every j.
This is expensive: n parameters require roughly n additional loss evaluations.
But it is useful for checking analytical/backprop gradients on small models.
Backpropagation will compute all needed derivatives far more efficiently.
23. Why We Usually Use Central Differences for Checking
Forward difference:
has first-order approximation error.
A better numerical check often uses central difference:
It samples symmetrically around the point and usually has smaller truncation error for smooth functions.
This is a numerical-analysis tool, not the training algorithm itself.
24. Code: Build a Gradient Numerically
import numpy as np
def numerical_gradient(f, x, h=1e-6):
x = np.asarray(x, dtype=float)
grad = np.zeros_like(x)
for i in range(len(x)):
xp = x.copy()
xm = x.copy()
xp[i] += h
xm[i] -= h
grad[i] = (f(xp) - f(xm)) / (2*h)
return grad
def bowl(v):
x, y = v
return x*x + 4*y*y
print(numerical_gradient(bowl, [1., 1.]))
# approximately [2., 8.]
The loop nudges one coordinate at a time.
That is Chapter 019 turned into a vector.
25. Shape Check
For
parameter vector θ shape (10,)
loss L(θ) scalar
gradient ∇L shape (10,)
small step Δθ shape (10,)
∇L · Δθ scalar
The dot product produces predicted scalar loss change.
That shape story is worth memorizing because it generalizes.
26. Gradients of Matrix Parameters
Neural networks often have a weight matrix
rather than one flat vector.
The gradient with respect to W is arranged with the same shape:
Each entry
So “gradient has the same shape as the thing being differentiated” becomes a powerful practical rule for scalar losses.
Later Chapter 025 will develop matrix calculus carefully.
27. Gradient vs Jacobian
The word gradient is most naturally used when a scalar-valued function has vector input:
If the function itself outputs a vector,
then each output has its own gradient.
Stacking those derivatives produces a Jacobian matrix.
We do not need full Jacobian machinery yet, but this distinction will matter in matrix calculus and autodiff.
28. A Tempting Wrong Idea: “Negative Gradient Is Always Globally Best”
The negative gradient is the steepest local descent direction under ordinary Euclidean geometry and unit-length steps.
It does not claim:
- that the global minimum lies directly that way,
- that a huge step is safe,
- that Euclidean geometry is always the best optimization geometry,
- that gradient descent is the fastest possible optimizer.
A Tempting Wrong Idea
“The gradient points to the minimum.”
No. The gradient points uphill locally. Negative gradient points downhill locally. The minimum may require a curved path and many updates.
29. History Lens — From Surfaces to Optimization
Multivariable calculus converted geometric surfaces into local algebraic information.
The gradient became central because it links three ideas at once:
- coordinate partial derivatives,
- directional rates of change,
- steepest ascent.
Optimization then turns the sign around and uses negative gradients to search for minima.
Modern deep learning scales this classical geometry to parameter spaces with millions or billions of dimensions.
The picture is impossible to draw, but the mathematics is unchanged.
30. Distinctions That Matter
| Pair | Difference |
|---|---|
| partial derivative vs gradient | one coordinate sensitivity vs vector of all sensitivities |
| gradient vs directional derivative | vector of local information vs scalar slope along one chosen direction |
| gradient vs function value | local change information vs current scalar height |
| gradient vs Jacobian | scalar-output derivative vector vs vector-output derivative matrix |
| gradient direction vs step size | where to move vs how far to move |
| stationary point vs minimum | zero first-order gradient vs actual local/global optimum |
31. What We Discovered
- The gradient collects all partial derivatives into one vector.
- It has one component per input coordinate.
- Local change satisfies
- Directional derivative is
- The dot-product formula proves the gradient points toward steepest local increase.
- Negative gradient points toward steepest local decrease.
- Gradient magnitude is the maximum local directional slope for a unit step.
- Gradients are perpendicular to level sets/contours.
- Zero gradient does not prove minimum; maxima and saddles can also be stationary.
- Numerical gradient checks are useful but expensive.
- Gradient shape mirrors parameter shape for scalar losses.
- We still need a way to compute gradients efficiently through deeply composed functions.
32. Mathematics We Built
Gradient
Directional derivative
Dot-product form
Local linear approximation
Negative-gradient local loss change
If
then
33. One-Minute Explanation
A multivariable function has one partial derivative per input coordinate. Put those partial derivatives into a vector and you get the gradient. The gradient is powerful because its dot product with any unit direction gives the slope in that direction. The dot-product geometry then proves that the gradient itself points in the direction of steepest local increase, while the negative gradient points in the direction of steepest local decrease. For optimization, that gives us the direction to move—but not yet how to compute these sensitivities efficiently through many nested functions. That is where the chain rule enters.
34. Mastery Check
- Write the gradient of .
- What is the difference between a partial derivative and a gradient?
- Why does approximate local change?
- Derive the directional derivative formula.
- Use the dot product to prove the gradient is steepest ascent.
- Why is negative gradient steepest local descent?
- Why is the gradient perpendicular to contour lines?
- Why does zero gradient not guarantee a minimum?
- What is the gradient shape for a scalar loss with a
(5,3)weight matrix? - Why can a correct gradient still produce a bad update if the learning rate is huge?
- What is the distinction between a gradient and a Jacobian?
- Why is numerical gradient estimation impractical for training a billion-parameter network?
🔭 Bridge to Chapter 021
We now know what gradient information we want.
But a neural-network loss is not usually one simple formula.
It is a composition:
A parameter affects the loss only through everything downstream of it.
How do we multiply local rates of change through a chain of computations?
That question forces the chain rule.