Partial derivatives and gradients
偏导数与梯度
Connect changing one parameter at a time to moving in any direction, and derive the gradient's local approximation and steepest-descent direction.
partial-derivativesHow should two controls move together?
Consider the invented function . At , increasing only raises the function at local rate 1. Increasing only changes it at rate -3. Each rate holds the other control fixed.
Moving both parameters requires more than one partial derivative. Collecting the coordinatewise rates gives the gradient. When the function is differentiable, this collection also predicts the first-order change caused by any sufficiently small joint displacement.
From coordinate changes to directional changes
For , partial derivative holds other coordinates fixed. The unit vector has a 1 in coordinate and zeros elsewhere. This article uses a column gradient:
Some textbooks use row gradients. Either convention works with the corresponding transposes in matrix products; a shape convention is not a mathematical disagreement. MML, Section 5.2
Differentiating the example gives:
If is differentiable at , small displacements satisfy:
The directional derivative along a unit vector is therefore . Under a Euclidean unit-length constraint, Cauchy–Schwarz gives minimum . For nonzero gradients it is attained by . This explains steepest local descent in Euclidean geometry. D2L, Section 2.4.3
At , displacement predicts first-order change -0.1, taking the value from 2 to approximately 1.9. Direct calculation gives 1.9016. Second-order terms explain the discrepancy; the gradient is not wrong.
Check coordinates and a direction
This complete Python example estimates each partial derivative, then checks the rate along the negative-gradient unit direction. It uses a small function with moderate finite values.
import math
def numerical_gradient(f, point, h=1e-5):
if not math.isfinite(h) or h <= 0:
raise ValueError("invalid step")
result = []
for i in range(len(point)):
plus, minus = list(point), list(point)
plus[i] += h
minus[i] -= h
result.append((f(plus) - f(minus)) / (2 * h))
return result
def f(p):
x, y = p
return x * x + x * y + 2 * y * y
p = [1.0, -1.0]
g = numerical_gradient(f, p)
assert all(abs(a - b) < 1e-8 for a, b in zip(g, [1, -3]))
u = [-1 / math.sqrt(10), 3 / math.sqrt(10)]
h = 1e-5
plus = [x + h * v for x, v in zip(p, u)]
minus = [x - h * v for x, v in zip(p, u)]
assert abs((f(plus) - f(minus)) / (2 * h) + math.sqrt(10)) < 1e-8
assert abs(f([0.99, -0.97]) - 1.9016) < 1e-12
Central differences need function evaluations for a complete gradient, plus construction of perturbed vectors. This becomes expensive with many parameters and costly evaluations. Automatic differentiation often reuses computational structure instead of running the full model twice per parameter.
Existing partial derivatives need not give a linear approximation
Consider away from the origin, with . It is zero on either coordinate axis, so both partial derivatives at the origin are 0. Along , however, its value remains : it is not even continuous at the origin. Arranging the partial derivatives into a vector does not create a valid joint linear approximation for a nondifferentiable function.
Holding other variables fixed also differs from following an actual path. Along , has derivative 8 at , while at is 3. The path changes the other variable too, and its contribution must be included.
Steepest descent depends on how step length is measured. Changing parameter units or the norm changes the meaning of an equally large step. The negative Euclidean gradient is not a direction independent of coordinate scale. Finite-step improvement also requires a suitable step size; a local result does not justify arbitrary large updates.
Match the number to the question
- What is the example’s gradient at ? Answer: .
- At , what is the directional derivative along positive ? Answer: -3.
- Does a zero gradient prove global optimality? Answer: no. Curvature, constraints, and global structure still matter when first-order information vanishes.
- Do convergent finite differences on two coordinate axes prove multivariable differentiability? Answer: no. The function above is a counterexample.
Gradients connect local calculations
The chain rule combines sensitivities between groups of variables, and backpropagation passes loss sensitivity from outputs toward parameters. Natural gradients and preconditioning further change the geometry of a step. Fix the variables, shapes, coordinate conventions, and step metric before comparing update formulas.
Make a prediction, then check
For f(x,y)=x²+xy+2y², what is the rate along y=x at x=1?
Learning objectives
- Calculate partial derivatives, gradients, and first-order change along a specified direction.
- Distinguish holding other variables fixed, moving along a path, and the assumptions needed for differentiability.
Prerequisites & connections
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Dive into Deep Learning — Calculus
Aston Zhang; Zachary C. Lipton; Mu Li; Alexander J. Smola · 2023
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Recalculated gradient (1,−3), negative-gradient unit-direction rate −√10, joint-displacement value 1.9016, and derivative 8 along y=x.
- Defined e_i and the column-gradient convention; checked the partials-without-continuity counterexample and steepest-descent conditions under Euclidean unit steps.
- Compared arguments, numbers, examples, limitations, and exercise answers across languages; structural validation separately compares display formulas, code, sources, and sections. No independent human peer review.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §5.2, PDF pp. 152–153: partials and gradient conventions
- Dive into Deep Learning — Calculus · §2.4.3: gradients and directional change
Related topics