Goal
Turn many coordinate-by-coordinate sensitivities into one object that answers three questions:
- How does the function change in any chosen direction?
- Which unit direction increases it fastest?
- What is the best linear prediction of its nearby change?
That object is the gradient.
Motivation
Lesson 34 treated a multivariable function one coordinate at a time. For a loss with thousands or billions of parameters, isolated partial derivatives are useful ingredients, but an optimizer needs them assembled.
The gradient compresses every first-order coordinate sensitivity into a vector:
Once assembled, the same object predicts local change, measures slope along any direction, and supplies the update direction in gradient descent.
Intuition: A Local Sensitivity Arrow
Imagine standing on a smooth hill. The ground may rise differently in every horizontal direction.
- Each partial derivative measures the slope along one coordinate axis.
- The gradient combines those axis-aligned measurements into one arrow.
- The arrow points toward the steepest local increase.
- The arrow’s length records how steep that best ascent is.
- The negative gradient points toward the steepest local decrease.
This statement compares unit directions. Without fixing direction length, any slope could be made arbitrarily large merely by scaling the direction vector.
Notation / Symbol Table
| Symbol | Plain-English name | Meaning |
|---|---|---|
| scalar field | A function with a vector input and scalar output. | |
| input point | The point where local sensitivity is measured. | |
| gradient | Column vector of all coordinate partial derivatives at . | |
| direction | A vector describing a direction of motion; usually . | |
| directional derivative | Local rate of change along . | |
| displacement | A small change to the input vector. | |
| differential at | Linear map that sends to first-order output change. | |
| Euclidean norm | Length of a vector . |
From Partials To A Gradient
For , define:
The shape is important:
- input : vector in ;
- output : scalar in ;
- each partial derivative: scalar;
- gradient : vector in .
The gradient has one component for every input coordinate.
Worked Example: Assemble The Components
Let:
Compute the partial derivatives:
Assemble them:
At :
The function rises locally faster through the coordinate than through the coordinate, but the steepest direction is not either coordinate axis. It is the direction of the combined vector .
Directional Derivatives
A coordinate partial derivative measures change along a basis direction. A directional derivative measures change along any direction .
For a differentiable scalar field:
When is a unit vector, this is the slope per unit distance traveled in that direction.
For the previous example, choose the unit direction:
Then:
So the function decreases in that direction, even though both gradient components are positive. Directional change depends on alignment with the whole gradient.
Why The Gradient Is Steepest
For every unit vector , the Cauchy-Schwarz inequality gives:
Equality occurs when points in the same direction as the gradient:
Therefore:
- steepest unit-direction increase is along ;
- its rate is ;
- steepest unit-direction decrease is along ;
- its rate is .
If , first-order information does not prefer any direction. The point may be a minimum, maximum, saddle, or flat point; curvature or higher-order information is needed.
The Gradient As A Local Linear Approximation
For a small displacement :
Equivalently:
This unifies Lesson 34’s coordinate formula. In two dimensions:
The dot product is not an extra trick. It is the compact form of adding every coordinate sensitivity contribution.
Gradient Vector And Differential
There is a subtle but useful distinction.
The derivative at is fundamentally the linear rule:
For a scalar field in Euclidean coordinates:
So:
- is a linear map from an input displacement to a scalar change;
- is the vector representing that map through the Euclidean dot product.
You may see the derivative called a covector because it consumes a vector and returns a scalar. In ordinary Euclidean machine-learning calculations, the inner product lets us represent that covector by the familiar gradient vector. Later matrix-calculus lessons will make the shape convention explicit.
Contours And Orthogonality
A level set keeps the function value constant:
Moving tangent to a smooth level set produces no first-order change. If is a tangent direction, then:
Therefore the gradient is normal to the level set.
This recovers the geometry from constrained optimization: at a constrained optimum, the objective gradient cannot have a component along feasible tangent directions, so it aligns with the constraint normal.
Connection To Gradient Descent
Gradient descent applies:
The gradient supplies a local direction, while the learning rate chooses a step length.
Using the local linear approximation with :
For and a nonzero gradient, the predicted first-order change is negative. This explains the sign in the update. It does not guarantee that an arbitrarily large step decreases the true loss; curvature limits how far the local approximation remains reliable.
Connection To Programming
A scalar field accepts a vector and returns one number:
type ScalarField = (x: readonly number[]) => number;
A numerical gradient applies a centered partial-derivative check to every coordinate:
function numericalGradient(
f: ScalarField,
at: readonly number[],
h = 1e-5,
): number[] {
return at.map((_, coordinate) => {
const plus = at.map((value, index) =>
index === coordinate ? value + h : value,
);
const minus = at.map((value, index) =>
index === coordinate ? value - h : value,
);
return (f(plus) - f(minus)) / (2 * h);
});
}
Each pair of evaluations perturbs the original point at. The output array has the same length as the input because a scalar field’s gradient has one component per input coordinate.
Finite differences are useful for checking a derived or automatically computed gradient. They are approximate and require two function evaluations per coordinate, so modern training systems normally use automatic differentiation instead.
Common Mistakes
Treating the gradient as a scalar
Each partial derivative is scalar; the gradient is the vector containing all of them.
Forgetting to evaluate at a point
may be a vector-valued formula. A numerical direction appears only after substituting a point.
Using a non-unit direction without tracking scale
scales when is scaled. Normalize the direction when asking for slope per unit distance or comparing directions.
Saying the gradient always points downhill
The gradient points toward steepest local increase. Its negative points toward steepest local decrease.
Assuming a zero gradient proves a minimum
A zero gradient identifies a stationary point. Hessian or higher-order information is needed to classify it.
Confusing a local prediction with a global guarantee
The gradient is first-order local information. Large steps can cross into a region with different slope or curvature.
Exercises
-
In one sentence, explain how a gradient differs from a single partial derivative.
-
For , compute and evaluate it at .
-
For the gradient from Problem 2, compute its Euclidean norm at and give the unit direction of steepest increase when the gradient is nonzero.
-
At a point, . Compute the directional derivative along each direction below, first checking whether it is a unit vector:
-
Explain geometrically why the gradient is perpendicular to a smooth contour line .
-
Use Cauchy-Schwarz to explain why no unit direction has a directional derivative larger than .
-
Let . At , use the first-order approximation to estimate , then compare with the exact value.
-
A loss has gradient and learning rate . Compute the gradient-descent displacement and the predicted first-order change in loss.
-
Explain why does not distinguish a local minimum from a saddle point. Name the mathematical object from Lesson 31 that can help.
-
Write TypeScript that uses
numericalGradientto check the analytic gradient of at[1, -2]. Compare the two vectors with a small tolerance. -
In your own words, distinguish the differential as a linear map from the gradient as its Euclidean coordinate representation.
Summary
- The gradient assembles every first partial derivative of a scalar field.
- A directional derivative is the dot product .
- Among unit directions, the gradient points toward steepest local increase.
- The negative gradient points toward steepest local decrease.
- The gradient supplies the first-order approximation .
- The differential is the local linear map; the Euclidean inner product represents it as a gradient vector.
- Gradient descent uses this local information, while curvature limits how far a step can safely trust it.
Next Lesson
Lesson 36 generalizes from scalar outputs to vector outputs. The Jacobian collects the derivatives of every output with respect to every input and makes the local linear-map shape explicit.
Completed Exercises
Handwritten work submitted after completing this lesson.








