One-Line Summary: The mathematical machinery for measuring how outputs change with inputs -- the foundation of all learning algorithms.

Prerequisites: Vectors and Matrices, single-variable calculus (limits, derivatives).

What Are Derivatives and Gradients?

Imagine you are standing on a hilly landscape and you want to walk downhill as efficiently as possible. At any point you can feel the steepness of the slope in every direction. The gradient is the vector that points in the direction of the steepest uphill climb, and its magnitude tells you how steep that climb is. Walk in the opposite direction and you descend most rapidly. This is, in essence, what every gradient-based learning algorithm does: it evaluates the gradient of a loss function with respect to model parameters and steps in the negative gradient direction.

Formally, the derivative of a scalar function at a point is:

When depends on multiple variables, we generalize to partial derivatives and collect them into a gradient vector.

How It Works

Partial Derivatives

For , the partial derivative with respect to measures how changes when only varies:

The Gradient Vector

The gradient collects all partial derivatives into a single vector:

Two critical properties: (1) points in the direction of steepest ascent, and (2) is orthogonal to level sets of (the contours where is constant).

Directional Derivatives

The rate of change of in an arbitrary direction (unit vector) is:

This is maximized when is parallel to (confirming the steepest ascent interpretation) and zero when is perpendicular to it.

The Chain Rule

The chain rule is the single most important calculus result for ML. If , then:

In the multivariate case, if where and :

This is a product of Jacobian matrices. Backpropagation in neural networks is precisely the repeated application of this multivariate chain rule, propagating derivatives from the loss backward through each layer.

The Jacobian Matrix

For a vector-valued function , the Jacobian collects all first-order partial derivatives:

The Jacobian generalizes the gradient to functions with vector outputs. In a neural network, the Jacobian of a layer's output with respect to its input determines how errors propagate.

The Hessian Matrix

For , the Hessian contains all second-order partial derivatives:

The Hessian encodes the curvature of . If is positive definite at a critical point, that point is a local minimum. If it has mixed signs, the point is a saddle point. Newton's method uses the Hessian to take curvature-informed steps: .

Automatic Differentiation

Computing derivatives in practice uses one of three approaches:

  • Symbolic differentiation: Applies calculus rules to produce an exact derivative expression. Can lead to expression swell for complex functions.
  • Numerical differentiation: Approximates via finite differences . Simple but suffers from floating-point errors and scales poorly with dimension.
  • Automatic differentiation (AD): Decomposes the function into elementary operations and applies the chain rule mechanically. Forward mode computes one directional derivative per pass; reverse mode (backpropagation) computes the full gradient in one pass regardless of the number of parameters.

Modern ML frameworks (PyTorch, JAX, TensorFlow) all implement reverse-mode AD, enabling gradients of loss functions with millions of parameters to be computed with roughly the same cost as a single forward evaluation.

Why It Matters

Every parameter update in supervised, unsupervised, and reinforcement learning relies on gradient computation. Without efficient, accurate gradient calculation, training deep networks with billions of parameters would be impossible. The gradient tells the optimizer which direction to move and how far -- it is the feedback signal that enables learning.

Key Technical Details

  • The gradient lives in the same space as (parameter space), not in the output space.
  • For , reverse-mode AD computes the full gradient in backward passes (relative to the forward pass cost). Forward-mode requires passes.
  • The Hessian has entries, making it impractical to store for large models. Approximations include diagonal Hessians, Hessian-vector products, and the Fisher information matrix.
  • Clairaut's theorem: If second partials are continuous, , so the Hessian is symmetric.
  • Vanishing and exploding gradients: In deep networks, repeated chain rule applications can cause gradients to shrink toward zero or grow unboundedly. Architectural choices (residual connections, layer normalization) and careful initialization mitigate this.

Common Misconceptions

  • "The gradient IS the derivative." The gradient is the derivative of a scalar function with respect to a vector. For vector-valued functions, the appropriate generalization is the Jacobian, not the gradient.
  • "Zero gradient means global minimum." A zero gradient () is a necessary condition for any critical point: local minimum, local maximum, or saddle point. The Hessian is needed to distinguish among these.
  • "Automatic differentiation is just numerical differentiation." AD computes exact derivatives (up to floating-point precision) using the chain rule, not finite differences. It is fundamentally different in both accuracy and computational cost.

Connections to Other Concepts

  • cost-latency-optimization.md: Gradients provide the update direction; the Hessian informs second-order methods and learning rate selection.
  • vectors-and-matrices.md: Gradients are vectors; Jacobians and Hessians are matrices. Matrix calculus is the language of multivariable derivatives.
  • maximum-likelihood-estimation.md: MLE requires differentiating the log-likelihood with respect to parameters and setting the gradient to zero.
  • information-theory.md: The gradient of cross-entropy loss is directly related to the KL divergence between predicted and true distributions.

Further Reading

  • Rudin, Principles of Mathematical Analysis (1976) -- Rigorous treatment of limits, derivatives, and the multivariate chain rule.
  • Griewank & Walther, Evaluating Derivatives (2008) -- The definitive reference on automatic differentiation.
  • Baydin et al., "Automatic Differentiation in Machine Learning: a Survey" (2018) -- Accessible overview of AD modes and their use in ML frameworks.