Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Matrix and vector calculus

Vector–Jacobian and Jacobian–Vector Products

Autodiff never builds a full Jacobian. Forward mode pushes a direction through the program (a JVP, J v), and reverse mode pulls output weights back (a VJP, uᵀJ). Learn which one to use, build forward mode from dual numbers, and use both for matrix-free Newton steps, Hessian traces and Lipschitz bounds.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Two questions you can ask a machine

Picture a machine with many knobs (inputs) and many dials (outputs). You can ask it two kinds of question:

  • Forward: “If I turn the knobs a little in this combination, how do all the dials move?”
  • Backward: “I care about this combination of dials. How much does each knob matter to it?”

The first question is a JVP. The second is a VJP. Training a neural network asks the backward question about a single dial, the loss. That’s backpropagation.

Pushing vectors forward (JVP) and pulling covectors back (VJP)
f(x, y) = (x² − y, x + y²/2) · J = [[1.60, −1], [1, 0.50]]
input space (x, y)output space f(x, y)
JVP: J v = (0.89, 1.12) · VJP: Jᵀu = (0.07, 0.93) · uᵀ(Jv) = 0.5237 = (Jᵀu)ᵀv = 0.5237
Grey: the true image of a small circle. Dashed: its image under J (an ellipse). The JVP carries an input direction forward to an output direction. The VJP carries output weights back to an input-space gradient. They always agree on uᵀJv.
Cost of the full Jacobian of f: ℝⁿ → ℝᵐ
forward mode: 1000 JVP passes · reverse mode: 1 VJP passes → use reverse (VJP) · a scalar loss: one backward pass gives the whole gradient
A robot arm asks both questions

A robot arm’s Jacobian maps joint speeds to hand speed (forward: “move the joints like this, where does the hand go?”). Its transpose maps a force at the hand back to joint torques (backward: “push here, what does each motor feel?”). Every manipulator controller uses both products.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Asking how all outputs move for one input nudge is a…
  2. 2. Asking how every input affects a single loss is a…
  3. 3. f(x) = 3x. Nudge x by 0.1: the output moves by…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The two products

For f:Rn→Rmf:\mathbb R^n\to\mathbb R^m with Jacobian J∈Rm×nJ\in\mathbb R^{m\times n} at x\mathbf x:

JVP (forward / tangent) VJP (reverse / cotangent)
Input direction v∈Rn\mathbf v\in\mathbb R^n weights u∈Rm\mathbf u\in\mathbb R^m
Output Jv∈RmJ\mathbf v\in\mathbb R^m J⊤u∈RnJ^\top\mathbf u\in\mathbb R^n
Meaning directional derivative ddtf(x+tv)\frac{d}{dt}f(\mathbf x + t\mathbf v) gradient of u⊤f(x)\mathbf u^\top f(\mathbf x)
Picks out a column of JJ (when v=ej\mathbf v = \mathbf e_j) a row of JJ (when u=ei\mathbf u = \mathbf e_i)

Example: for f(x)=Axf(\mathbf x) = A\mathbf x with A=[1234]A = \begin{bmatrix}1&2\\3&4\end{bmatrix}: Jv=AvJ\mathbf v = A\mathbf v, so v=(1,1)\mathbf v = (1,1) gives (3,7)(3,7). And J⊤u=A⊤uJ^\top\mathbf u = A^\top\mathbf u, so u=(1,0)\mathbf u = (1,0) gives the first row (1,2)(1,2).

Dual numbers: forward mode in one idea

Invent a number ε\varepsilon with ε2=0\varepsilon^2 = 0. Then (2+ε)2=4+4ε+ε2=4+4ε(2+\varepsilon)^2 = 4 + 4\varepsilon + \varepsilon^2 = 4 + 4\varepsilon. The ε\varepsilon-part, 4, is exactly ddxx2\frac{d}{dx}x^2 at 2. Running any program on x+εx + \varepsilon returns f(x)+f′(x)εf(x) + f'(x)\varepsilon, with exact derivatives and no step size.

📝 Quick check (Level 1)
0/3 answered
  1. 1. For f: ℝ⁵ → ℝ², a JVP returns a vector of length…
  2. 2. And a VJP returns a vector of length…
  3. 3. (3 + ε)² with ε² = 0 has ε-part…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

How autodiff computes them

Forward mode (JVP): carry a tangent v˙\dot v next to every value. Each primitive updates it with its local derivative: (ab)˙=a˙ b+a b˙\dot{(ab)} = \dot a\,b + a\,\dot b and (sin⁡a)˙=cos⁡a a˙\dot{(\sin a)} = \cos a\,\dot a. Seeding inputs with v\mathbf v yields JvJ\mathbf v in one pass, in the same order as the program, with no stored tape.

Reverse mode (VJP): run forward and store values, then propagate adjoints backwards (A3.3-5). Seeding outputs with u\mathbf u yields J⊤uJ^\top\mathbf u in one backward pass.

Composition. For f=g∘hf = g\circ h, J=JgJhJ = J_gJ_h:

  • JVP: Jv=Jg(Jhv)J\mathbf v = J_g(J_h\mathbf v). Push through hh first, then gg.
  • VJP: J⊤u=Jh⊤(Jg⊤u)J^\top\mathbf u = J_h^\top(J_g^\top\mathbf u). Pull back through gg first, then hh.

Neither ever forms a matrix product. Each step is a matrix-vector product with a local Jacobian, which is usually cheap and often structured (diagonal for element-wise operations).

Which one to use

Task Best tool Passes
gradient of a scalar loss, nn parameters VJP 1
full Jacobian, n≪mn \ll m (few inputs) JVPs nn
full Jacobian, m≪nm \ll n (few outputs) VJPs mm
sensitivity of a trajectory to one parameter JVP 1
Hessian–vector product JVP of a VJP about 1 gradient
🐍 Exercise 1: forward mode with dual numbers, reverse mode by hand
Implement dual numbers (+, −, ×, sin, exp) and use them to compute J v for f(x, y) = (xy, sin x + y, e^(xy) − x). Hand-write the matching VJP and verify both against a finite-difference Jacobian and the dot-product test.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Full Jacobian of f: ℝ¹⁰ → ℝ⁴ with fewest passes uses…
  2. 2. For f = g∘h, a VJP applies…
  3. 3. Forward mode needs a stored tape of intermediate values?

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

The adjoint identity

For any u\mathbf u and v\mathbf v, u⊤(Jv)=(J⊤u)⊤v\mathbf u^\top(J\mathbf v) = (J^\top\mathbf u)^\top\mathbf v. Both sides equal ∑i,juiJijvj\sum_{i,j}u_iJ_{ij}v_j. The VJP is the adjoint (transpose) of the JVP as linear maps. This gives the standard dot-product test for a hand-written backward pass: pick random u\mathbf u and v\mathbf v and check that the two sides agree to machine precision (Practice Code 4). The test is used routinely for adjoint codes in weather prediction and seismic imaging.

Why dual numbers are exact

Take a polynomial or analytic ff. By Taylor’s theorem, f(a+bε)=f(a)+f′(a)bε+12f′′(a)b2ε2+⋯f(a + b\varepsilon) = f(a) + f'(a)b\varepsilon + \tfrac12f''(a)b^2\varepsilon^2 + \cdots, and every term from ε2\varepsilon^2 on vanishes. The arithmetic rules for duals ((a+bε)(c+dε)=ac+(ad+bc)ε(a+b\varepsilon)(c+d\varepsilon) = ac + (ad + bc)\varepsilon) are the sum and product rules, so by induction over the program, every intermediate carries its exact derivative. There’s no truncation error, unlike finite differences (A3.1-2).

Tangents and cotangents

Geometrically, JJ pushes forward tangent vectors (velocities) from input space to output space. J⊤J^\top pulls back covectors (gradients, “measurement directions”) from output space to input space. This is why a gradient has the shape of the input, not the output. It’s also why in robotics x˙=J(q)q˙\dot{\mathbf x} = J(\mathbf q)\dot{\mathbf q} while τ=J(q)⊤F\boldsymbol\tau = J(\mathbf q)^\top\mathbf F: velocities push forward, and forces (covectors, since power F⊤x˙=τ⊤q˙\mathbf F^\top\dot{\mathbf x} = \boldsymbol\tau^\top\dot{\mathbf q} is conserved) pull back.

Hessian–vector products

The Hessian is the Jacobian of the gradient. So Hv=ddt∇L(x+tv)∣t=0H\mathbf v = \frac{d}{dt}\nabla L(\mathbf x + t\mathbf v)\big|_{t=0}, a JVP of the VJP (“forward-over-reverse”). It costs a small constant times one gradient, regardless of dimension (Pearlmutter, 1994). With HVPs alone you can run conjugate gradient on Hd=−gH\mathbf d = -\mathbf g and take Newton steps without ever storing HH (A2.4-7).

Exercise 3.1: Check the adjoint identity for J = [[1, 2], [0, 1]], u = (1, 1), v = (1, 1)

Jv=(3,1)J\mathbf v = (3,1), so u⊤Jv=4\mathbf u^\top J\mathbf v = 4. J⊤u=(1,3)J^\top\mathbf u = (1,3), so (J⊤u)⊤v=4(J^\top\mathbf u)^\top\mathbf v = 4. ✓

📝 Quick check (Level 3)
0/3 answered
  1. 1. uᵀ(Jv) = (Jᵀu)ᵀv says the VJP is the ___ of the JVP.
  2. 2. Dual numbers have truncation error of order…
  3. 3. Joint torques from a hand force use…

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Matrix-free second-order methods

Second-order information is often too large to store. A model with 10810^8 parameters has a Hessian with 101610^{16} entries. HVPs make it usable:

  • Hessian-free optimization (Martens, 2010) runs truncated Newton-CG with HVPs or Gauss–Newton vector products.
  • Hutchinson’s estimator (Hutchinson, 1989): tr⁡H≈1k∑izi⊤Hzi\operatorname{tr}H \approx \frac1k\sum_i\mathbf z_i^\top H\mathbf z_i with random ±1\pm1 vectors. It’s used for sharpness measures, for the divergence in continuous normalizing flows (Grathwohl et al., 2019), and in score matching.
  • Top eigenvalues of HH come from Lanczos or power iteration on HVPs. This is how curvature spectra of deep networks are measured (Ghorbani et al., 2019).
🐍 Exercise 2 (research): Newton-CG and Hutchinson with HVPs only
For L2-regularized logistic regression, write an HVP as the JVP of the gradient (no Hessian). Use it in conjugate gradient to take Newton steps (watch quadratic convergence) and to estimate tr(H) with Rademacher probes.

More uses in research and engineering

  • Lipschitz control. Power iteration with alternating JVPs and VJPs estimates ∥J∥2\|J\|_2. That’s the basis of spectral normalization for stable GAN and robust-controller training (Miyato et al., 2018) and of certified robustness bounds (M1) (Practice Code 5).
  • Jacobian-free Newton–Krylov (Knoll and Keyes, 2004). Implicit time-stepping in plasma, combustion and re-entry flow solvers needs only JvJ\mathbf v products inside GMRES.
  • Implicit differentiation. To differentiate through an optimizer, an equilibrium or a physics solve F(z∗,θ)=0F(\mathbf z^*, \theta) = 0, solve one linear system with VJPs and never unroll the solver (Blondel et al., 2022; deep equilibrium models, Bai et al., 2019).
  • Trajectory sensitivities. Forward mode suits “how does this whole trajectory change if the payload mass changes?” (one parameter, many outputs). Reverse/adjoint mode suits “how does final miss distance depend on all controls?” (many parameters, one output).
  • JAX’s design. jvp is the primitive, linearize caches the linear map, and vjp is obtained by transposing that linear map (Frostig et al., 2021). The adjoint identity is built right into the system.

References

  1. B. Pearlmutter (1994). “Fast exact multiplication by the Hessian.” Neural Computation, 6(1).
  2. M. Hutchinson (1989). “A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines.” Communications in Statistics: Simulation and Computation, 18(3).
  3. J. Martens (2010). “Deep learning via Hessian-free optimization.” ICML.
  4. A. G. Baydin, B. Pearlmutter, A. Radul, J. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR, 18.
  5. T. Miyato, T. Kataoka, M. Koyama, Y. Yoshida (2018). “Spectral normalization for generative adversarial networks.” ICLR.
  6. D. Knoll, D. Keyes (2004). “Jacobian-free Newton–Krylov methods: a survey of approaches and applications.” Journal of Computational Physics, 193.
  7. M. Blondel et al. (2022). “Efficient and modular implicit differentiation.” NeurIPS.
  8. R. Frostig, M. Johnson, D. Maclaurin, A. Paszke, A. Radul (2021). “Decomposing reverse-mode automatic differentiation.” LAFI workshop, arXiv:2105.09469.
  9. B. Ghorbani, S. Krishnan, Y. Xiao (2019). “An investigation into neural net optimization via Hessian eigenvalue density.” ICML.
  10. B. Siciliano, L. Sciavicco, L. Villani, G. Oriolo (2009). Robotics: Modelling, Planning and Control. Springer. (Velocity and force Jacobians.)