TExcelTAD

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 VV is a function ∥⋅∥:V→R\lVert \cdot \rVert: V \to \mathbb{R} with, for all x,y\mathbf{x}, \mathbf{y} and scalars λ\lambda:

∥λx∥=∣λ∣ ∥x∥absolutely homogeneous∥x+y∥≤∥x∥+∥y∥triangle inequality∥x∥≥0, and ∥x∥=0  ⟺  x=0positive definite\begin{aligned} &\lVert \lambda\mathbf{x} \rVert = \lvert \lambda \rvert\,\lVert \mathbf{x} \rVert && \text{absolutely homogeneous} \\ &\lVert \mathbf{x} + \mathbf{y} \rVert \le \lVert \mathbf{x} \rVert + \lVert \mathbf{y} \rVert && \text{triangle inequality} \\ &\lVert \mathbf{x} \rVert \ge 0, \text{ and } \lVert \mathbf{x} \rVert = 0 \iff \mathbf{x} = \mathbf{0} && \text{positive definite} \end{aligned}

The three norms ML uses most

∥x∥1=∑i∣xi∣,∥x∥2=∑ixi2,∥x∥∞=max⁡i∣xi∣\lVert \mathbf{x} \rVert_1 = \sum_{i} \lvert x_i \rvert, \qquad \lVert \mathbf{x} \rVert_2 = \sqrt{\sum_i x_i^2}, \qquad \lVert \mathbf{x} \rVert_\infty = \max_i \lvert x_i \rvert

All three are special cases of the pp-norm ∥x∥p=(∑i∣xi∣p)1/p\lVert \mathbf{x} \rVert_p = (\sum_i \lvert x_i \rvert^p)^{1/p} for p=1,2,∞p = 1, 2, \infty. The unit ball {x:∥x∥≤1}\{\mathbf{x} : \lVert \mathbf{x} \rVert \le 1\} is a diamond for ℓ1\ell_1, a disc for ℓ2\ell_2 and a square for ℓ∞\ell_\infty.

python · try it yourself
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 ∥w∥1\lVert \mathbf{w} \rVert_1 (the lasso) tends to drive many weights to exactly zero, while penalizing ∥w∥22\lVert \mathbf{w} \rVert_2^2 (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 12(w−a)2+λ pen(w)\frac{1}{2}(w - a)^2 + \lambda\,\mathrm{pen}(w) gives

wℓ2=a1+2λ(for pen=w2),wℓ1=sign⁡(a)max⁡(∣a∣−λ,0)(for pen=∣w∣)w_{\ell_2} = \frac{a}{1 + 2\lambda} \quad (\text{for pen} = w^2), \qquad w_{\ell_1} = \operatorname{sign}(a)\max(\lvert a \rvert - \lambda, 0) \quad (\text{for pen} = \lvert w \rvert)

The ℓ1\ell_1 answer, called soft thresholding, is exactly 0 whenever ∣a∣≤λ\lvert a \rvert \le \lambda.

python · try it yourself
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 ∥A∥F=∑ijaij2\lVert A \rVert_F = \sqrt{\sum_{ij} a_{ij}^2} treats a matrix as a long vector; the spectral norm ∥A∥2=max⁡∥x∥2=1∥Ax∥2\lVert A \rVert_2 = \max_{\lVert \mathbf{x} \rVert_2 = 1}\lVert A\mathbf{x} \rVert_2 is the largest stretch factor, equal to the largest singular value (SVD).

In software

  • Lasso, Ridge and ElasticNet in scikit-learn are least squares plus ℓ1\ell_1, ℓ22\ell_2^2 or both (Regularization).
  • Gradient clipping rescales a gradient whose ℓ2\ell_2 norm exceeds a threshold.
  • Adversarial robustness is measured in ℓ∞\ell_\infty balls (every pixel may change by at most ϵ\epsilon).
  • Spectral normalization divides a layer's weights by ∥W∥2\lVert W \rVert_2 to keep GANs stable.

Exercises

Exercise 1: Compute three norms

Find ∥x∥1\lVert \mathbf{x} \rVert_1, ∥x∥2\lVert \mathbf{x} \rVert_2 and ∥x∥∞\lVert \mathbf{x} \rVert_\infty for x=(1,−2,2)\mathbf{x} = (1, -2, 2).

Answer

5, 3 and 2.

Solution

1+2+2=51 + 2 + 2 = 5; 1+4+4=9=3\sqrt{1 + 4 + 4} = \sqrt{9} = 3; max⁡(1,2,2)=2\max(1, 2, 2) = 2. In general ∥x∥∞≤∥x∥2≤∥x∥1\lVert \mathbf{x} \rVert_\infty \le \lVert \mathbf{x} \rVert_2 \le \lVert \mathbf{x} \rVert_1.

Exercise 2: Not a norm

Show that f(x)=x12+x22f(\mathbf{x}) = x_1^2 + x_2^2 is not a norm.

Answer

It fails absolute homogeneity.

Solution

f(2x)=4f(x)f(2\mathbf{x}) = 4f(\mathbf{x}), but a norm needs ∥2x∥=2∥x∥\lVert 2\mathbf{x} \rVert = 2\lVert \mathbf{x} \rVert. (Its square root, the ℓ2\ell_2 norm, is fine.)

Exercise 3: Soft thresholding (code)

Apply soft thresholding with λ=1\lambda = 1 to (2.5,−0.7,1.0,−3.0)(2.5, -0.7, 1.0, -3.0).

Answer

(1.5,0,0,−2)(1.5, 0, 0, -2).

Solution

Shrink each magnitude by 1 and clip at 0: 2.5→1.52.5 \to 1.5, 0.7→00.7 \to 0, 1.0→01.0 \to 0, 3.0→23.0 \to 2, keeping the signs.

python · try it yourself
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)?

  1. ∥w∥22\lVert \mathbf{w} \rVert_2^2
  2. ∥w∥1\lVert \mathbf{w} \rVert_1
  3. ∥w∥∞\lVert \mathbf{w} \rVert_\infty
  4. none of them

∥(3,−4)∥2\lVert (3, -4) \rVert_2 is…

  1. 7
  2. 5
  3. 4
  4. 1

The spectral norm ∥A∥2\lVert A \rVert_2 of a matrix equals…

  1. the sum of its entries
  2. its largest singular value
  3. its determinant
  4. its trace