Unit 5 of 6 · about 4 hours · derive, implement

Forward mode, reverse mode, JVPs, VJPs and Hessian-vector products

One reverse pass costs about one forward pass while forward mode costs one pass per input. That cost model decides how every large model is differentiated.

You'll be able to

  • Explain forward mode (JVP) and reverse mode (VJP) and choose between them from input and output counts.
  • Implement forward-mode dual numbers and a reverse-mode accumulation for a small graph without forming any Jacobian.
  • Compute a Hessian-vector product by forward-over-reverse or double backward and check it against a finite difference of gradients.

Covers

Computational graphs and accumulation through branchesForward mode, reverse mode, JVPs and VJPsHessian-vector products
1

Read

In this order, only the parts listed

0 of 4 done

  1. Forward and reverse-mode automatic differentiationMIT 18.S096 - Matrix Calculus for ML

    Lecture 5, Part 3 notes (course notes Chapter 8, Sections 8.1 and 8.4): forward mode via dual numbers with the Babylonian square-root example, the cost rule (forward scales with inputs, reverse with outputs) and forward-over-reverse Hessian-vector products (Section 8.4.1)

  2. Dual numbers and both AD enginesHands-On ML - Autodiff notebook

    Cells Forward mode autodiff using dual numbers (DualNumber class; seeding x gives 24.0 and seeding y gives 10.0 for f at (3, 4)), Reverse mode autodiff (toy engine with backpropagate on the same f), Reverse mode autodiff using PyTorch (torch.autograd.grad with create_graph=True for second order)

    Rebuild the dual-number and reverse engines independently in your own file; do not copy the notebook cells.

  3. Backpropagation cost and graph equationsMathematics for Machine Learning - Ch. 5

    Section 5.6 (pp. 159-164): explicit gradient (5.109)-(5.110) motivating the cost worry, forward and backward graph computation with accumulation over parents (5.135)-(5.145), reverse mode cheaper when inputs outnumber labels

  4. Reverse-mode graph, accumulation and grad modesPyTorch Autograd mechanics

    How autograd encodes the history (reverse-mode graph recording); accumulation into .grad and torch.autograd.grad as a vector-Jacobian product; Grad Modes with Default, No-grad and Inference behavior

    Consult after your handwritten engine. Skip multithreading and in-place correctness checks.

2

Focus on

  • A JVP pushes a tangent forward (, one column); a VJP pulls a cotangent back (, one row). Forward cost scales with inputs, reverse with outputs.
  • Reverse mode must keep or recompute forward values. That memory-for-time trade is the price of the single pass.
  • Dual numbers evaluate exactly since ; seeding each input in turn gives one gradient column per pass.
  • An HVP is the JVP of the gradient. It needs so the differentiation order does not matter.
Common mistakes · 3
  • Building the full Jacobian to get one JVP or VJP. Propagate the vector instead; the matrix is never needed.
  • Calling torch.autograd.functional.jacobian for a check on wide maps. It materializes the matrix; use grad (a VJP) or directional differences.
  • Assuming reverse mode always wins. For , one forward pass beats reverse passes, so count outputs before choosing.
3

Try it yourself

Closed-book, before checking anything

Closed-book, implement a minimal DualNumber type () and recover and for at (3, 4), matching the notebook analytic values. State the cost rule: inputs with 1 output take 1 reverse pass versus forward passes. Verify an HVP without forming on in with , drawing (), , and in that order with standard_normal from numpy.random.default_rng(11) in float64: the centered difference at misses by 2.2e-11 in the 2-norm. The gradient is linear, so that gap is rounding alone.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Compare forward and reverse cost for .Answer

    Reverse costs about one forward pass; forward costs about 1000 passes, one per input. Uses: each primitive JVP or VJP costs forward equivalents.

  2. 2.What do a JVP and a VJP compute, and which one does torch.autograd.grad perform?Answer

    JVP computes (pushforward of a tangent); VJP computes (pullback of a cotangent). torch.autograd.grad performs a VJP.

  3. 3.Give the two HVP routes and the assumption each needs.Answer

    Forward-over-reverse (JVP of the gradient) or double backward (VJP of the gradient). Both need so mixed partials agree and the order is irrelevant.

  4. 4.Why does reverse mode store forward values?Answer

    Local pullbacks need operand values (for example needs and ). The forward pass saves them; recomputation trades time for memory.

5

Practice

You're done when

Your dual-number gradient matches (24.0, 10.0), your HVP matches a finite difference of gradients to 1e-9, and you pick the AD mode from without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 2 more in the section
  • MIT 18.S096 - Matrix Calculus for MLMIT OpenCourseWare · primary

    Use lectures 1-5, the Hessian portion of lecture 7 and computational-graph portion of lecture 8; do the supplied problems and notebooks.

  • Hands-On ML - Autodiff notebookAurelien Geron · required

    Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.

  • PyTorch Autograd mechanicsPyTorch documentation · required

    Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.

  • Mathematics for Machine Learning - Ch. 5Deisenroth, Faisal & Ong · reference

    Use Chapter 5 as the compact derivative, Jacobian, chain-rule and backpropagation reference.

Also in this section

  • MIT 18.02SC - Multivariable Calculus Unit 2MIT OpenCourseWare · optional

    Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.

  • CS231n - Linear ClassificationStanford University · support

    Read the Softmax classifier section for the cross-entropy loss formula with both the information-theory and MLE views, and Practical issues: Numeric stability for the max-subtraction trick. Derive the softmax gradient by hand; do not implement the course assignment here.