Skip to content
AI.info

Mathematical foundations

Gradients, Jacobians, and Hessians

Learn to read multivariable derivatives through shapes, directional derivatives, local linear maps, and curvature matrices.

By the end you can

Visual

A derivative shape table

The derivative shape follows the dimensions of the input and output spaces: scalar to scalar gives a scalar, vector to scalar a gradient vector under a declared convention, vector to vector a Jacobian matrix, and a second derivative of a scalar function a square Hessian. The last row of the table is different in kind. Frameworks frequently never build the Jacobian at all. They evaluate Jacobian–vector and vector–Jacobian products instead, and the reason is a published cost law rather than an implementation habit.

Building the whole m×n Jacobian costs one sweep per input coordinate in forward mode, and one sweep per output coordinate in reverse mode. The same small constant sits in front of both. A 2018 survey of automatic differentiation in the Journal of Machine Learning Research sets the arithmetic out: “if we denote the operation count to evaluate the original function by ops(f), the time it takes to calculate the m×n Jacobian by the forward mode is nc ops(f), whereas the same computation can be done via reverse mode in mc ops(f), where c is a constant guaranteed to be c<6 and typically c∼[2,3] (Griewank and Walther, 2008)”. That constant is below 6, usually between 2 and 3. The survey draws the conclusion in one sentence: “That is to say, reverse mode AD performs better when m≪n.”

That inequality is why a scalar training loss over millions of parameters is differentiated backwards: m is 1 and n is the parameter count. JAX ships the asymmetry as an API rule rather than advice. “jacfwd uses forward-mode automatic differentiation, which is more efficient for 'tall' Jacobian matrices”, while “jacrev uses reverse-mode, which is more efficient for 'wide' Jacobian matrices”. The shape of the map does not only tell you which derivative object exists. It tells you what computing it will cost.

FigureHierarchy · 5 levels
  • Scalar → scalar

    Derivative is a scalar.

    • Vector → scalar

      Gradient is a vector under a declared convention.

      • Vector → vector

        Jacobian is a matrix.

        • Vector → scalar, second order

          Hessian is a square matrix.

          • Tensor program

            Frameworks often use JVPs and VJPs instead of explicit high-order arrays.

The derivative of a multivariable function is a map

For f:ℝⁿ→ℝ, the gradient collects partial derivatives into a vector. For g:ℝⁿ→ℝᵐ, the derivative is a Jacobian matrix that maps small input changes to output changes. A Hessian stores second partial derivatives of a scalar function and describes how the gradient changes locally. These objects are related, but they are not interchangeable. Their shapes follow directly from the function's input and output dimensions.

Both matrices carry a nineteenth-century name, and both names can be traced to a printed page. The functional determinant has been Jacobi's since a memoir he published in Crelle's Journal in 1841. The determinant that became the Hessian arrived the following year, in an 1842 paper by Ludwig Otto Hesse on cubic and quadratic curves; the MacTutor history at St Andrews says he “is remembered particularly for introducing the Hessian determinant”.

The name itself came later, and from someone else: “The Hessian matrix was developed by Ludwig Otto Hesse (1811 – 1874), a German mathematician, though the term was first used by James Joseph Sylvester (1814 – 1897)”. The objects are older than the vocabulary now used to teach them. The vocabulary is what obscures that they answer different questions.

Derivative shape is determined by the map being differentiated.

Comparison

Jacobian and Hessian answer different local questions

Both are matrices in common cases, which makes them easy to confuse. A matrix of the right dimensions will multiply without complaint even when it is the wrong object.

The Jacobian J is first-order and applies to vector-valued outputs. It is the local linear map g(x+Δ)≈g(x)+JΔ, with rows and columns following a stated convention, and it is what sensitivity analysis, coordinate changes and backpropagation are made of. The Hessian H is second-order and applies to scalar-valued outputs: an n×n square matrix for n inputs, symmetric when the mixed partials are regular. It enters the local quadratic model through the term ½ΔᵀHΔ, and the signs of its eigenvalues sort directions into upward, flat and downward. The gradient ∇f is first-order for a scalar output: one entry per input, producing directional derivatives through an inner product.

The distinction that matters in practice is the number of derivatives, not the number of indices. A Jacobian tells you where a perturbation goes. A Hessian tells you how the answer to that question is itself changing as you move. Reading one as the other is the error that shape checking exists to catch.

FigureComparison · 3 columns

Jacobian J

First-order map from input perturbations to output perturbations.

  • Applies to vector-valued outputs
  • Rows or columns follow a stated convention
  • Local approximation: g(x+Δ)≈g(x)+JΔ
  • Used in sensitivity, coordinate changes, and backpropagation

Hessian H

Second-order curvature of a scalar-valued function.

  • Square n×n matrix for n inputs
  • Symmetric when mixed partials are regular
  • Quadratic approximation uses ½ΔᵀHΔ
  • Eigenvalues distinguish local upward, flat, and downward directions

Gradient ∇f

First-order sensitivity of a scalar output.

  • Vector with one entry per input
  • Produces directional derivatives through inner products
  • Depends on coordinate metric
  • Used for first-order optimization

Example

A two-output function

Let g(x,y)=(x²+y, xy). Its Jacobian is [[2x,1],[y,x]] — two rows because the output has two components, two columns because the input has two coordinates. The shape is fixed before any entry is computed.

  • At (1,2): J=[[2,1],[2,1]]. The two rows coincide at this point, so the matrix has rank one here, even though the map is not degenerate everywhere.
  • Perturbation: For Δ=(0.01,−0.02), JΔ=(0,0). Each row gives 2×0.01+1×(−0.02)=0, predicting no first-order output change in either component.
  • Meaning: The chosen perturbation lies locally in a null direction of the Jacobian. That is a statement about this point and this direction only.
  • Limitation: Higher-order terms may still change the output. A zero first-order prediction is a prediction about the linear term, nothing more.
  • Use: The same analysis reveals locally insensitive directions in neural networks and simulators, and it is cheap. The Jacobian–vector product JΔ can be evaluated without ever assembling J.

Analogy

A Jacobian is a gearbox and a Hessian is the road curvature

A Jacobian is a gearbox that converts a small input motion into several output motions. Each input direction produces a characteristic combination of output changes. The Hessian describes how the slope itself changes as you move, like curvature in the terrain around the current point.

Both descriptions are written in whatever coordinates you happened to choose. That is not a pedantic caveat. It is the premise of an entire line of optimization research. Martens opens his 2020 paper on the natural gradient method with exactly that contrast: “Under this interpretation (discussed in detail in Section 6), natural gradient descent is invariant to any smooth and invertible reparameterization of the model, putting it in stark contrast to gradient descent, whose performance is parameterization dependent.” A reparameterization changes the numerical gradient and Hessian. Whether it changes the trajectory of the optimizer depends on which method you chose.

The same programme has been built from the geometry side. Ollivier derived training rules for feedforward networks in 2015 that are “invariant under a number of transformations in data and network representation” — a design goal that exists precisely because ordinary backpropagation is not. The gearbox is real. The units it is written in are your choice, and the numbers change with them.

The Jacobian maps motion; the Hessian describes how the local slope bends.

A gradient answers every directional derivative at once

Along a unit direction u, the directional derivative of f is ∇f(x)·u. The gradient therefore encodes local change along any direction through a single dot product. Under Euclidean geometry it points toward steepest local increase, and its negative points toward steepest decrease.

That last sentence carries a condition that teaching usually drops. Martens attaches it: “The negative gradient −∇h can be interpreted as the steepest descent direction for h in the sense that it yields the greatest instantaneous rate of reduction in h per unit of change in θ, where change in θ is measured using the standard Euclidean norm”. Steepest is defined relative to a way of measuring change in the parameters. Rescale the parameters and you have changed the norm. That changes the gradient coordinates, and it can change the apparent steepest direction even when the underlying function is the same function.

The directional-derivative view is also how gradients are tested in practice, because probing every coordinate is unaffordable. JAX's jax.test_util.check_grads compares automatic derivatives against finite differences, and documents the compromise plainly: “Gradients are only checked in a single randomly chosen direction”. One direction, one inner product, one number to compare. The gradient's defining property, used as a test procedure.

Visual

Match the function to its derivative

The output dimension determines whether local sensitivity is naturally a scalar, a vector, a matrix, or a higher-order object. Scalar to scalar gives an ordinary derivative: one local rate. Vector to scalar gives a gradient: one sensitivity coordinate per input dimension. Vector to vector gives a Jacobian: a local linear transformation between two spaces, m×n for f:ℝⁿ→ℝᵐ. Vector to scalar at second order gives a Hessian: local curvature across input directions, n×n and symmetric under regularity.

Writing the shapes first is not bookkeeping. It is the cheapest available test. A claimed derivative of the wrong shape cannot be repaired by checking the algebra inside it, because the object itself is answering a different question.

FigureHierarchy · 4 levels
  • Scalar → scalar

    Ordinary derivative: one local rate.

    • Vector → scalar

      Gradient: one sensitivity coordinate per input dimension.

      • Vector → vector

        Jacobian: a local linear transformation between spaces.

        • Vector → scalar, second order

          Hessian: local curvature across input directions.

A shape mismatch often reveals a conceptual mismatch before any algebra is checked.

Steps

A derivative-shape audit

Use dimensions before symbols to test a derivative claim. First, write the input and output shapes: state f:ℝⁿ→ℝᵐ explicitly, before writing any partial derivative. Second, choose the derivative object the map actually calls for — gradient, Jacobian or Hessian. Third, test a perturbation: verify that the derivative maps Δx into the expected output shape. A product that will not conform is a claim that cannot be right. Fourth, check symmetry and conventions: confirm Hessian regularity and the row/column orientation your source uses for the Jacobian.

Fifth, validate numerically. Here the audit stops being a discipline you maintain by hand, because both major frameworks ship it as a tested utility. PyTorch's torch.autograd.gradcheck is documented as follows: “Check gradients computed via small finite differences against analytical gradients wrt tensors in inputs that are of floating point or complex type and with requires_grad=True.” Its published signature carries the tolerances it will judge you by — gradcheck(func, inputs, *, eps=1e-06, atol=1e-05, rtol=0.001, ...) — and one warning decides whether a failure is your bug or your dtype: “The default values are designed for input of double precision. This check will likely fail if input is of less precision, e.g., FloatTensor.”

JAX's equivalent is jax.test_util.check_grads, with signature check_grads(f, args, order, modes=('fwd', 'rev'), atol=None, rtol=None, eps=None). It compares automatic derivatives to finite differences, checks gradients in a single randomly chosen direction to stay affordable in high dimensions, and raises AssertionError on mismatch. Step five is not an exhortation to compare local changes at several scales. It is two function calls whose default tolerances are written down and can be read before you trust the result.

FigureProcess · 5 steps
  1. 1. Write input and output shapes

    State f:ℝⁿ→ℝᵐ explicitly.

  2. 2. Choose the derivative object

    Use gradient, Jacobian, or Hessian according to the map.

  3. 3. Test a perturbation

    Verify that the derivative maps Δx into the expected output shape.

  4. 4. Check symmetry and conventions

    Confirm Hessian regularity and Jacobian row/column orientation.

  5. 5. Validate numerically

    Compare predicted local changes with finite perturbations at several scales.

Key idea

A positive Hessian at one point is local evidence

If the Hessian is positive definite at a stationary point, the point is a strict local minimum under regularity conditions. This does not prove global optimality. A positive semidefinite Hessian can be inconclusive, because higher-order terms may determine the shape along flat directions.

In trained neural networks, those flat directions are not an edge case. They are what measurement finds. Sagun and colleagues reported the shape of the spectrum directly, in a 2017 paper revised for the ICLR 2018 workshop track: “In particular, in the context of deep learning, we empirically show that the spectrum of the Hessian is composed of two parts: (1) the bulk centered near zero, (2) and outliers away from the bulk.” Ghorbani and colleagues found the same structure in 2019 with a different instrument — their own stochastic Lanczos quadrature estimator of the full eigenvalue density, run up to ImageNet scale: “To understand the dynamics of training in deep neural networks, we study the evolution of the Hessian eigenvalue density throughout the optimization process”. The large isolated outliers emerge above the near-zero bulk in networks without batch normalization. Batch normalization pushes them back into the bulk.

The other correction to textbook intuition concerns what stationary points usually are. Dauphin and five co-authors stated the case in their 2014 abstract: “Here we argue, based on results from statistical physics, random matrix theory, neural network theory, and empirical evidence, that a deeper and more profound difficulty originates from the proliferation of saddle points, not local minima, especially in high dimensional problems of practical interest.” Pennington and Bahri later attached a number to that geometry, in 2017: “Our analysis predicts and numerical simulations support that for critical points of small index, the number of negative eigenvalues scales like the 3/2 power of the energy.” The count of negative eigenvalues is not incidental detail. It tracks the level of the loss.

None of this requires the full matrix, which is fortunate, because in large models the full matrix cannot be formed. Pearlmutter derived the R{·} differential operator for the Hessian–vector product in Neural Computation in 1994, and his abstract states the cost: “The result is an exact and numerically stable procedure for computing Hv, which takes about as much computation, and is about as local, as a gradient evaluation.” The point of the paper is that one can compute properties of H “obviating any need to calculate the full Hessian”. JAX's Autodiff Cookbook gives the same reasoning as an engineering constraint: “The trick is not to instantiate the full Hessian matrix: if n is large, perhaps in the millions or billions in the context of neural networks, then that might be impossible to store”. Curvature information along a chosen direction costs about one gradient. The n×n array costs everything you have.

Second-order information classifies local geometry, not the entire objective landscape.

Key takeaways