Unit 2.1 · about 3 h
59 / 324Limits, partial and directional derivatives, and gradients
Every later gradient is a stack of directional derivatives. If you cannot compute one by hand and check it numerically, no backward pass output will mean anything to you.
- derive
You can now
- Compute partial and directional derivatives from the limit definition and evaluate them at a point by hand.
- Explain the gradient as the steepest-ascent direction and relate every directional derivative to it through \(D_u f = \operatorname{grad} f \cdot u\).
- Convert between row-vector (MML) and column-vector (18.S096) gradient conventions without dropping a transpose.
Covers
- Limits, local approximation and differentiability
- Partial and directional derivatives
- Gradients as local linear models and steepest directions
Study, in this order
3 steps
- Univariate derivatives, partials and gradientsMathematics for Machine Learning - Ch. 5
Section 5.1, Differentiation of Univariate Functions (pp. 141-145): difference quotient (5.3), derivative as a limit (5.4), polynomial derivation (5.5)-(5.6), product, quotient, sum and chain rules (5.29)-(5.32). Section 5.2, Partial Differentiation and Gradients (pp. 146-149): partial derivatives, gradient as a row vector, the chain rule as matrix multiplication (5.48)-(5.53)
- Derivatives as linear operatorsMIT 18.S096 - Matrix Calculus for ML
Lecture 1, Part 2 notes (course notes Chapter 2, Sections 2.1-2.3): \(df = f'(x)[dx]\) as a linear operator, scalar-valued case \(df = (\operatorname{grad} f) \cdot dx\) with the gradient as the transpose of the derivative
- Gradient geometry and directional derivativesMIT 18.02SC - Multivariable Calculus Unit 2
Sessions 35 to 38 (Part B): gradient definition and perpendicularity to level curves, the proof, a worked example, directional derivatives
Depth · Refresher only. Open only if partial derivatives or gradients are weak; it does not replace the MML derivations.
Focus on
- The derivative is the limit of the difference quotient; each partial derivative holds all other variables constant.
- For unit \(u\), \(D_u f = \operatorname{grad} f \cdot u\), and the maximum over \(u\) is \(\|\operatorname{grad} f\|\) at \(u = \operatorname{grad} f / \|\operatorname{grad} f\|\) by Cauchy-Schwarz.
- MML writes gradients as row vectors and 18.S096 as column vectors; the two differ by a transpose and the chain rule changes shape with the choice.
- Existing partials do not imply differentiability; continuous partials do.
Reproduce closed-book
float64 NumPy centered differences at \(h = 10^{-5}\): both components agree within 3e-10 absolute (measured 1.0e-11 and 2.2e-10).Traps
- Reading \(D_u f\) as a partial derivative. A partial fixes an axis; a directional derivative moves along \(u\) and needs the full gradient dot product.
- Assuming equal partials imply a smooth function. With \(f(x, y) = x^2 y/(x^4 + y^2)\) off the origin and \(f(0, 0) = 0\), both partials at the origin are 0, yet \(f\) equals 1/2 along \(y = x^2\), so \(f\) is discontinuous and not differentiable there.
- Mixing gradient conventions across sources and silently dropping a transpose, which flips every later matrix shape.
Practice
MML Chapter 5, Exercise 5.5 (partial derivatives and Jacobians)
Exercises (pp. 170-171), Exercise 5.5: dimensions of each partial derivative in part (a), then the Jacobians in part (b)
Do part (a) before part (b); the predicted dimensions fix the Jacobian shapes.
TrenTorch: Directional derivatives
Implement directional_derivative(f, x, direction) as a centered difference along the normalized direction, and steepest_ascent_direction(f, x) as the unit gradient
Then run both on the Reproduce cubic at (1, 2): the derivative along (1, 1) must land within 1e-9 of 17.67766953, and the steepest-ascent value must equal \(\|\operatorname{grad} f\| = \sqrt{313}\) = 17.69180601.
Self-test
-
State the difference quotient and the assumption that turns it into the derivative.
\((f(x + h) - f(x))/h\). The two-sided limit as \(h \to 0\) must exist. Uses: \(f\) is defined near \(x\) for the quotient, and differentiability at \(x\) for the limit. -
For \(f(x_1, x_2) = x_1^2 x_2 + x_1 x_2^3\), give \(\operatorname{grad} f\) at (1, 2) and \(D_u f\) for \(u = (1, 1)/\sqrt{2}\).
Partial derivatives are \(2 x_1 x_2 + x_2^3\) and \(x_1^2 + 3 x_1 x_2^2\), so \(\operatorname{grad} f = (12, 13)\) and \(D_u f = (12 + 13)/\sqrt{2} = 25/\sqrt{2}\) = 17.67766953. -
Give a function with both partials zero at the origin that is not differentiable there, and the curve that exposes it.
\(f(x, y) = x^2 y/(x^4 + y^2)\) with \(f(0, 0) = 0\). Both partials at the origin are 0, but along \(y = x^2\) the value is constantly 1/2, so \(f\) is discontinuous and hence not differentiable at the origin. -
MML gives a \(1 \times 2\) gradient where 18.S096 gives \(2 \times 1\) for the same \(f: \mathbb{R}^2 \to \mathbb{R}\). Which is right?
Both, by stated convention. MML uses numerator layout (row); 18.S096 uses column gradients with \(df = (\operatorname{grad} f)^\top dx\). Convert with a transpose and never mix the two inside one derivation.
Completion criterion
Stop when
You compute \(\operatorname{grad} f\) and any \(D_u f\) for a new quadratic or cubic by hand, and a float64 centered-difference check at \(h = 10^{-5}\) agrees to 1e-9 absolute.
Leads to
- 2.2 Jacobians, Hessians and Taylor approximations Gradients stack into Jacobian rows and Hessian entries.
- 1.3 Computation graphs and reverse accumulation Reverse accumulation computes these partials on a graph; check it against centered differences.
Resources for this unit
- MIT 18.S096 - Matrix Calculus for ML Use lectures 1-5, the Hessian portion of lecture 7 and computational-graph portion of lecture 8; do the supplied problems and notebooks.
- Mathematics for Machine Learning - Ch. 5 Use Chapter 5 as the compact derivative, Jacobian, chain-rule and backpropagation reference.
- MIT 18.02SC - Multivariable Calculus Unit 2 Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.
Also in this section
- Hands-On ML - Autodiff notebook Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.
- PyTorch Autograd mechanics Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.
- CS231n - Linear Classification 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.