Inner products, norms and orthogonality
Lengths, distances and angles in any dimension: the dot product, the ℓ₁, ℓ₂ and ℓ∞ norms, Cauchy–Schwarz and cosine similarity of embeddings, orthogonal projection, least squares and the normal equations, orthogonal matrices and the QR decomposition.
The dot product: length and angle
The topic Matrices and linear systems ended with systems that have no solution. Take three equations in two unknowns:
The first two force , not . In matrix form has no exact solution, but some come closer than others. To say "closer" we need lengths, and lengths come from one operation.
The dot product of multiplies matching coordinates and adds them up:
It gives the length and the distance . In the plane it also gives the angle, by the law of cosines:
For and : , both lengths are , so and . In there is no protractor, so we turn this around and define the angle by the formula.
Two vectors are orthogonal, , when . Then Pythagoras holds in any dimension: .
The dot product is one example of an inner product: a rule that is symmetric, linear in each argument and gives for every . Another is with and all . It weighs features differently, which is what rescaling features does to a model's geometry. Below, means the plain dot product.
The ℓ₁, ℓ₂ and ℓ∞ norms
The length is one way to measure size. A norm is any rule with three properties: for , , and the triangle inequality . Three norms do most of the work in ML:
For they give , and . Only comes from an inner product; without a subscript means . The test is the parallelogram law: expanding both squares and adding shows that a norm from an inner product has . For and the norm gives against , and gives against .
Each norm has its unit ball . In the plane the ball is a diamond, the ball is a disc and the ball is a square. The point has norms , and : outside the diamond, on the circle, inside the square.
The three norms are always ordered, and the gaps are at most :
The first chain holds because the largest is at most the sum of all of them, and is plus nonnegative cross terms. The last bound holds because each of the terms of is at most . The middle one needs Cauchy–Schwarz, the next block.
Each norm has its job. Weight decay penalizes and shrinks all weights a little. The lasso penalizes ; the diamond's corners lie on the axes, so the optimum often has exact zeros. Adversarial attacks bound : no pixel may change by more than . For a whole weight matrix the analog of is the Frobenius norm .
The Cauchy–Schwarz inequality
The angle formula only makes sense if lies in . The Cauchy–Schwarz inequality guarantees it in every dimension:
with equality exactly when one vector is a multiple of the other.
The proof uses only the fact that a squared length is never negative. For and any number ,
The right-hand side is a quadratic in that never goes below zero, so it has at most one real root and its discriminant is not positive: . That is the inequality. Equality means the quadratic touches zero, so for some .
Example: and give , and the angle is about .
Three consequences come up again and again. The triangle inequality for follows by bounding the cross term: . The bound from the previous block is Cauchy–Schwarz for and : . And a correlation coefficient always lies in : it is the cosine between two centered data vectors.
Cosine similarity of embeddings
Embeddings of words, images or users are compared by the angle between them. Cosine similarity is
It ignores length: doubling a vector changes nothing. That helps when length reflects something irrelevant, like how frequent a word is.
For unit vectors, cosine and Euclidean distance carry the same information. Expanding the square with gives
So after normalizing all embeddings to length , the nearest neighbors by distance are exactly the most similar by cosine. Many retrieval systems normalize once and then use the dot product. Attention, by contrast, scores queries against keys with raw dot products , so there length matters.
What counts as "similar" depends on the dimension. Two random directions in are almost orthogonal: their cosine is around with standard deviation about . For that is , so a cosine of is more than five standard deviations away from chance. The same in would mean almost nothing.
Projection onto a line
Now the question we started with, in its simplest form: which point on the line through is closest to ? Write . The best point is the one where the error is orthogonal to the line:
Why the perpendicular wins: for any other point on the line, lies along and is orthogonal to . Pythagoras gives .
For and : , and . Indeed and . The distance from to the line is .
The projection is a linear map of , so it is a matrix:
Projecting twice changes nothing, . In ML, is the best single weight when one feature predicts the targets .
Least squares
Back to the system with no solution. The columns of span its column space, and ranges over all of it. The closest we can get is the projection of onto that space. As with a line, the error must be orthogonal to the space, that is, to every column: . Stacking these equations gives the normal equations:
When the columns are independent, is invertible: if , then , so and . Then . This minimizes , which is why the method is called least squares. In the regression notation of the previous topic this is : the rows of are training examples, holds the weights, holds the targets, and is the sum of squared errors.
In our example has columns and , and :
The fit is and the error is , orthogonal to both columns. No gets closer to than . The best compromise raised and above and to bring their sum closer to .
The projection matrix onto the column space is
It is symmetric and idempotent, and projects onto the directions orthogonal to every column, where the errors live.
The underdetermined case asks for the shortest exact solution. That is an orthogonality condition too: the shortest solution has no component in the null space of . The topic The singular value decomposition gives a formula for it.
Orthonormal bases and orthogonal matrices
Projections become easy when the basis is orthogonal. Vectors are orthonormal when each has length and they are pairwise orthogonal. As columns of a matrix , that reads
Coordinates in such a basis need no system to solve. Each one is a dot product: with . For , and the coordinates are and , and indeed . The projection onto their span is just : the normal equations with .
A square with is an orthogonal matrix. Its inverse is its transpose, . It keeps all the geometry:
Rotations, reflections and permutations are orthogonal. In ML this is why PCA (later topics) only rotates the data into new axes without distorting it, and why orthogonal initialization of weights keeps the signal's scale through many linear layers: no direction is stretched or squashed.
Gram–Schmidt and the QR decomposition
Any independent columns can be made orthonormal. The Gram–Schmidt process goes column by column: from subtract its projections onto the already built, then normalize what is left:
For and : ; then , , and .
Recording the coefficients gives the QR decomposition , with and upper triangular, and :
Least squares then reduces to a triangular system. Substitute into the normal equations, use and cancel , which is invertible because is triangular with a positive diagonal:
It is solved from the bottom row up, and is never formed. Libraries prefer this route to the normal equations: forming loses precision, as the topic Conditioning and numerical stability will show. They also orthogonalize with Householder reflections rather than Gram–Schmidt, for the same reason.
An orthogonal matrix keeps every length. A general matrix does not: it stretches some directions more than others and turns most vectors as well. Are there directions that a matrix only stretches, without turning? That is the question of the next topic, Determinants and eigenvalues.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.