Knowledge Sprint SeriesMT24 Learning

Phase 4 · Sprint 9/90 · Mathematical & Logical FoundationsVectors · Matrices · Gradients · Chain Rule

Linear Algebra & Calculus for Machine Learning

Data become vectors, layers become matrices, learning becomes a walk downhill.

In short. Vectors and cosine similarity, matrices as maps, gradients and gradient descent, and the chain rule behind backpropagation.

Sprint 9 of 90 infographic: Linear Algebra & Calculus for Machine Learning. Data become vectors, layers become matrices, learning becomes a walk downhill. The full text of the image is on this page.
Full resolution2160 × 2700 PNGPhase summary

The part of the image you are reading lights up. Tap a part to jump to its text.

Fig. 1 in words

Computed descent on L = ½(w₁² + 10w₂²), η = 0.18: the steps cross the narrow valley.

  1. 1 Level set

    • Same loss all round the ring.
    • rings round the minimum, on the axes w₁ and w₂
  2. 2 Gradient ∇L

    • Steepest way up, ⊥ to the ring.
    • arrow from the starting point
  3. 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. 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

  1. Deisenroth, Faisal & Ong (2020). Mathematics for Machine Learning. Cambridge University Press
  2. Goodfellow, Bengio & Courville (2016). Deep Learning, ch. 2, 4, §6.5. MIT Press
  3. Baydin et al. (2018). Automatic Differentiation in Machine Learning: a Survey. JMLR 18
  4. Rumelhart, Hinton & Williams (1986). Nature 323
  5. Cauchy (1847). Comptes rendus 25

Cite this phase

Tahri, M. (2026). MT24 Learning — Knowledge Sprint Series, Phase 4: Mathematical & Logical Foundations (Version 1.0) [Educational resource]. Zenodo. https://doi.org/10.5281/zenodo.22999436

Version DOI 10.5281/zenodo.22999436 · all versions 10.5281/zenodo.22999435 · first published 25 September 2026