7 routes42 sections324 units

Unit 2.1 · about 3 h

59 / 324

Limits, 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

  1. Univariate derivatives, partials and gradients
    Mathematics 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)

  2. Derivatives as linear operators
    MIT 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

  3. Gradient geometry and directional derivatives
    MIT 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

Closed-book, differentiate \(f(x_1, x_2) = x_1^2 x_2 + x_1 x_2^3\) at (1, 2) to get \(\operatorname{grad} f = (12, 13)\), then the directional derivative along \(u = (1, 1)/\sqrt{2}\), which is \(25/\sqrt{2}\) = 17.67766953. Check with 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

  1. State the difference quotient and the assumption that turns it into the derivative.

  2. 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}\).

  3. Give a function with both partials zero at the origin that is not differentiable there, and the curve that exposes it.

  4. 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?

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.

Mark passed, then open 2.2 Only mark this when you can do it without notes.

Leads to

Resources for this unit

Also in this section

  • Hands-On ML - Autodiff notebook
    Aurelien Geronrequiredimplementation
    Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.
  • PyTorch Autograd mechanics
    PyTorch documentationrequiredreference
    Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.
  • CS231n - Linear Classification
    Stanford Universitysupportreference
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels