Math for AI course · Lesson 40 of 180

L1, L2, and L-infinity See Size Differently

Compare L1, L2, and L-infinity vector size, see their unit balls change, and distinguish parameter penalties from input-robustness budgets.

Share this article

A model can be sensitive to a change that looks tiny in every input coordinate. To describe that change, we first have to choose how to measure its size. The L1, L2, and L-infinity norms give different answers to that question—and their unit balls show exactly why.

By the end, you will be able to compare those norms, read their two-dimensional shapes, and tell a penalty on model parameters apart from a budget on input changes.

Learning outcome: Explain how different norms change geometry and sensitivity.

Lesson 39 introduced norms as rules for measuring vectors. Section 3.1 of the official Mathematics for Machine Learning book develops common norms and their geometry. The examples, comparison, interactive figure, and AI applications below are original.

One vector, three measurements

For a real vector x=(x1,…,xn)x=(x_1,\ldots,x_n), the L1 norm adds the absolute coordinate values, the L2 norm takes the square root of their squared sum, and the L-infinity norm keeps the largest absolute coordinate:

Take x=(3,−4)x=(3,-4). Its L1 norm is ∣3∣+∣−4∣=3+4=7|3|+|-4|=3+4=7. Its L2 norm is 32+(−4)2=9+16=5\sqrt{3^2+(-4)^2}=\sqrt{9+16}=5. Its L-infinity norm is max⁡(3,4)=4\max(3,4)=4. Nothing about the vector changed; each rule emphasizes a different aspect of its coordinates. L1 accumulates all coordinate magnitudes, L2 measures Euclidean length, and L-infinity reports the single largest change.

NumPy can compute each vector norm by setting ord explicitly:

import numpy as np

x = np.array([3.0, -4.0])
np.linalg.norm(x, ord=1)      # 7.0
np.linalg.norm(x, ord=2)      # 5.0
np.linalg.norm(x, ord=np.inf) # 4.0

Here x is one-dimensional, so ord=2 requests its Euclidean norm. With a two-dimensional array, numpy.linalg.norm can instead compute a matrix norm; specify both the norm order and the vector axis when working with batches of vectors. NumPy documents these vector and matrix cases separately.

Unit balls make the choice visible

A unit ball for a norm is the set of vectors whose norm is at most one. In two dimensions, the L1 unit ball is a diamond, the L2 unit ball is a circle, and the L-infinity unit ball is an axis-aligned square. For example, (0.6,0.8)(0.6,0.8) lies exactly on the L2 boundary because 0.62+0.82=1\sqrt{0.6^2+0.8^2}=1. It sits outside the L1 ball because 0.6+0.8=1.40.6+0.8=1.4, and inside the L-infinity ball because its largest coordinate magnitude is 0.80.8.

Move either coordinate below. The same point is tested against all three boundaries, while the table reports each norm and whether the point is inside its unit ball.

One vector inside three differently shaped unit balls
Choose the vector coordinates

Use the arrow keys to change a coordinate by 0.1. Each norm is measured on the same vector.

For x = (0.6, 0.8): L1 is 1.4 (outside); L2 is 1 (on the boundary); L-infinity is 0.8 (inside).

L1, L2, and L-infinity unit balls in a shared coordinate planeThe vector is (0.6, 0.8). The diamond is the L1 unit ball, the circle is the L2 unit ball, and the square is the L-infinity unit ball. For x = (0.6, 0.8): L1 is 1.4 (outside); L2 is 1 (on the boundary); L-infinity is 0.8 (inside).-1-1011x₁x₂

Unit-ball boundaries

  • L1: diamond boundary
  • L2: circle boundary
  • L-infinity: square boundary
Norm values and unit-ball membership for the selected vector
NormValuePosition
L11.4outside
L21on the boundary
L-infinity0.8inside

The status table remains available without interpreting the plotted colors or shapes.

The shape determines which directions fit under a fixed budget. At (0.7,0.7)(0.7,0.7), for instance, L-infinity is 0.70.7 and L2 is about 0.990.99, so the point is inside both unit balls; L1 is 1.41.4, so it falls outside the diamond. A square budget limits each coordinate separately, while a round budget limits their combined Euclidean length.

The norms still constrain one another. For every x∈Rnx\in\mathbb{R}^n,

∥x∥∞≤∥x∥2≤∥x∥1≤n ∥x∥2≤n ∥x∥∞.\lVert x\rVert_{\infty}\leq\lVert x\rVert_2\leq\lVert x\rVert_1 \leq\sqrt{n}\,\lVert x\rVert_2\leq n\,\lVert x\rVert_{\infty}.

The largest coordinate cannot exceed the square root of the sum of all squared coordinates, which cannot exceed the sum of the absolute coordinates. The Cauchy–Schwarz inequality gives the next bound, ∥x∥1≤n∥x∥2\lVert x\rVert_1\leq\sqrt{n}\lVert x\rVert_2; bounding every squared coordinate by the largest one gives ∥x∥2≤n∥x∥∞\lVert x\rVert_2\leq\sqrt{n}\lVert x\rVert_{\infty}. Thus the measures differ by factors that can grow with the dimension, and can rank two vectors differently. For u=(3,0)u=(3,0) and v=(2,2)v=(2,2), L1 says vv is larger (4>34>3), while L2 says uu is larger (3>83>\sqrt{8}).

Parameter penalties and input budgets answer different questions

During model fitting, regularization adds a term to the objective to discourage certain parameter values. A training objective might include λ1∥w∥1\lambda_1\lVert w\rVert_1 or λ2∥w∥22\lambda_2\lVert w\rVert_2^2, where ww is a vector of learned parameters and each λ\lambda sets the penalty strength. L1 penalties can favor sparse coefficients; squared L2 penalties tend to shrink many coefficients without generally making them zero. The feature scales and penalty strengths affect the result. The squared L2 expression is a squared norm, not itself a norm.

An input perturbation budget applies a norm to a different vector. If δ\delta is added to input xx, the constraint ∥δ∥∞≤ε\lVert\delta\rVert_{\infty}\leq\varepsilon means that every coordinate changes by at most ε\varepsilon. If image values have been scaled to [0,1][0,1], then ε\varepsilon is measured on that scale; clipping a modified image back into the valid input range is a separate constraint.

Goodfellow, Shlens, and Szegedy’s fast gradient sign method uses this L-infinity geometry. For an untargeted attack, let J(θ,x,y)J(\theta,x,y) be the model’s loss at parameters θ\theta, input xx, and label yy. A single step is

δ=ε sign⁡ ⁣(∇xJ(θ,x,y)).\delta=\varepsilon\,\operatorname{sign}\!\left(\nabla_xJ(\theta,x,y)\right).

The gradient estimates how a small input change affects the loss. Choosing each coordinate’s sign in that direction maximizes the first-order increase while keeping every coordinate within ε\varepsilon. This is a local one-step construction: it does not guarantee a misclassification, and its success depends on the model and input. Nor does a small L-infinity value guarantee that a change is imperceptible or realistic. The bound states a coordinate-wise budget; people must decide whether that budget matches the threat they care about.

When a model’s sensitivity is being evaluated, record what vector may change, which norm constrains it, and the units of its budget. Then a statement such as “robust within ε\varepsilon” has a precise geometric meaning instead of hiding its assumptions in the word small.

Check your understanding

Question 1

For x = (3, −4), compute its L1, L2, and L-infinity norms. Show the operation for each.

Show the step-by-step solution

L1 adds absolute values: ∣3∣+∣−4∣=7|3|+|-4|=7. L2 squares, adds, and takes a square root: sqrt(3² + (−4)²) = sqrt(9 + 16) = 5. L-infinity keeps the largest absolute coordinate: max⁡(3,4)=4\max(3,4)=4.

Question 2

For x = (−2, 1, −3), which norm is largest, and what are all three norm values?

Show the step-by-step solution

L1 is 2+1+3=62+1+3=6. L2 is sqrt((−2)² + 1² + (−3)²) = sqrt(4 + 1 + 9) = sqrt(14) ≈ 3.74. L-infinity is max⁡(2,1,3)=3\max(2,1,3)=3. Here L1 is largest because adding the nonnegative magnitudes is at least as large as either the Euclidean combination or the largest single coordinate.

Question 3

Is (0.7, 0.7) inside each two-dimensional unit ball? Calculate the norms before deciding.

Show the step-by-step solution

L1 is 0.7+0.7=1.40.7+0.7=1.4, so the point is outside the L1 ball. L2 is sqrt(0.7² + 0.7²) = sqrt(0.98) ≈ 0.99, so it is inside the L2 ball. L-infinity is max⁡(0.7,0.7)=0.7\max(0.7,0.7)=0.7, so it is inside the square-shaped L-infinity ball.

Question 4

Explain why the L-infinity norm cannot exceed the L2 norm, and why the L2 norm cannot exceed the L1 norm.

Show the step-by-step solution

Let m=max⁡i∣xi∣m=\max_i|x_i|. One squared coordinate equals m2m^2, and all other squared coordinates are nonnegative, so ∑ixi2≥m2\sum_i x_i^2\geq m^2. Taking square roots gives ∥x∥2≥m=∥x∥∞\lVert x\rVert_2\geq m=\lVert x\rVert_{\infty}. Next, squaring a sum of nonnegative values adds the individual squares plus nonnegative cross terms, so (∑i∣xi∣)2≥∑i∣xi∣2(\sum_i|x_i|)^2\geq\sum_i|x_i|^2. Taking square roots gives ∥x∥1≥∥x∥2\lVert x\rVert_1\geq\lVert x\rVert_2.

Question 5

A 1,000-coordinate perturbation satisfies ||δ||∞ ≤ 0.01. What coordinate-wise limit does that impose, and what upper bound follows for ||δ||₁?

Show the step-by-step solution

The L-infinity condition means ∣δi∣≤0.01|\delta_i|\leq0.01 for every coordinate ii. Adding the 1,000 absolute values gives ∥δ∥1=∑i∣δi∣≤1,000(0.01)=10\lVert\delta\rVert_1=\sum_i|\delta_i|\leq1{,}000(0.01)=10. A small maximum coordinate change can therefore allow a larger total absolute change in a high-dimensional input.

Question 6

Why must NumPy code for a vector batch specify the axis when computing each vector's norm, and why should a matrix's default not be assumed to mean its L2 operator norm?

Show the step-by-step solution

For an array containing several vectors, the axis argument identifies which coordinate dimension belongs to each vector; without it, NumPy may treat a two-dimensional array as a matrix and compute a matrix norm. The matrix 2-norm is the largest singular value, while the vector 2-norm is Euclidean length. Naming the array’s structure, ord, and axis makes the intended quantity explicit.

Question 7

Does an L2 penalty on model weights guarantee robustness to L-infinity-bounded input changes? Explain the two vectors involved.

Show the step-by-step solution

No. The penalty is applied to the parameter vector ww while fitting the model. An L-infinity robustness budget applies to an input perturbation vector δ\delta. They constrain different quantities in different parts of the model, so a weight penalty alone does not establish the stated input-robustness guarantee.

Question 8

For a loss gradient with respect to the input g = (2, −1) and ε = 0.01, what perturbation does the untargeted fast gradient sign step choose, and what first-order loss change does g predict?

Show the step-by-step solution

The coordinate signs are (+1,−1)(+1,-1), so the step is δ=0.01(1,−1)=(0.01,−0.01)\delta=0.01(1,-1)=(0.01,-0.01). Its predicted first-order change is g⋅δ=2(0.01)+(−1)(−0.01)=0.03g\cdot\delta=2(0.01)+(-1)(-0.01)=0.03. This is the linear approximation from the gradient, not a guarantee that the exact loss rises by 0.03 or that the prediction changes.

Sources

  1. Mathematics for Machine Learning book PDF, Section 3.1
  2. NumPy documentation: numpy.linalg.norm
  3. Goodfellow, Shlens, and Szegedy: Explaining and Harnessing Adversarial Examples