1.3 Gradients and matrix calculus
1. Learn
Section titled “1. Learn”- 3Blue1Brown, Essence of Calculuschapters 1–4: The essence of calculus, The paradox of the derivative, Derivative formulas through geometry, Visualizing the chain rule and product rule
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong)§5.1 Differentiation of Univariate Functions (pp. 141–146), §5.2 Partial Differentiation and Gradients (pp. 146–149), §5.3 Gradients of Vector-Valued Functions (pp. 149–155), §5.5 Useful Identities (pp. 158–159)
- CS231n, Gradient checkssection "Gradient Checks": centered formula and relative error
2. Bridge
Section titled “2. Bridge”Training a network means following derivatives downhill, so this is the most-used module in the course.
A partial derivative partial derivativeRate of change of f as x_i alone changes, all other inputs held fixed.Introduced in 1.3 Gradients and matrix calculus. changes one input and holds the rest fixed. Stack all of them and you get the gradient gradientVector of partial derivatives ∂f/∂x_i. Same shape as x. Points in the direction of steepest increase.Introduced in 1.3 Gradients and matrix calculus. MML treats gradients as row vectors (§5.2); we follow UDL and CS229 and use the same shape as x., which has the same shape as (MML §5.2). Two facts make it useful. The slope in a unit direction is , which is largest when points along (Cauchy–Schwarz), so the gradient is the direction of steepest ascent (MML §5.1, p. 142). And it is perpendicular to the level sets, because along a contour does not change (MML §7.1). The explorer below lets you see both.
For a vector-valued , the partials form the Jacobian JacobianFor f: ℝⁿ → ℝᵐ, the m×n matrix of partials J_{ij} = ∂f_i/∂x_j. The best linear approximation of f near a point.Introduced in 1.3 Gradients and matrix calculus.: the best linear approximation of near a point. Its determinant is the local area factor (MML §5.3). The chain rule says Jacobians multiply. Backprop (3.2) is just that rule applied efficiently.
Notation trap: MML writes gradients as row vectors (MML §5.2, Remark “Gradient as a Row Vector”). We follow UDL and CS229 and use the shape of (see Notation). The math is the same up to a transpose.
Checking gradients numerically. The central difference has error . The one-sided has error , so prefer central. Compare the two with a relative error, , so the threshold does not depend on the gradient’s scale (CS231n, gradient checks). Values around mean correct. Values above mean a bug. The checker you build here verifies every gradient you derive for the rest of the course.
3. Explore
Section titled “3. Explore”f = lambda x: np.sin(x)
x, true = 0.5, np.cos(0.5)
for h in [1e-1, 1e-2, 1e-3, 1e-4]:
fwd = (f(x + h) - f(x)) / h
ctr = (f(x + h) - f(x - h)) / (2 * h)
print(f'h={h:.0e} forward err={abs(fwd-true):.1e} central err={abs(ctr-true):.1e}')Tab indents · Esc then Tab leaves the editor · Shift+Enter runs
Watch the forward error fall 10× per row and the central error fall 100×. (Push down to and rounding error takes over. That is why is the default.)
Build numerical_grad for arrays of any shape, rel_error and grad_check, then derive four gradients by hand and let your checker confirm them. One test feeds logsumexp_grad inputs of 1000: make it stable.
stubs/m1_3_gradients.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m1_3_gradients.py and run mlc check 1.3. The tests are identical; import the results on the progress page.
Locally: mlc check 1.3.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.