liveBook · Manning

L2 norm

As explained in Natural Language Processing in Action, Second Edition

The L2 norm of a vector is the square root of the sum of the squares of its components.

The L2 norm measures a vector’s length: square each component, add the squares, and take the square root. Geometrically, it is the diagonal length of a right triangle extended across every dimension. section 3.3 section 6.4.6

Use the L2 norm to:

  • Calculate Euclidean distance by subtracting two vectors first, then measuring the L2 norm of their difference. section 3.3
  • Normalize vectors by dividing each vector by its own L2 norm, giving vectors with length 1. section 6.4.6
  • Relate vector length to cosine similarity, which divides a dot product by the L2 norm of each vector. section 3.3.1

Compute and normalize a vector

For vector [1, 2, 3, 4], the L2 norm is sqrt(1² + 2² + 3² + 4²), approximately 5.47. NumPy computes it with np.linalg.norm(vector), then normalizes the vector with vector / np.linalg.norm(vector). The same operation scales 300-dimensional sentence embeddings to a common length while preserving their directional differences. section 6.4.6 section 6.4.6

Figure 3.2 2D Euclidean distance
figure

Inspect the two-dimensional geometry of Euclidean distance and the vector difference whose L2 norm supplies that distance. section 3.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines the formula, explains geometric consequences, and demonstrates NumPy normalization without deriving deeper linear-algebra theory.
Examples
Uses two-dimensional vector geometry, a four-element NumPy vector, TF vectors, and normalized sentence embeddings.
What it assumes
Assumes vectors, component-wise arithmetic, and the Pythagorean theorem while introducing the norm through geometric intuition.
What you can do after
Apply it via NumPy to calculate vector lengths, normalize embeddings, and support cosine-similarity computations.
How it connects
Connects Euclidean distance, cosine similarity, dot product, vector normalization, sentence embeddings, and L1 norm.

Also explained in

Compare how 3 books explain l2 norm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.