Mathematical Foundations
P10.text-representation.01 · Audience: guest, it-ml, language-pro
Prerequisite concepts needed before diving into Transformers — four mathematical operations that appear in every layer, each computed live with every intermediate value shown.
What this means in plain words
A word can be described by several properties at once — for example, how formal it is, how concrete (versus abstract) it is, or how emotionally charged it is. If we write each property as a number, we get a list: [0.8, 0.2, 0.5]. That list is a vector. Two words that describe similar things will have similar numbers in their lists; two unrelated words will have very different numbers.
Cosine similarity is a way to measure how alike two such lists are — a score of 1 means the two word-descriptions point in exactly the same direction, 0 means they share nothing in common.
Note: a vector here is a list of numbers — not a directed arrow anchored at a point in physical space. The geometric diagram below is just one way to picture the angle between two lists; the list itself is what matters.
🗣️ From a linguist's perspective: vectors as semantic feature profiles
ⓘ Concept: Vectors and Cosine Similarity
Why it matters — Cosine similarity is used throughout Transformers: in attention to measure query–key alignment, and at inference time to find the nearest sentence embedding.
Reference: 3Blue1Brown — Essence of Linear Algebra
Try it — adjust vectors and see similarity live
Try this ▶ Drag vector b to match vector a exactly and watch the cosine similarity reach 1.0 — then point them in opposite directions to see it drop toward −1.0.
Computing…
📚 Go further
Curated resources to explore text representation foundations further.
- Jurafsky & Martin, Speech and Language Processing (3rd ed.) — Ch. 2: Regular Expressions, Text Normalisation, Edit Distance (free PDF draft)
- Jay Alammar — The Illustrated Word2Vec — visual introduction to word representations
- Hugging Face — NLP Course, Chapter 1: Transformer Models (free)
Check your understanding
Given a = [1, 0] and b = [0, 1], what is their cosine similarity?
Given E = [[1,0],[0,1]] and W = [[2,3],[4,5]], what is entry [0, 1] of E × W?
Given logits [1.0, 2.0], what is softmax([1, 2])[1]? (round to 4 d.p.)
Given x = [1.0, 3.0, 5.0, 7.0], what is the mean that LayerNorm subtracts?
Which Transformer operation uses dot products of Q and K vectors to decide which tokens to attend to?
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Try it yourself
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Where next?
Later in Text Representation
Go up a level