Norms
A norm measures the size of a vector. ML uses norms everywhere: the length of an error vector is a loss, the size of a weight vector is a regularization penalty, and the distance between two embeddings is a norm of their difference. Different norms give different notions of "small", and that choice changes what a model learns.
The definition
A norm on is a function with, for all and scalars :
The three norms ML uses most
All three are special cases of the -norm for . The unit ball is a diamond for , a disc for and a square for .
import numpy as np
x = np.array([3.0, -4.0, 0.0, 1.0])
for p in (1, 2, np.inf):
print(f"p = {p}: numpy {np.linalg.norm(x, ord=p):.4f}")
print("by hand:", np.abs(x).sum(), np.sqrt((x**2).sum()).round(4), np.abs(x).max())
# the triangle inequality on random vectors
rng = np.random.default_rng(0)
ok = all(np.linalg.norm(a + b, 1) <= np.linalg.norm(a, 1) + np.linalg.norm(b, 1) + 1e-12
for a, b in rng.normal(size=(1000, 2, 5)))
print("triangle inequality held 1000 times:", ok)p = 1: numpy 8.0000
p = 2: numpy 5.0990
p = inf: numpy 4.0000
by hand: 8.0 5.099 4.0
triangle inequality held 1000 times: True
Why the choice matters: sparsity
Penalizing (the lasso) tends to drive many weights to exactly zero, while penalizing (ridge) shrinks all weights a little. A geometric reason: when an expanding level set of the loss first touches a diamond, it usually touches a corner, and corners lie on the axes. A one-dimensional calculation shows the same effect: minimizing gives
The answer, called soft thresholding, is exactly 0 whenever .
import numpy as np
a = np.array([3.0, 0.4, -0.2, -2.5, 0.8, 0.05])
lam = 0.5
ridge = a / (1 + 2 * lam)
lasso = np.sign(a) * np.maximum(np.abs(a) - lam, 0)
print("unpenalized:", a)
print("ridge: ", np.round(ridge, 3))
print("lasso: ", np.round(lasso, 3) + 0)
print("zeros: ridge", int(np.sum(ridge == 0)), " lasso", int(np.sum(lasso == 0)))unpenalized: [ 3. 0.4 -0.2 -2.5 0.8 0.05]
ridge: [ 1.5 0.2 -0.1 -1.25 0.4 0.025]
lasso: [ 2.5 0. 0. -2. 0.3 0. ]
zeros: ridge 0 lasso 3
Matrix norms
Matrices have norms too. The Frobenius norm treats a matrix as a long vector; the spectral norm is the largest stretch factor, equal to the largest singular value (SVD).
In software
Lasso,RidgeandElasticNetin scikit-learn are least squares plus , or both (Regularization).- Gradient clipping rescales a gradient whose norm exceeds a threshold.
- Adversarial robustness is measured in balls (every pixel may change by at most ).
- Spectral normalization divides a layer's weights by to keep GANs stable.
Exercises
Exercise 1: Compute three norms
Find , and for .
Answer
5, 3 and 2.
Solution
; ; . In general .
Exercise 2: Not a norm
Show that is not a norm.
Answer
It fails absolute homogeneity.
Solution
, but a norm needs . (Its square root, the norm, is fine.)
Exercise 3: Soft thresholding (code)
Apply soft thresholding with to .
Answer
.
Solution
Shrink each magnitude by 1 and clip at 0: , , , , keeping the signs.
import numpy as np
a = np.array([2.5, -0.7, 1.0, -3.0])
print(np.sign(a) * np.maximum(np.abs(a) - 1, 0) + 0)[ 1.5 0. 0. -2. ]
Test yourself
Which penalty tends to produce sparse weight vectors (many exact zeros)?
- none of them
is…
- 7
- 5
- 4
- 1
The spectral norm of a matrix equals…
- the sum of its entries
- its largest singular value
- its determinant
- its trace