Create your own
Lesson illustration

Geometric Interpretation of Dot Products, Norms, Cosine Similarity, and Projections

Welcome back. In the previous lesson, you treated vectors and matrices as computational objects: you tracked shapes, distinguished elementwise products from matrix products, and used the dot product as a weighted sum. That operational view is essential in PyTorch. This lesson adds the geometric view: a dot product tells us about length, direction, alignment, and the portion of one vector that lies along another.

These ideas recur throughout ML: normalized embedding retrieval uses cosine similarity; ranking systems may use raw dot products; attention uses query–key dot products; and projections underlie least-squares reasoning. By the end, you should be able to calculate each quantity and explain what its value means geometrically.


Norms: the size of a vector

A norm assigns a nonnegative size to a vector. In machine learning, the default meaning of “vector length” is usually the L2 norm:

Geometrically, is the Euclidean distance from the origin to the tip of . For

we have:

The important identity connecting the prior lesson to this one is:

In words: a vector dotted with itself is its squared L2 length.

Other norms measure size differently:

For :

NormValueGeometric intuition
Manhattan-style distance: total coordinate movement
Straight-line Euclidean distance
Largest coordinate magnitude

For this lesson, L2 is central because it is the norm used to define angles, cosine similarity, and ordinary orthogonal projection. L1 will later matter for sparsity and regularization, while L2 regularization penalizes large parameter vectors.

What is Norm in Machine Learning?

Watch “What is Norm in Machine Learning?” from Normalized Nerd for a compact visual treatment of vector magnitude and the geometry of common norms.

Watch the setup to establish what a norm measures. Then watch L2 geometry, L1 geometry, and general norms. Focus on how the shape of the unit-norm boundary changes: a diamond for L1, a circle for L2, and a square-like shape for L-infinity.

A subtle ML caveat: a norm has meaning only relative to the coordinate system. If one feature is measured in dollars and another in milliseconds, raw Euclidean length can be dominated by whichever feature has the largest numerical scale. This is one reason numerical features are often standardized before distance-based modeling.


The dot product measures aligned length

For equal-length vectors, the dot product is computed algebraically as:

But its geometric identity is more revealing:

where is the angle between the two nonzero vectors.

This says that a dot product combines two factors:

  1. The vectors’ lengths, and .
  2. Their directional alignment, .

The sign of the dot product has an immediate geometric interpretation:

Relative directionAngleDot product
Similar directionPositive
PerpendicularZero
Generally opposite directionNegative

For example, let:

Then:

Both vectors point generally up and to the right, so a positive dot product is expected.

Now compare with:

So and are orthogonal: they meet at a right angle. Notice that neither vector is zero. A zero dot product does not imply that one of the vectors is zero; it implies perpendicularity when both are nonzero.

Dot products and duality | Chapter 9, Essence of linear algebra

Watch the selected segment of 3Blue1Brown’s “Dot products and duality” for the geometric “shadow” interpretation of a dot product and its behavior under scaling.

Start with the shadow view: visualize one vector’s signed shadow onto the direction of the other. Then watch scaling symmetry, which explains why either vector can be viewed as the projected one even though the dot product is symmetric.

The angle itself is recoverable from the dot product:

This expression is undefined when either vector is the zero vector: the zero vector has no direction, so an angle with it is not defined.


Cosine similarity: compare direction, ignore magnitude

Cosine similarity isolates the angular part of the dot product:

Its value lies in the interval:

The interpretation is:

  • : same direction
  • : orthogonal directions
  • : opposite directions

Consider:

Their dot product is:

Their norms are and , respectively, so:

The two vectors differ in length, but their directions are identical. Cosine similarity deliberately treats them as maximally similar.

This is why cosine similarity is common for embeddings. If the question is “Which document embedding points most like this query embedding?”, the direction may be what matters. But cosine similarity discards magnitude by design. Whether that is desirable is a modeling decision, not a universal rule.

Measuring similarity from embeddings | Machine Learning

Read Google’s Machine Learning guide section to connect the geometry to a practical embedding-retrieval choice: Euclidean distance, cosine similarity, and dot product do not retain the same information.

Begin with the comparison table at the top of the page. Then, in “Choosing a similarity measure,” read the length effect. Focus on the trade-off: raw dot product can intentionally retain signals associated with vector magnitude, while cosine similarity removes them.

Dot product versus cosine similarity in an ML system

The relationship is:

So a high dot product can mean:

  • strong directional alignment,
  • large vector norms,
  • or both.

In a recommender system, an item embedding with a large norm might encode a popularity-related signal. A raw dot product can preserve that signal. However, it can also cause popular items to dominate scores even when their direction is not the best semantic match. Unit normalization removes this magnitude effect.

For nonzero vectors, define normalized versions:

Then:

For unit-normalized vectors, Euclidean distance and cosine similarity produce equivalent rankings:

Thus, minimizing Euclidean distance between unit vectors is the same as maximizing their cosine similarity. This equivalence is useful when choosing a vector index metric: it applies only after normalization.


Projection: extracting the component in one direction

A projection answers a more specific question than similarity:

How much of lies in the direction of ?

There are two related answers.

The scalar projection is a signed number:

where:

is the unit vector in ’s direction.

The scalar projection tells you the signed length of along the axis defined by . It is positive when points generally along , zero when it is perpendicular, and negative when it points generally opposite to .

The vector projection is the vector that lies along :

This is a scalar multiple of , so it must be parallel to .

A geometric view of projecting vector \(\mathbf{a}\) onto vector \(\mathbf{b}\): the signed scalar projection is \(\mathbf{a}^{\top}\mathbf{b}/\|\mathbf{b}\|_2=\|\mathbf{a}\|_2\cos(\theta)\), while the corresponding vector projection lies along \(\mathbf{b}\).

Take the same vectors:

We already found:

and:

Therefore:

The part of that is not explained by the direction is the residual:

Check orthogonality:

So the original vector decomposes as:

This decomposition is the geometric basis for approximation: preserve the component in an allowed direction or subspace, and leave the unexplained remainder as an orthogonal residual.

2.9: The Dot Product and Projection - Mathematics LibreTexts

Read the projection portion of Mathematics LibreTexts to consolidate the geometric construction, the scalar-versus-vector distinction, and the parallel-plus-orthogonal decomposition.

In the subsection beginning with the discussion of two nonzero vectors and an acute angle, follow the construction that drops a perpendicular from the tip of one vector onto the other vector’s direction. Read through “Definition: vector projection” and the “Alternate Formulas for Vector Projections” discussion. Use the alternate formulas to compare the unit-vector form with the computationally convenient \bigl(\mathbf{a}^{\top}\mathbf{b}/\mathbf{b}^{\top}\mathbf{b}\bigr)\mathbf{b} form.


PyTorch: calculate, normalize, project

For individual vectors, make each geometric quantity explicit:

import torch
import torch.nn.functional as F

a = torch.tensor([3.0, 4.0])
b = torch.tensor([2.0, 1.0])

# Lengths and alignment
a_norm = torch.linalg.vector_norm(a)         # 5.0
b_norm = torch.linalg.vector_norm(b)         # sqrt(5)
dot = torch.dot(a, b)                        # 10.0
cosine = dot / (a_norm * b_norm)

# Projection of a onto b
b_hat = b / b_norm
scalar_projection = torch.dot(a, b_hat)

vector_projection = scalar_projection * b_hat
# Equivalent formula:
vector_projection_2 = (dot / torch.dot(b, b)) * b

residual = a - vector_projection

assert torch.allclose(vector_projection, vector_projection_2)
assert torch.allclose(torch.dot(residual, b), torch.tensor(0.0))

For embedding retrieval, cosine similarity is commonly implemented by normalizing each embedding and then taking dot products:

# E: one candidate embedding per row, shape (num_candidates, d)
# q: one query embedding, shape (d,)

E_unit = F.normalize(E, p=2, dim=1)
q_unit = F.normalize(q, p=2, dim=0)

cosine_scores = E_unit @ q_unit  # shape: (num_candidates,)

This works because the dot product of unit vectors equals cosine similarity. In a production system, handle zero or near-zero vectors deliberately; normalization requires a nonzero norm, and a near-zero embedding may signal an upstream data or model issue.

For an interview explanation, the concise version is:

The dot product is length times directional alignment. Cosine similarity divides out length, so it measures angle only. Projection goes further: it produces the component of one vector parallel to another and leaves an orthogonal residual.


You now have four connected interpretations:

  • A norm measures vector size.
  • A dot product combines size and directional alignment.
  • Cosine similarity measures alignment after removing size.
  • A projection extracts the component of one vector in another vector’s direction.

These are not merely geometric vocabulary. They determine what a retrieval system ranks, what a normalized embedding comparison means, and why residual errors are orthogonal in projection-based approximations.

Next, you will shift from individual vectors to matrices as transformations: eigendecomposition will reveal directions that a symmetric matrix preserves and the scaling applied along each of those directions.

Can't find a good explanation? Sign up and we'll make it for you

Sign up