Skip to gallery
AK Books / Computational Optimization for AI
Syllabus Gallery Start reading Report an error

Gallery

Every interactive figure and every code-session chart in the book, each with a link to its place in the text.

Chapter 1 · Vectors, Norms, and the Cost of Doing AI

Code session · Section 1.2.3 Code session in Chapter 1

Small vectors: constants beat Big-O

100 ns 200 ns 400 ns 1,000 ns 2,000 ns 4,000 ns 1 2 4 8 16 32 64 128 vector length p (log scale) crossover Python loop NumPy

Time for one inner product against p. NumPy stays near 400 ns at every size: almost all of it is start-up cost. The loop grows by about 26 ns per entry, so the loop wins up to p = 8 and loses from p = 16 on. Big-O hides exactly these constants.

Example 1.5 Example 1.5 in Chapter 1

Scale and add vectors head to tail

Workbench Example 1.5
Section 1.3.8 Section 1.3.8 in Chapter 1

Python · the loop behind 2p - 1 flops

Workbench Section 1.3.8
Python · the loop behind 2p - 1 flops
show setup
# Example 1.7
x = [1, -2, 5]
y = [3, 1, -3]
1def inner_product_loop(x, y):
2    result = x[0] * y[0]
3    for i in range(1, len(x)):
4        result += x[i] * y[i]
5    return result
6
7print(inner_product_loop(x, y))
line: - i: - result: - flops: 0
Output -14
Example 1.7 Example 1.7 in Chapter 1

Step through the sum in ℝᵖ

Workbench Example 1.7
Code session · Section 1.3.8 Code session in Chapter 1

Loop versus NumPy: same O(p), very different speed

10⁻⁷ s 10⁻⁶ s 10⁻⁵ s 10⁻⁴ s 10⁻³ s 10⁻² s 10⁻¹ s 1 s 10³ 10⁴ 10⁵ 10⁶ 10⁷ vector length p (log scale) 132x Python loop NumPy

Time for one inner product, on log scales. The loop's time grows tenfold with each tenfold step in p, as O(p) predicts. NumPy grows about as fast once p passes 10,000; below that, a fixed start-up cost dominates. At ten million entries, NumPy is 132 times faster: the same O(p), with a much smaller constant.

Example 1.11 Example 1.11 in Chapter 1

Inner product, angle, and projection

Workbench Example 1.11
Example 1.15 Example 1.15 in Chapter 1

Contours and gradient of a 2D quadratic

Workbench Example 1.15
Section 1.5.2 Section 1.5.2 in Chapter 1

Unit balls of the three norms

Workbench Section 1.5.2
Example 1.26 Example 1.26 in Chapter 1

Inner product, angle, and projection

Workbench Example 1.26
Code session · Section 1.5.17 Code session in Chapter 1

Similarity search: inner product or distance, same answer

1.0 1.2 1.4 1.6 1.8 -0.4 -0.2 0 0.2 0.4 distance ‖d − q‖₂ inner product ⟨d, q⟩ dist = √(2(1 − score)) top 3 (#51, #960, #378)

Each dot is one document: its inner product with the query (across) and its distance from the query (up). All 1000 dots lie on one falling curve, \(\text{dist} = \sqrt{2(1 - \text{score})}\): the document with the highest inner product is always the closest.

Chapter 2 · Optimization: from problems to gradient descent

Example 2.1 Example 2.1 in Chapter 2

Gradient descent on a 1D quadratic

Workbench Example 2.1
Code session · Section 2.2.4 Code session in Chapter 2

Three norms, three best constants

0 25k 50k best w = 22 ℓ₂² loss 0 240 480 best w = 3 ℓ₁ loss 0 60 120 best w = 50.5 ℓ∞ loss 0 20 40 60 80 100 candidate constant w

Each panel is one loss against w, with its best w marked. Squared residuals pick the mean, 22: the outlier pulls the answer far from the other four numbers. Absolute residuals pick the median, 3. The largest residual picks the midrange, 50.5, halfway between 1 and 100.

Example 2.7 Example 2.7 in Chapter 2

Taylor polynomials of ln(1 + x)

Workbench Example 2.7
Section 2.3.14 Section 2.3.14 in Chapter 2

Gradient descent on 2D contours

Workbench Section 2.3.14
Code session · Section 2.4.1 Code session in Chapter 2

Gradient descent on least squares: three step sizes

10⁻² 1 10² 10⁴ 10⁶ 0 10 20 30 40 loss (log scale) step t η = 0.02 η = 0.3 η = 1.2

Loss against step, on a log scale. With a step of 0.02 the loss falls slowly, from 3.9 at the start to 0.39 after 40 steps. With 0.3 it reaches the best possible loss for this data, about 0.009, within 40 steps. With 1.2 each step overshoots by more than the last, and the loss explodes to about 300,000. Steps above about 1.1 diverge on this problem; Chapter 6 shows where that limit comes from.

Example 2.19 Example 2.19 in Chapter 2

Sending messages back along the arrows

Workbench Example 2.19

Chapter 3 · Matrices: operations, norms, and geometry

Section 3.2.1 Section 3.2.1 in Chapter 3

Column and row views of Ax

Workbench Section 3.2.1
Code session · Example 3.10 Code session in Chapter 3

One step size or one per coordinate

10⁻¹⁸ 10⁻¹⁵ 10⁻¹² 10⁻⁹ 10⁻⁶ 10⁻³ 1 10³ 0 5 10 15 20 25 30 f(x) (log scale) step k one step size (η = 0.015) per-coordinate (0.015, 0.5)

f against step, on a log scale. The first three values of each run are Table 3.1. Both runs take the same step along the steep coordinate, which halves and flips sign at every step. With one shared step size the flat coordinate shrinks by only 1.5% per step, so f is still 20.2 after 30 steps. With its own step of 0.5 the flat coordinate halves too, and f is divided by 4 at every step.

Code session · Example 3.18 Code session in Chapter 3

Same product, two orders

Multiplications 0 300k 600k 900k 52,500 (AB)C 750,000 A(BC) 14× multiplications Time (microseconds) 0 15 30 45 5.45 µs (AB)C 34.95 µs A(BC) 6.4× runtime

Both orders give the same 10 x 50 matrix. A(BC) does 14 times as many multiplications as (AB)C and takes about 6 times as long on this laptop. The big products run faster per multiplication than the small ones, so time grows more slowly than the count; the order still decides the cost.

Chapter 4 · Matrix inverse and eigendecomposition

Code session · Example 4.7 Code session in Chapter 4

One entry changes, the answer jumps

0 0.5 1 1.5 2 1.00005 1.0001 1.00015 1.0002 1.00025 1.0003 second coordinate x₂ bottom-right entry of A 1.0001: x₂ = 1 1.0002: x₂ = 0.5

The second coordinate of the solution as the bottom-right entry of A moves from 1.00005 to 1.0003. Moving that entry by 0.0001, from 1.0001 to 1.0002, moves the solution from (1, 1) to (1.5, 0.5) and changes the entries of the inverse by about 5000, half their size. NumPy puts the condition number of A at about 40,000, the value Section 4.3.2 computes from the eigenvalues; a large condition number is the warning sign for this behavior.

Section 4.3 Section 4.3 in Chapter 4

Vectors scaled without changing direction

Workbench Section 4.3
Example 4.11 Example 4.11 in Chapter 4

Energy under orthogonal transformations

Workbench Example 4.11
Code session · Example 4.16 Code session in Chapter 4

Principal components: Example 4.16, then a cloud

Example 4.16 (3 points) 0 1 2 3 4 0 2 4 6 x₂ x₁ q₁ (1, 1) (3, 2) (5, 3) Cloud (200 points) -8 -4 0 4 8 -10 -5 0 5 10 x₂ x₁ q₁ q₂

In the first panel, Example 4.16's three points lie on one line, so the second eigenvalue is 0 and the first component, (2, 1)/√5 ≈ (0.894, 0.447), describes them exactly. The second panel shows the cloud with its two principal axes. The first eigenvalue is about 43 times the second, so the first component carries 98% of the variance.

Chapter 5 · The power method

Section 5.1.4 Section 5.1.4 in Chapter 5

Rank-k truncation of an image

Workbench Section 5.1.4
Code session · Section 5.1.4 Code session in Chapter 5

Low-rank approximations of an image

10⁻¹² 10⁻¹⁰ 10⁻⁸ 10⁻⁶ 10⁻⁴ 10⁻² 1 0 16 32 48 64 relative error (log scale) rank k k = 1 k = 5 k = 10 k = 20

The relative error of the best rank-k approximation against k, on a log scale. Rank 10 keeps 1,290 numbers instead of 4,096 and misses 1.3% of the image (measured by the sum of squared entries). The error is exactly the share of the squared singular values left out, as the Eckart–Young theorem says (Example 5.2).

Example 5.13 Example 5.13 in Chapter 5

Power method iterates on the unit circle

Workbench Example 5.13
Code session · Example 5.15 Code session in Chapter 5

The spectral gap sets the speed

10⁻⁸ 10⁻⁶ 10⁻⁴ 10⁻² 1 10² 0 5 10 15 20 25 30 angle in degrees (log scale) step t B (λ₂/λ₁ = 0.95) A (λ₂/λ₁ = 0.5)

The angle against step, on a log scale. For A, with λ2/λ1 = 0.5, the tangent of the angle halves at every step: 14 degrees after 2 steps, as in the example, and under a ten-millionth of a degree after 30. For B, with λ2/λ1 = 0.95, it shrinks by only 5% per step: 42 degrees after 2 steps and still 12 after 30.

Chapter 6 · Curvature, Newton's method, and convexity

Example 6.12 Example 6.12 in Chapter 6

Contours and eigenvectors of ½ xᵀ H x

Workbench Example 6.12
Code session · Example 6.26 Code session in Chapter 6

Newton against gradient descent

Least squares 10⁻¹⁶ 10⁻¹² 10⁻⁸ 10⁻⁴ 1 10⁴ 0 10 20 30 gradient size (log scale) step t gradient descent Newton Logistic regression 10⁻¹⁶ 10⁻¹² 10⁻⁸ 10⁻⁴ 1 10⁴ 0 250 500 750 1000 gradient size (log scale) step t gradient descent Newton

The size of the gradient against step, on a log scale. On least squares, Newton's method lands on the answer in one step, as Example 6.26 shows, and gradient descent needs 21 steps. On logistic regression, Newton needs 9 steps, and in its last steps the number of correct digits roughly doubles each time. Gradient descent is still at 0.019 after 1000 steps: at the answer, the flattest curvature is about 380 times smaller than L, so the step 1/L is tiny there.

Example 6.28 Example 6.28 in Chapter 6

Step sizes on an elliptical bowl

Workbench Example 6.28
Code session · Example 6.28 Code session in Chapter 6

Steps against the condition number

10 10² 10³ 10⁴ 10⁵ 1 10 10² 10³ steps (log scale) condition number κ (log scale) Example 6.28: 132 steps

Steps against the condition number, on log-log axes. κ = 10 is Example 6.28's bowl: 132 steps. The count grows in proportion to κ, about 13.8 steps per unit of κ, because each step removes only the fraction 1/κ of the flat coordinate and ln(10⁶) ≈ 13.8.