NeuroGenesys.
The Geometry of LearningAcademy • Foundations • Lesson 1 of 3

Vectors, dot products and similarity

7 min read • By Will Schulz • NeuroGenesys AI Academy

Neural networks represent words, images and whole documents as lists of numbers, and then decide what is related to what by multiplying those lists together. The dot product is that multiplication, and understanding its geometry explains embeddings, retrieval and attention.

Vectors as points and arrows

A vector in Rd\mathbb R^d is an ordered list of dd real numbers, a=(a1,…,ad)a = (a_1, \dots, a_d). It can be read as a point in dd-dimensional space or as an arrow from the origin to that point. Vectors add component-wise, which places arrows tip to tail, and scale by multiplying every component by a number.

The length (Euclidean norm) of a vector is

∥a∥=a12+⋯+ad2,\|a\| = \sqrt{a_1^2 + \cdots + a_d^2},

Pythagoras' theorem extended to dd dimensions. A vector of length 1 is a unit vector; dividing any nonzero vector by its length normalises it.

In machine learning dd is large. A token embedding in a mid-sized transformer might have d=768d = 768 or 40964096 components. None of these dimensions need correspond to anything human-interpretable; what matters is the geometry, meaning the lengths of vectors and the angles between them.

The dot product

The dot product of two vectors is the sum of their component-wise products:

a⋅b=∑i=1daibi.a \cdot b = \sum_{i=1}^{d} a_i b_i.

Algebraically this is a single multiply-accumulate loop, the operation that modern accelerators are built to do quickly. Geometrically it has a second meaning:

a⋅b=∥a∥ ∥b∥cos⁡θ,a \cdot b = \|a\|\,\|b\|\cos\theta,

where θ\theta is the angle between the arrows. (This follows from the law of cosines applied to the triangle with sides aa, bb and a−ba - b.[1]) So the dot product is large and positive when the vectors point the same way, zero when they are perpendicular (orthogonal), and negative when they point in opposite directions. It also grows with the vectors' lengths.

Two related quantities fall out immediately.

Cosine similarity removes the length:

cos⁡θ=a⋅b∥a∥ ∥b∥∈[−1,1].\cos\theta = \frac{a \cdot b}{\|a\|\,\|b\|} \in [-1, 1].

Projection: the component of bb along the unit vector a^=a/∥a∥\hat a = a/\|a\| is a^⋅b\hat a \cdot b. A dot product with a unit vector measures "how much of bb points in this direction". Most linear-algebraic readings of neural networks, from a classifier's decision boundary to a probe for a concept, are projections.

Worked example

Let a=(1,2,2)a = (1, 2, 2) and b=(2,0,1)b = (2, 0, 1).

  • a⋅b=1⋅2+2⋅0+2⋅1=4a \cdot b = 1\cdot 2 + 2 \cdot 0 + 2 \cdot 1 = 4.
  • ∥a∥=1+4+4=3\|a\| = \sqrt{1 + 4 + 4} = 3, ∥b∥=4+0+1=5≈2.236\quad \|b\| = \sqrt{4 + 0 + 1} = \sqrt 5 \approx 2.236.
  • cos⁡θ=4/(3×2.236)≈0.596\cos\theta = 4 / (3 \times 2.236) \approx 0.596, so θ≈53°\theta \approx 53°.

Now double bb to 2b=(4,0,2)2b = (4, 0, 2). The dot product doubles to 8, but the cosine stays 0.596: the direction has not changed. Whether length should count is a modelling decision, and different systems decide differently.

Check your understanding

Vectors u=(3,4)u = (3, 4) and v=(−4,3)v = (-4, 3). What are u⋅vu \cdot v and the cosine similarity? What about uu and w=(6,8)w = (6, 8)?

Show answer +

u⋅v=−12+12=0u \cdot v = -12 + 12 = 0, so they are orthogonal and the cosine is 0. u⋅w=18+32=50u \cdot w = 18 + 32 = 50 and ∥u∥∥w∥=5×10=50\|u\|\|w\| = 5 \times 10 = 50, so the cosine is exactly 1: w=2uw = 2u points the same way.

Distance and similarity agree on the sphere

Expanding the squared distance gives

∥a−b∥2=∥a∥2+∥b∥2−2 a⋅b.\|a - b\|^2 = \|a\|^2 + \|b\|^2 - 2\,a \cdot b.

For unit vectors this is 2−2cos⁡θ2 - 2\cos\theta. Ranking neighbours by smallest Euclidean distance and ranking by largest cosine similarity therefore give the same order once vectors are normalised, which is why vector databases can offer either metric on normalised embeddings with identical results.

From documents to word vectors

Representing text as vectors and comparing them with cosines is older than neural networks. Salton, Wong and Yang's 1975 vector space model represented each document as a vector of term weights and retrieved documents by their similarity to a query vector.[2]

Neural embeddings learn the vectors instead of counting words. Mikolov and colleagues' word2vec models (2013) trained shallow networks to predict a word from its neighbours (continuous bag-of-words) or the neighbours from a word (skip-gram), and reported learning high-quality vectors from a 1.6-billion-word dataset in under a day.[3] Words used in similar contexts end up with high cosine similarity. A companion paper reported that relationships appeared as roughly consistent vector offsets: the vector king−man+woman\text{king} - \text{man} + \text{woman} lies closest, by cosine similarity, to queen\text{queen}.[4] Analogy arithmetic works only approximately and for some relations, but it shows that directions in embedding space can carry meaning.

Dot products inside attention

A transformer's attention layer is a soft lookup driven by dot products. Each position produces a query vector qq, and every position produces a key kjk_j and a value vjv_j. The score for attending from the query to position jj is q⋅kj/dkq \cdot k_j / \sqrt{d_k}; a softmax turns the scores into weights, and the output is the weighted average of the values.[5] The dk\sqrt{d_k} keeps the scores' spread independent of dimension (the variance argument appears in Expectation and concentration).

Attention uses the raw dot product, not the cosine, so a key's length acts as a volume control. A query that is perpendicular to a key gives that position a score of 0, not −∞-\infty, so the position still receives some weight after the softmax. Positions only drop out when other scores are much larger. The full mechanism is the subject of Attention from first principles.

Check your understanding

A query q=(1,0)q = (1, 0) meets keys k1=(2,0)k_1 = (2, 0) and k2=(0,3)k_2 = (0, 3), ignoring the dk\sqrt{d_k} scaling for simplicity. What are the scores and the softmax attention weights?

Show answer +

Scores: q⋅k1=2q \cdot k_1 = 2, q⋅k2=0q \cdot k_2 = 0. Weights: e2/(e2+e0)≈7.39/8.39≈0.88e^2/(e^2 + e^0) \approx 7.39/8.39 \approx 0.88 and ≈0.12\approx 0.12. The orthogonal key still gets 12% of the attention.

High dimensions behave differently

Intuition from two and three dimensions misleads in R768\mathbb R^{768}. If two vectors have independent random components with mean zero, their cosine similarity has mean zero and a standard deviation of about 1/d1/\sqrt d: roughly 0.036 at d=768d = 768. Random high-dimensional vectors are almost always nearly orthogonal. That is good news for capacity, since a space can hold far more nearly-orthogonal directions than it has dimensions, a fact interpretability research leans on (see Mechanistic interpretability).

Real learned embeddings are not random, however. Ethayarajh found that contextual representations in ELMo, BERT and GPT-2 are anisotropic, occupying a narrow cone rather than spreading in all directions; in GPT-2's last layer, two randomly chosen words had nearly perfect average cosine similarity.[6] A cosine of 0.9 can therefore mean "closely related" in one space and "unremarkable" in another. Similarity scores are meaningful relative to the distribution of scores in that particular space, not on an absolute scale.

Key takeaways

  • A dot product is a sum of products, and geometrically ∥a∥∥b∥cos⁡θ\|a\|\|b\|\cos\theta: it measures alignment, scaled by length.
  • Cosine similarity discards length; for unit vectors it ranks neighbours in exactly the same order as Euclidean distance.
  • Embeddings (from the vector space model to word2vec) place related items at small angles; attention scores are scaled dot products between queries and keys.
  • Random high-dimensional vectors are nearly orthogonal, but learned embedding spaces are often anisotropic, so judge similarity scores against their own baseline.

Sources

  1. G. Strang (2023). Introduction to Linear Algebra, 6th ed. Wellesley-Cambridge Press. https://math.mit.edu/~gs/linearalgebra/ila6/indexila6.html ↩

  2. G. Salton, A. Wong, C. S. Yang (1975). "A Vector Space Model for Automatic Indexing." Communications of the ACM 18(11): 613–620. https://doi.org/10.1145/361219.361220 ↩

  3. T. Mikolov, K. Chen, G. Corrado, J. Dean (2013). "Efficient Estimation of Word Representations in Vector Space." arXiv:1301.3781. https://arxiv.org/abs/1301.3781 ↩

  4. T. Mikolov, W. Yih, G. Zweig (2013). "Linguistic Regularities in Continuous Space Word Representations." NAACL-HLT 2013, 746–751. https://aclanthology.org/N13-1090/ ↩

  5. A. Vaswani et al. (2017). "Attention Is All You Need." NeurIPS 2017. arXiv:1706.03762. https://arxiv.org/abs/1706.03762 ↩

  6. K. Ethayarajh (2019). "How Contextual are Contextualized Word Representations? Comparing the Geometry of BERT, ELMo, and GPT-2 Embeddings." EMNLP 2019. arXiv:1909.00512. https://arxiv.org/abs/1909.00512 ↩

Leadership

A word from the founder — on why the brain is the blueprint, and why the next decade of intelligence will be built here.

"The brain is the universe's only proof that general intelligence is possible. We treat it as the blueprint."

From cortical interfaces to photonic silicon, every program at NeuroGenesys exists to answer one question: how does intelligence arise — and how do we build it safely.

Will Schulz

Will Schulz

Founder, NeuroGenesys • AW3 Technology