Introduction: measuring in data space
In ML, you constantly need to answer two questions:
- How big is this vector? (to control model weights, measure a gradient)
- How close are these two vectors? (to compare embeddings, classify a neighbor, evaluate a prediction)
Norms answer the first question. Distances and similarities answer the second. They are the fundamental measurement tools of applied linear algebra.
The norm: measuring the size of a vector
A norm is a function that assigns to each vector a non-negative number, representing its "size" or "length." Formally, a norm satisfies:
- and
- (homogeneity)
- (triangle inequality)
Different norms measure size in different ways. The choice of norm is not trivial, it fundamentally changes the behavior of algorithms.
L1 norm: the Manhattan distance
Definition
The L1 norm of a vector is the sum of the absolute values of its components:
Intuition
Imagine a grid of streets (like in Manhattan). To go from point A to point B, you can't cut diagonally, you have to follow the streets. The L1 distance is the distance "walking along the blocks."
For : .
Key property: sparsity
The L1 norm has a remarkable property: it promotes sparsity. When you minimize an objective with an L1 penalty, the solution tends to have many components exactly equal to zero.
Geometrically, the L1 "unit ball" (the set of vectors with L1 norm less than or equal to 1) is a diamond with tips on the axes. Optimal solutions tend to "land" on these tips, where coordinates are zero.

L2 norm: the Euclidean distance
Definition
The L2 norm is the square root of the sum of squares of the components:
It's the "natural" norm, the length of the vector in the usual geometric sense. It's the generalized Pythagorean theorem.
For : .
Properties
The L2 norm is related to the dot product:
The L2 "unit ball" is a sphere. Optimal solutions with an L2 penalty are small but rarely exactly zero, the L2 norm distributes the penalty uniformly across all components.
L-infinity norm: the maximum value
Definition
It's simply the largest component in absolute value. For : .
Intuition
Imagine a chessboard where each square is a king's move. The L-infinity distance between two squares is the number of moves needed, the king can move diagonally.
The L-infinity unit ball is a hypercube (a square in 2D, a cube in 3D).
Use in ML
The L-infinity norm is used in adversarial attacks. An adversarial perturbation with L-infinity norm bounded by means that no pixel is modified by more than . It's a realistic model of "imperceptible" perturbation.
Comparing norms
For a vector :
| Norm | Formula | Value |
|---|---|---|
| L1 | $ | 1 |
| L2 | ||
| L-infinity |
All norms are equivalent in finite dimensions (they define the same notions of convergence), but they don't behave the same way in optimization.
Ordering relation:

Distance between two vectors
The distance between two vectors and under a norm is simply the norm of their difference:
The L2 distance between and is the classic Euclidean distance:
Cosine similarity: comparing directions
Definition
Cosine similarity doesn't measure distance, but the angle between two vectors:
The result is between and :
- : the vectors point in the same direction (angle of 0)
- : the vectors are orthogonal (angle of 90 degrees)
- : the vectors point in opposite directions (angle of 180 degrees)

Why it's so important in ML
Cosine similarity ignores magnitude and only looks at direction. That's exactly what you want for comparing embeddings.
Concrete example: two documents of very different lengths (a tweet and an article) discuss the same topic. Their TF-IDF vectors have very different norms (the article has more words), but their directions are similar. Euclidean distance judges them "far apart," cosine similarity judges them "close."
It's the standard metric for:
- Semantic search: finding documents most similar to a query
- Word embeddings: measuring semantic proximity (Word2Vec, GloVe)
- RAG (Retrieval-Augmented Generation): retrieving relevant passages for a question
- Recommendation systems: comparing user profiles
Cosine distance
Cosine distance is simply . It equals 0 for vectors identical in direction and 2 for opposite vectors.
Applications in ML
L1 and L2 regularization
Regularization adds a penalty on the norm of weights to the loss function:
-
L2 regularization (ridge, weight decay): . Pushes all weights toward small values. Prevents overfitting by limiting the "size" of the model.
-
L1 regularization (lasso): . Pushes some weights exactly to zero. Produces sparse models, useful for feature selection.
-
Elastic Net: combines both, .
Loss functions
Several loss functions are defined in terms of norms:
-
MSE (Mean Squared Error): it's the squared L2 norm of the error (divided by ):
-
MAE (Mean Absolute Error): it's the L1 norm of the error (divided by ):
-
Cosine embedding loss: used to train embedding models to bring similar pairs closer and push dissimilar pairs apart.
MSE penalizes large errors more (because of the square). MAE is more robust to outliers.
k-NN (k nearest neighbors)
The k-NN algorithm classifies a point by looking at the closest points in the training dataset. The notion of "close" depends entirely on the choice of distance. Using L2, L1, or cosine distance gives different results, and the right choice depends on the problem.
Gradient clipping
During training, if the gradient becomes too large (gradient explosion), you "clip" it:
It's a normalization by the L2 norm of the gradient. Without this, training RNNs and Transformers would be unstable.
Why this matters for AI
Norms and distances are the measurement instruments of ML:
- Regularization (L1/L2) controls model complexity and fights overfitting
- Cosine similarity is the universal metric for comparing embeddings
- Loss functions (MSE, MAE) measure prediction quality in terms of norms
- Gradient clipping uses the L2 norm to stabilize training
Choosing the right norm or distance isn't a technical detail. It's a modeling decision that profoundly affects algorithm behavior. Understanding these tools means being able to make informed choices.
Where this is used. Cosine similarity is the measuring instrument of all semantic search: see text embeddings.