Last updated 19 October 2026
What is the dot product?
The dot product of two vectors multiplies matching entries and adds them up: . Geometrically it equals the product of their lengths times the cosine of the angle between them, so it measures how much two vectors point the same way. A norm measures a single vector's length.
At the heart are two fundamental operations: Dot Product (alignment) and Norms (magnitude).
Every neural network prediction, recommender system suggestion, and regularization penalty uses these operations. Together, they form the foundation of how machines understand similarity, distance, and direction in high-dimensional space.
Why These Matter
Understanding dot products and norms is crucial for modern ML architectures. Matrix multiplication is just batched dot products. Transformer attention computes similarity via scaled dot products. Regularization techniques like Ridge and Lasso use different norms to constrain model complexity.
The Dot Product
The dot product (also called inner product or scalar product) takes two vectors of equal length and returns a single scalar. It is the most fundamental operation in linear algebra.
Multiply corresponding elements, then sum.
Algorithm
a = [1, 3]
b = [4, -2]
a · b = (1)(4) + (3)(-2) = -2
Matrix Notation
Row vector × Column vector. This generalizes to specific rows and columns in larger matrices.
Geometric Interpretation
The dot product measures alignment between vectors. It bridges algebra and geometry.
Product of magnitudes times the cosine of the angle between them.
Positive
Vectors point in roughly the same direction ().
Zero
Vectors are orthogonal (perpendicular) ().
Negative
Vectors point in opposite directions ().
The Projection View
Think of it as casting a shadow of vector a onto b. The dot product tells you: "How much of a goes in the direction of b?". This "projection" concept is why dot products measure similarity.
Properties of the Dot Product
Commutative
Order does not matter.
Distributive
Distributes over vector addition.
Self Dot Product
Dotting a vector with itself gives the squared norm (magnitude).
Interactive: Dot Product
Drag the sliders to change vectors. See how the dot product relates to angle, cosine, and projection.
Vector norms: L1 vs L2 vs infinity
A norm measures the "size" or "magnitude" of a vector. Different norms measure size in different ways, each with unique geometric interpretations and ML applications.
L2 Norm (Euclidean)
Straight-line distance. Used in Ridge Regression, KNN.
L1 Norm (Manhattan)
Sum of absolute values (taxi-cab distance). Promotes sparsity (Lasso).
L-infinity Norm (Max)
Largest single element. Used in adversarial robustness.
| L1 | L2 | Infinity | |
|---|---|---|---|
| Formula | |||
| For | 8 | about 5.10 | 4 |
| Unit ball in 2D | Diamond | Circle | Square |
| As a penalty | Lasso: pushes weights to exactly zero | Ridge: shrinks all weights smoothly | Caps the largest weight |
Properties of Norms
For a function to be a valid norm, it must satisfy three axioms:
- Non-negativity: Norm is always non-negative. Zero only if the vector is zero.
- Absolute Homogeneity: Scaling the vector scales the norm by the absolute scalar value.
- Triangle Inequality: The direct path is always shorter than any detour.
The p-Norm Family
All common norms are special cases of the generalized p-norm (where ).
L1 Norm (Manhattan)
L2 Norm (Euclidean)
Higher-order norms
L-infinity (Max)
Interactive: Unit Balls
The "unit ball" (vectors with norm = 1) has a different shape for each p-norm. This shape explains why L1 regularization promotes sparsity (sharp corners on axes).
Vector Norms & Unit Balls
Visualizing for different p-norms.
Normalization (Unit Vectors)
Normalization converts a vector to a unit vector (norm = 1) pointing in the same direction, isolating direction from magnitude.
Divide by the norm.
Applications
- Cosine Similarity: Normalized vectors reduce dot product to cosine similarity.
- Batch Norm: Normalizes layer activations to stabilize training.
- Word Embeddings: Often normalized so only direction (meaning) matters, not frequency.
Cosine similarity
Dividing the dot product by both lengths leaves only the angle:
The result runs from -1 (opposite directions) through 0 (perpendicular) to 1 (same direction), whatever the vectors' lengths. That is why embedding search and recommendation systems compare vectors with cosine similarity: a long document and a short one about the same topic still score as similar. If the vectors are already L2-normalized, cosine similarity is just their dot product.
Python implementation
NumPy computes all of these in one line each. np.linalg.norm takes the norm order as its second argument and defaults to L2:
import numpy as np
a = np.array([3.0, 4.0])
b = np.array([4.0, 3.0])
x = np.array([3.0, -4.0, 1.0])
print(f"dot product = {a @ b:.3f}")
print(f"cosine similarity = {a @ b / (np.linalg.norm(a) * np.linalg.norm(b)):.3f}")
print(f"L1 norm = {np.linalg.norm(x, 1):.3f}")
print(f"L2 norm = {np.linalg.norm(x):.3f}")
print(f"Linf norm = {np.linalg.norm(x, np.inf):.3f}")
print(f"unit vector L2 norm = {np.linalg.norm(x / np.linalg.norm(x)):.3f}")dot product = 24.000 cosine similarity = 0.960 L1 norm = 8.000 L2 norm = 5.099 Linf norm = 4.000 unit vector L2 norm = 1.000
The two example vectors are 5 units long and almost aligned, so their cosine similarity is 0.96. The vector gives the three different norms from the table, and dividing it by its L2 norm leaves a unit vector.
ML Applications
Regularization
Penalizing the norm of weights prevents overfitting.
- L1 (Lasso): - creates sparse models (feature selection).
- L2 (Ridge): - distributes weight values, prevents unstable large weights.
Attention Mechanism
Transformer attention is a scaled dot product. It calculates relevance scores between tokens.
Distance Metrics
KNN and K-Means rely on L2 distance . Using different norms here changes the algorithm's behavior drastically.
Computational Considerations
Time Complexity
Hardware
GPUs optimize these O(n) ops via massive parallelism (SIMD). Always use vectorized libraries (PyTorch/NumPy), never loops.
Practice this
On TensorTonic you implement this from scratch and test it in the browser.
- Dot product: the operation this lesson starts from.
- Implement Cosine Similarity: normalize by both L2 norms and handle zero vectors.
- L2 normalization: turn vectors into unit vectors.
- Euclidean distance: the L2 norm of a difference.