Skip to content
Glacius
CalculusConcept reference

Gradients

The gradient of a scalar function is the vector of its coordinate partial derivatives in input order.

On this page 10 sections
  1. Overview
  2. Collect the coordinate rates
  3. Evaluate every entry at the same point
  4. Predict a small change in the output
  5. Follow the gradient as the input changes
  6. Connect the gradient to optimization
  7. Key takeaway
  8. Further questions
  9. Sources & further reading
  10. Concept connections

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 f(x,y)=x2+3yf(x,y)=x^2+3y, the xx-partial is 2x2x and the yy-partial is 33. We use column vectors:

∇f(x,y)=(2x3).\nabla f(x,y)=\begin{pmatrix}2x\\3\end{pmatrix}.

The symbol ∇\nabla is called nabla. Read ∇f\nabla f as “the gradient of ff.” Its first entry belongs to the first input, xx, and its second belongs to yy.

At p=(2,1)p=(2,1), the function value is f(p)=7f(p)=7, while the gradient is ∇f(p)=(4,3)T\nabla f(p)=(4,3)^T. The superscript TT transposes the written row into a column. A value of seven and rates of four and three answer different questions.

For f(x,y)=x²+3y at (2,1), the gradient is (4,3). This free vector is anchored at the origin to show its components, not the parameter’s position. The x and y coordinate units are equal.For f(x,y)=x²+3y at (2,1), the gradient is (4,3). This free vector is anchored at the origin to show its components, not the parameter’s position. The x and y coordinate units are equal.
Figure 1For f(x,y)=x²+3y at (2,1), the gradient is (4,3). This free vector is anchored at the origin to show its components, not the parameter’s position. The x and y coordinate units are equal.
Link to this figure ↗Download SVGDownload PNG

The diagram draws (4,3)(4,3) as an arrow from the origin to make its components visible on equal scales. It represents the gradient evaluated at (2,1)(2,1); its drawn starting point is not the evaluation point.

In an input-space plot, the same gradient direction could instead be attached at (2,1)(2,1). 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, h(x,y)=x2+3y2h(x,y)=x^2+3y^2, both partial derivatives depend on the input:

∇h(x,y)=(2x6y).\nabla h(x,y)=\begin{pmatrix}2x\\6y\end{pmatrix}.

At (2,−1)(2,-1), the gradient is (4,−6)T(4,-6)^T. The negative second entry says that a small positive change in yy decreases hh there, while a small positive change in xx increases it.

First build the symbolic gradient, then substitute the same point into every entry. Using an xx-derivative at one point and a yy-derivative at another would not give the gradient at either point.

Also keep the coordinate order. Swapping (4,−6)T(4,-6)^T to (−6,4)T(-6,4)^T attaches each rate to the wrong input.

Check your reasoning

Order (x,y)(x,y): fy=−2f_y=-2, fx=5f_x=5. Find ∇f\nabla f.

  1. A(−2,5)(-2,5)
  2. B33
  3. C(5,−2)(5,-2)
Show answer and explanation
(5,−2)(5,-2)

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 Δp\Delta p, the first-order prediction is

Δf≈∇f(p)TΔp.\Delta f\approx \nabla f(p)^T\Delta p.

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 f=x2+3yf=x^2+3y at (2,1)(2,1), where the gradient is (4,3)T(4,3)^T. Increase xx by 0.010.01 and yy by 0.020.02.

Δf≈4(0.01)+3(0.02)=0.10.\begin{gathered}\Delta f\approx4(0.01)+3(0.02)\\=0.10.\end{gathered}

The actual change is f(2.01,1.02)−f(2,1)=0.1001f(2.01,1.02)-f(2,1)=0.1001. The small difference comes from curvature, which the first-order prediction leaves out.

A zero partial derivative still occupies a coordinate slot. For r(x,y)=x2r(x,y)=x^2, the gradient is (2x,0)T(2x,0)^T, not just 2x2x.

The zero says that this function has no dependence on yy. For other functions, a zero derivative at one point may only describe that point; it does not establish independence everywhere.

Check your reasoning

Order (x,y,z)(x,y,z): fx=2f_x=2, fy=0f_y=0, fz=−1f_z=-1. A learner drops the zero. Repair ∇f\nabla f.

  1. A(2,0,−1)(2,0,-1)
  2. B(2,−1)(2,-1)
  3. C11
Show answer and explanation
(2,0,−1)(2,0,-1)

Keep the y slot, even when its value is zero.

04Follow the gradient as the input changes#

Some partial derivatives involve several coordinates. For s(x,y)=x2ys(x,y)=x^2y, treating the other coordinate as constant gives

∇s(x,y)=(2xyx2).\nabla s(x,y)=\begin{pmatrix}2xy\\x^2\end{pmatrix}.

At (2,3)(2,3), this is (12,4)T(12,4)^T. At (1,3)(1,3), it is (6,1)T(6,1)^T. Changing only xx 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.

Check your reasoning

Cost C(a,b)=a2+2abC(a,b)=a^2+2ab. At (a,b)=(1,3)(a,b)=(1,3), find ∇C\nabla C.

  1. A(2,8)(2,8)
  2. B(8,2)(8,2)
  3. C(7,7)(7,7)
Show answer and explanation
(8,2)(8,2)

Ca=8C_a=8 and Cb=2C_b=2.

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 WW, we can collect the entrywise partial derivatives back into a matrix of the same shape:

(∇WL)ij=∂L∂Wij.(\nabla_W L)_{ij}=\frac{\partial L}{\partial W_{ij}}.

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.

Key takeaway

One coordinate, one partial, one gradient slot.

  • Assemble a gradient from partial derivatives in coordinate order.

Further questions

Can I add the partials instead?
Adding them loses which rate belongs to which coordinate. A gradient retains every entry, including zeros.

Sources & further reading

  1. [1]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Gradients.” Math behind ML. /learn/c-gradient