Gradients
The gradient of a scalar function is the vector of its coordinate partial derivatives in input order.
On this page 10 sections
01Collect the coordinate rates#
A scalar function with several inputs has a partial derivative for each input. The gradient collects those partial derivatives in input order. It lets us keep the local effects of all coordinates together.
For , the -partial is and the -partial is . We use column vectors:
The symbol is called nabla. Read as “the gradient of .” Its first entry belongs to the first input, , and its second belongs to .
At , the function value is , while the gradient is . The superscript transposes the written row into a column. A value of seven and rates of four and three answer different questions.
The diagram draws as an arrow from the origin to make its components visible on equal scales. It represents the gradient evaluated at ; its drawn starting point is not the evaluation point.
In an input-space plot, the same gradient direction could instead be attached at . Separating the point where we evaluate from the vector we obtain prevents a common reading mistake.
02Evaluate every entry at the same point#
For a second function, , both partial derivatives depend on the input:
At , the gradient is . The negative second entry says that a small positive change in decreases there, while a small positive change in increases it.
First build the symbolic gradient, then substitute the same point into every entry. Using an -derivative at one point and a -derivative at another would not give the gradient at either point.
Also keep the coordinate order. Swapping to attaches each rate to the wrong input.
Order : , . Find .
Show answer and explanation
Put the x partial first.
03Predict a small change in the output#
The gradient becomes useful when several inputs move. For a differentiable function and a small input change , the first-order prediction is
This dot product multiplies each coordinate's rate by its change, then adds the contributions. With column vectors, the transpose makes a row times a column, producing one scalar change.
Return to at , where the gradient is . Increase by and by .
The actual change is . The small difference comes from curvature, which the first-order prediction leaves out.
A zero partial derivative still occupies a coordinate slot. For , the gradient is , not just .
The zero says that this function has no dependence on . For other functions, a zero derivative at one point may only describe that point; it does not establish independence everywhere.
Order : , , . A learner drops the zero. Repair .
Show answer and explanation
Keep the y slot, even when its value is zero.
04Follow the gradient as the input changes#
Some partial derivatives involve several coordinates. For , treating the other coordinate as constant gives
At , this is . At , it is . Changing only changed both entries of the gradient.
The gradient is generally a function of position, not one permanently fixed vector. A gradient computed before a parameter update belongs to the old parameter state.
This is why optimization usually evaluates a new gradient after moving. Reusing the previous one forever follows information from a location the model has already left.
Cost . At , find .
Show answer and explanation
and .
05Connect the gradient to optimization#
Under Euclidean distance, a nonzero gradient points in the direction of greatest first-order increase per unit of input movement. The negative gradient points toward greatest first-order decrease.
The qualification “first-order” matters. It describes sufficiently small movement near the evaluation point. A long step can pass through a minimum and increase the function.
For a scalar loss depending on a weight matrix , we can collect the entrywise partial derivatives back into a matrix of the same shape:
This is the same organizing idea: one rate for every independent parameter. A matrix-shaped gradient of a scalar loss is different from the full derivative of a vector-valued function, which the Jacobian represents.
One coordinate, one partial, one gradient slot.
- Assemble a gradient from partial derivatives in coordinate order.
Further questions
Can I add the partials instead?
Sources & further reading
- [1]