Quick recap
The key takeaways of every lesson on one page. Read it in ten minutes to refresh the whole track; open a lesson when a point feels unclear.
01 Derivatives in one variable
- The derivative is the limit of difference quotients: , the slope of the tangent line.
- Equivalently, with : the tangent line is the best linear approximation.
- and ReLU have no derivative at 0 (the one-sided slopes differ); frameworks use the subgradient value there.
- Rules: linearity, , and the chain rule : rates multiply.
- , , .
- on an interval means convex. At a critical point, gives a strict local minimum, a maximum, and decides nothing.
- Taylor: ; the error has order .
- Gradient descent on multiplies by per step and converges exactly when .
02 Partial derivatives and the gradient
- A function of two variables can be drawn as a surface or as a contour plot of level sets ; close contour lines mean a steep function.
- The partial derivative is the ordinary derivative in with all other variables fixed: the slope of a slice.
- The gradient is a column vector in the input space .
- is differentiable at if ; then .
- Among unit directions, gives the largest slope and the smallest (Cauchy–Schwarz).
- The gradient is perpendicular to the level set through its base point.
- In a stretched bowl , does not point at the minimum, and gradient descent zig-zags; the best rate per step is .
- Existing partial derivatives do not imply differentiability ( at 0); continuous partial derivatives () do.
03 Jacobians, Hessians and Taylor
- The derivative of is the Jacobian with ; its entries are .
- Shapes: Jacobian outputs × inputs; gradient (a column here, a row in MML); Hessian .
- ; an elementwise map has ; polar coordinates have .
- If , the Hessian is symmetric (Schwarz), and equals up to an error (Taylor).
- Minimizing the quadratic model gives the Newton step: solve . It is a descent direction when ; with an indefinite it goes to the model's saddle.
- At a critical point: all Hessian eigenvalues gives a strict local minimum, mixed signs a saddle; a zero eigenvalue with no negative one leaves the test undecided.
- Taylor test: has order 2 if is the gradient and order 1 if not; use float64, a random and the middle range of .
04 Chain rule and computational graphs
- Chain rule: , shapes . The map applied first stands on the right; the outer Jacobian is evaluated at .
- Entry form: , a sum over the intermediate variables.
- For a scalar loss, the gradient through a map is a vector-Jacobian product: , where is the adjoint of .
- The three basic VJPs: for ; for an elementwise ; (rank 1 for one sample).
- Fan-out: when a value is used in several places, the contributions of all paths add, . Residual connections and shared weights are fan-out.
- A computational graph has one node per primitive operation. The backward sweep (seed ) gives all partial derivatives of one output; the forward sweep gives one directional derivative.
- For at : , , .
05 Forward and reverse mode autodiff
- Automatic differentiation applies the chain rule to the primitive operations of a program at the current numbers. It is exact up to rounding, has no step size, and handles loops and branches (it differentiates the branch that ran).
- Forward mode carries (value, tangent) pairs; one pass gives a JVP , one column of for . It needs no tape.
- Dual numbers with implement forward mode: .
- Reverse mode records a tape in the forward run, then accumulates in reverse order; one pass gives a VJP , one row of .
- One pass of either mode costs at most a small constant times one evaluation (, typically 2–3). The full Jacobian of needs JVPs or VJPs.
- Cheap gradient principle: the gradient of a scalar function costs a constant times the function, for any number of inputs. Forward mode wins when inputs are few and outputs are many.
- Reverse mode stores the values of the forward run, so memory grows with depth and batch. Checkpointing every layers lowers the peak to about activations for at most one extra forward pass.
06 Backprop: MLPs, losses, layers
- Backprop for an MLP with softmax cross-entropy: , , , .
- For one sample, the gradient of a weight matrix is an outer product (rank at most 1); for a batch, has rank at most .
- With a mean loss over a batch, the factor enters once, in . The backward pass costs about two forward passes and needs the stored activations.
- Least squares : gradient , Hessian ; one Newton step reaches the minimizer.
- Logistic regression: gradient , Hessian with ; convex, but on separable data no minimizer exists.
- Softmax cross-entropy has gradient and Hessian . Compute with .
- Layer norm backward: ; its entries sum to 0, and it is orthogonal to when .
- The input-side error is a product of transposed layer Jacobians, so it can vanish or explode exponentially with depth.
07 Finite differences and gradient checks
- Forward difference has truncation error : first order. Central difference has : second order.
- Rounding adds an error of about , so the total error against is a V on a log–log plot; the best step is not the smallest.
- Optimal steps in float64: about for the forward difference (about 8 correct digits) and for the central one (about 10–11 digits); scale them by .
- The complex step has error and no subtraction, so gives machine precision; the code must be analytic and complex-safe.
- A finite-difference gradient costs or evaluations of ; a directional check costs 2. Use them to check gradients, never to train.
- A reliable gradient check uses float64, central differences, the relative error , a deterministic , a generic point and no kinks within .
- For a smooth function in float64, a relative error below is fine and above is almost surely a bug.
08 Hessian-vector products
- A Hessian-vector product is the derivative of the gradient along : .
- Forward-over-reverse computes as a JVP of the gradient; reverse-over-reverse as . Both are exact because is symmetric.
- One exact HVP costs a small constant times one gradient (about 2–4), for any . The full Hessian needs HVPs.
- A central difference of two gradients approximates with best relative error about (about in float64).
- . The Gauss-Newton matrix keeps the first term.
- when is convex, even where the Hessian is indefinite; at a perfect fit or for a linear model. needs one JVP and one VJP.
- Hutchinson: when . With Rademacher probes and symmetric , the variance is ; Gaussian probes give .
09 Implicit differentiation
- Implicit function theorem: if , is and is invertible, then near the solution is a unique map with .
- The derivative comes from differentiating with the chain rule; it does not depend on how was computed.
- Fixed point : , the sum of the Neumann series . Argmin of : .
- The gradient of a scalar loss needs one adjoint solve and one VJP: . CG (symmetric positive definite ) or GMRES do it matrix-free.
- Unrolled backprop stores all iterates and has truncation error about ; the implicit gradient stores only and its error is proportional to the forward error .
- Ridge: , so decreases in , though single weights can grow.
- The theorem fails at folds ( singular), for non-unique or non-smooth solutions, and the gradient is poor when is ill-conditioned or the solver has not converged.
10 Integrals and change of variables
- The integral is the limit of Riemann sums . The fundamental theorem gives for any antiderivative . The midpoint and trapezoid rules have error , the left sum .
- An expectation is an integral: . The Monte Carlo average of samples is unbiased with standard error , in any dimension.
- Tonelli () and Fubini () allow you to integrate one variable at a time, in any order.
- In polar coordinates , because .
- Change of variables: for a diffeomorphism . A smooth map scales small volumes by .
- Density of : with , so . If is not one-to-one, add the terms of all branches.
- For a composition of layers, the log-determinants add. A coupling layer has a triangular Jacobian with , an cost, and an exact inverse.
- A nonlinear map moves the mode and the mean in a nonobvious way: for , the mode of is and its mean is .
11 Gaussian integrals and Laplace
- : square the integral, use polar coordinates, and the factor in gives an elementary antiderivative. More generally .
- For symmetric : . Proof: complete the square, shift, rotate to the eigenvectors, factor.
- The normalized density is : is the precision matrix. The gradient of is the mean and its Hessian is the covariance.
- The marginal likelihood of Bayesian linear regression is a Gaussian integral over the weights; its term penalizes flexible models.
- Gauss–Hermite: for ; nodes are exact for polynomials of degree .
- Laplace's method: for a unique, nondegenerate minimum. It gives Stirling's formula , with relative error .
- Laplace approximation: and , with the negative log joint and its Hessian. Keeping only the terms that grow with gives the BIC.
- The Laplace approximation is exact for linear-Gaussian models. It fails for multimodal, skewed or bounded posteriors, modes on a boundary or at a kink, and singular or indefinite Hessians, and it depends on the parameterization.
12 Gradients of expectations
- When is in the distribution, changes because the density moves; autodiff through a plain sample returns or an error.
- Score-function identity (REINFORCE): . It needs only values of and works for discrete , but the support of must not depend on .
- The score has mean zero, , so a baseline that does not depend on keeps the estimator unbiased; the best constant is .
- Pathwise gradient (reparameterization): with , ; for a Gaussian, , , .
- The pathwise estimator needs a differentiable path and an without jumps; through a jump it is biased.
- For , : per-sample variances (pathwise), (score), (score with ); all means are .
- Score-function variance grows with and with the dimension; pathwise variance follows the slope of and can be larger for fast-oscillating .
- VAE encoder: pathwise. Policy gradients, discrete choices, black-box simulators: score function with a baseline.
13 Calculus of variations
- A functional maps a function to a number; on a grid it becomes an ordinary function of the values , and .
- The first variation is , the directional derivative in the direction of a function .
- Integration by parts and the fundamental lemma give the Euler–Lagrange equation , where is a total derivative; so .
- At a free end the minimizer satisfies the natural boundary condition (for : ); prescribed values are essential conditions.
- Euler–Lagrange is necessary only; if is convex in , every solution is a global minimizer.
- The shortest path is a straight line; the Dirichlet energy gives the Poisson equation .
- Energy (Ritz) losses need half the derivatives and get natural conditions for free; residual (PINN) losses work for any PDE but have Hessian like , with condition number .
- Gradient descent on a discretized functional is an explicit Euler step of the gradient flow ; for the Dirichlet energy this is the heat equation, stable only for .