Calculus & Automatic Differentiation
Training is repeated differentiation through composed programs. Shape-aware derivatives and numerical checks are the fastest way to separate a wrong model from a wrong implementation.
Section 2 of 5 · 6 units · about 20 hours
By the end you can Derive a branched matrix computation, implement a small reverse-mode autodiff engine, and isolate a seeded gradient error.
Units
- Limits, partial and directional derivatives, and gradients3h
- Jacobians, Hessians and Taylor approximations3h
- The chain rule and matrix derivatives4h
- Gradients of logistic, softmax cross-entropy and regularized losses3h
- Forward mode, reverse mode, JVPs, VJPs and Hessian-vector products4h
- Backpropagation through dense layers and gradient checks3h
Build it
Mini project · about 8 to 12 hours
A small reverse-mode engine and a matrix-calculus gradient-check harness
Build a scalar reverse-mode engine with forward-mode duals and a harness that checks closed-form matrix gradients by directional finite differences. Derive one branched computation by hand, plant a gradient bug, and show the harness isolating it.
- 1Write a scalar
Valueclass with arithmetic, powers,exp,logandtanh, each with its local derivative, and abackward()over topological order accumulating intograd. Test one graph where a node reaches the output through several paths against a hand derivative and central differences. - 2Add forward mode with dual numbers. For one function from , check Jacobian-vector against vector-Jacobian products through with directions of your choice.
- 3Build a
check(f, grad, X, V, h)harness comparing the directional derivative against central differences. Derive and check the trace, log-determinant, softmax cross-entropy, layer and inverse differential infloat64with directions of your choice, taking symmetric positive definite where needed. - 4Sweep the step over powers of ten for two checks and keep the raw error table. Predict where the curve bends, then measure: explain the falling and rising arms and choose your harness step and tolerance from the curve.
- 5Derive by hand the gradient of a branched computation reusing one matrix, writing each local vector-Jacobian product and its accumulation. Implement it in NumPy and check it with the harness, plus a Hessian-vector product by centered differences against the exact Hessian of a quadratic.
- 6Plant one gradient bug, such as a missing transpose, an overwrite instead of an accumulation, or a wrong derivative sign. Show the directional check failing, localize the fault by checking each local vector-Jacobian product alone, and keep the bug as a test that must fail.
What you hand in
numlab/autodiff/: scalar reverse-mode engine, forward-mode duals and gradient-check harness.- Tests for shared-path accumulation, the JVP-VJP identity, the branched derivation, the Hessian-vector product and the planted bug.
notes/calculus.mdwith the branched derivation and the step-size curve with its chosen step.
How to check it
- Self-grade with the rubric below
How it's graded
Engine correctness
Pass: Every operation matches central differences within a stated tolerance, including a node reached through several paths.
Great: Forward and reverse mode agree through the dot-product identity on directions of your choice.
Matrix gradients
Pass: Each closed form passes the directional check in float64.
Great: Each closed form is derived in the notes from differentials, naming the assumption it needs.
Step size
Pass: The sweep table is kept as raw output and the chosen step sits near its minimum.
Great: The note explains the truncation and cancellation arms and derives the tolerance from them.
Fault isolation
Pass: The planted bug fails the directional check and stays in the suite as a failing case.
Great: Local products are checked one at a time to name the faulty operation.
Resources
- MIT 18.S096 - Matrix Calculus for MLMIT OpenCourseWare
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
Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.
- PyTorch Autograd mechanicsPyTorch documentation
Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.
- CS231n - Linear ClassificationStanford University
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.
- MIT 18.02SC - Multivariable Calculus Unit 2MIT OpenCourseWare
Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.
- Mathematics for Machine Learning - Ch. 5Deisenroth, Faisal & Ong
Use Chapter 5 as the compact derivative, Jacobian, chain-rule and backpropagation reference.