Fig. 1 in words
Computed descent on L = ½(w₁² + 10w₂²), η = 0.18: the steps cross the narrow valley.
1 Level set
- Same loss all round the ring.
- rings round the minimum, on the axes w₁ and w₂
2 Gradient ∇L
- Steepest way up, ⊥ to the ring.
- arrow from the starting point
3 Step -η∇L
- Downhill, but not aimed at w*.
- the steps zig-zag across the valley · Δw₁ = 8 from the start to the minimum
4 Minimum w*
- Here ∇L = 0.
01 Vectors
- A data point is a vector: one number per feature
- Dot product: x·y = Σxᵢyᵢ = ‖x‖‖y‖cos θ
Divided by both lengths, it becomes cosine similarity, used to compare embeddings.
02 Matrices as Maps
- A matrix maps one vector to another: y = Ax
- A dense layer: Wx + b, then a nonlinearity
- Shapes chain: (m×n)(n×p) → m×p
Keep: write the shape beside every array.
03 Gradients
- ∂L/∂wᵢ: how fast the loss moves as one weight moves
- ∇L stacks them all and points uphill fastest
- Descend: w ← w - η∇L
Cauchy proposed this step in 1847.
04 The Chain Rule
- (f∘g)′(x) = f′(g(x))·g′(x)
- A network is a chain of functions; backprop runs the rule from the loss backwards
Backprop only computes the gradient; descent does the learning (Goodfellow et al., §6.5).
05 Try It Yourself
sprint09_linear_algebra_calculus_for_ml.py
import numpy as np
rng = np.random.default_rng(9)
X = rng.normal(size=(100, 2))
y = X @ np.array([2.0, -3.0])
L = lambda w: ((X@w - y)**2).mean()
g = lambda w: 2*X.T@(X@w - y)/100
w, e = np.zeros(2), np.eye(2)[0]*1e-6
num = (L(w+e) - L(w-e)) / 2e-6
print(g(w)[0].round(4), num.round(4))
for _ in range(100): w = w - 0.1*g(w)
print(w.round(3), L(w).round(6))
Output, as printed on the image
-5.0586 -5.0586 · [ 2. -3.] 0.0
Run it. sprint09_linear_algebra_calculus_for_ml.py · versions, data and steps
06 General Notes
- Write the shape of every array
- Check each gradient against finite differences
- Loss climbing? Cut the step η
- Scale features: a rounder valley, fewer zig-zags
07 Mini Project
- Derive the least-squares gradient on paper
- Code it in NumPy; check it numerically
- Run descent and plot the loss per step
- Compare with np.linalg.lstsq
R Trusted Sources
- Deisenroth, Faisal & Ong (2020). Mathematics for Machine Learning. Cambridge University Press
- Goodfellow, Bengio & Courville (2016). Deep Learning, ch. 2, 4, §6.5. MIT Press
- Baydin et al. (2018). Automatic Differentiation in Machine Learning: a Survey. JMLR 18
- Rumelhart, Hinton & Williams (1986). Nature 323
- Cauchy (1847). Comptes rendus 25
