Core AI Intuitions · lesson 04/4
L1 vs L2 Norms
A norm measures the size of a vector. Two of them dominate practice: L1, the sum of absolute values, and L2, the Euclidean length. They differ in how they treat many small numbers versus one large one, and that difference drives regularization, robustness, and distance in embedding space.
The idea
The family of -norms is
with two members used constantly:
- L2 — , the straight-line length. It is smooth away from the origin and grows quadratically, so one large coordinate dominates the total.
- L1 — , the "taxicab" distance along the axes. It grows linearly, and every coordinate contributes equally.
- L∞ — , the largest single component.
The geometry explains the machine learning behavior. In two dimensions, the L2 unit ball is a circle, but the L1 unit ball is a diamond with corners exactly on the axes. Adding an L1 penalty therefore pushes solutions onto an axis, meaning some weights become exactly zero — this is sparsity, the basis of Lasso. An L2 penalty shrinks all weights smoothly toward zero without producing exact zeros. For the same reason, L2 loss is sensitive to outliers while L1 loss is not.
Worked example
For :
Now compare two 4-dimensional error vectors: and . Both have L1 norm , but while . A single error of costs twice as much as four errors of under L2, which is exactly why L1 is the robust choice.
In code
import torch
x = torch.tensor([3.0, -4.0])
print(torch.linalg.vector_norm(x, ord=1)) # tensor(7.)
print(torch.linalg.vector_norm(x, ord=2)) # tensor(5.)
print(torch.linalg.vector_norm(x, ord=float("inf"))) # tensor(4.)
print(x / torch.linalg.vector_norm(x, ord=2)) # tensor([ 0.6000, -0.8000])Dividing by the L2 norm projects the vector onto the unit circle, the standard step before a cosine similarity.
Check yourself
- Which of and has the larger L2 norm, and why does that matter when training on errors?
- Why does L1 regularization tend to produce exact zeros while L2 shrinks weights smoothly?
- Why is cosine similarity built on L2 normalization rather than L1 normalization?
Key takeaways
- L2 measures straight-line length and squares large components; L1 adds absolute values and treats every component equally.
- The corners of the L1 unit ball are what make L1 penalties sparse and L2 penalties smooth.
- L1 is robust to outliers, L2 is smooth and differentiable, and both appear everywhere in model training.