Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 Β· BEGINNERModule 1 Β· Vectors

Norms: how big is a vector?

Straight-line length is one way to measure a vector; adding absolute values and taking the biggest entry are two more. Each draws a different shape, and each shows up in training.

10 min Prerequisites: Lesson 7 (the dot product)

1. The idea

A norm measures the size of a vector with one number, never negative and zero only for the zero vector. The everyday one is straight-line length, the L2 norm: ‖𝐱‖₂ = √(x₁² + xβ‚‚Β² + …). It is Pythagoras, in any number of dimensions, and it equals √(𝐱·𝐱).

It isn't the only sensible choice. The L1 norm adds up absolute values, ‖𝐱‖₁ = |x₁| + |xβ‚‚| + …, like walking a city grid instead of flying. The L∞ norm keeps only the biggest entry. The widget draws each one's unit ball, every point whose norm is exactly 1: L2 gives a circle, L1 a diamond, L∞ a square.

Norms are how machine learning measures β€œtoo big”. Weight decay adds λ‖𝐰‖₂² to the loss to keep weights small. An L1 penalty λ‖𝐰‖₁ pushes many weights to exactly zero, because the diamond's corners sit on the axes, where some coordinates are 0. Gradient clipping shrinks a gradient whose norm is too large, and dividing an embedding by its L2 norm keeps only its direction.

2. Try it: three ways to measure

Pick a norm to draw its unit ball, then drag 𝐱. Put 𝐱 on the L1 diamond and switch to L2: is it inside the circle, on it or outside? Can a point on the diamond ever be outside the circle?

A plane showing the unit ball of the chosen norm: a diamond for L1, a circle for L2 or a square for L-infinity, through the points (1, 0), (0, 1), (βˆ’1, 0) and (0, βˆ’1). A draggable point x shows its L1, L2 and L-infinity norms, and whether it is inside, on or outside the outline.

3. The math

L2: straight-line length
βˆ₯xβˆ₯2=βˆ‘ixi2=xβ‹…x\|\mathbf{x}\|_2 = \sqrt{\textstyle\sum_i x_i^2} = \sqrt{\mathbf{x}\cdot\mathbf{x}}
The default when a paper writes ‖𝐱‖ with no subscript.
L1: add the absolute values
βˆ₯xβˆ₯1=βˆ‘i∣xi∣\|\mathbf{x}\|_1 = \sum_i |x_i|
Distance walked along the grid.
L∞: the biggest entry
βˆ₯xβˆ₯∞=max⁑i∣xi∣\|\mathbf{x}\|_\infty = \max_i |x_i|
Only the largest coordinate counts.
Unit vector: direction only
x^=xβˆ₯xβˆ₯2\hat{\mathbf{x}} = \frac{\mathbf{x}}{\|\mathbf{x}\|_2}
Same direction, length exactly 1. (Here the hat means β€œunit”, not β€œestimate”: context decides.)
Weight decay
Ltotal=L+λ βˆ₯wβˆ₯22L_{\text{total}} = L + \lambda\,\|\mathbf{w}\|_2^2
Ξ» sets how hard large weights are punished.

4. The code (python)

mm1_norms.py
import numpy as np

x = np.array([3.0, -4.0])
print(np.linalg.norm(x))           # 5.0
print(np.linalg.norm(x, 1))        # 7.0
print(np.linalg.norm(x, np.inf))   # 4.0
print(x / np.linalg.norm(x))       # [ 0.6 -0.8]

g = np.array([6.0, 8.0])           # clip a gradient to a norm of at most 5
max_norm = 5.0
n = np.linalg.norm(g)
if n > max_norm:
    g = g * (max_norm / n)
print(g, np.linalg.norm(g))        # [3. 4.] 5.0

6. Check yourself

Answer all 4 questions correctly to complete the chapter Β· 0 / 4 done
Q1/4 What is β€–(3, 4)β€–β‚‚?
Q2/4 What is β€–(1, βˆ’2, 2)‖₁?
Q3/4 Which norm's unit ball, in two dimensions, is a diamond with its corners on the axes?
Q4/4 Dividing a non-zero vector by its L2 norm gives…

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.