Mathematical Foundations
P10.text-representation.01 · Audience: Guest, IT/ML learner, Language professional
Where next?
Later in Text Representation
Go up a level
P10.text-representation.01 · Audience: Guest, IT/ML learner, Language professional
Later in Text Representation
Go up a level
A Transformer can look impenetrable from the outside, yet everything it does to a sentence is built from just four mathematical operations, repeated layer after layer. Once you can read those four — comparing vectors (lists of numbers) with cosine similarity, transforming them with matrix multiplication, turning raw scores into shares with softmax, and keeping the numbers stable with layer normalisation — no later module will hold anything unfamiliar. This module builds that intuition one operation at a time, computing each of them live so that every intermediate value stays visible.
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.
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 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.
Reads /api/text-representation/foundations/dot-product — loading
Computing…
Open in Labs: Dot product against cosine
Curated resources to explore text representation foundations further.
Self-check
Given a = [1, 0] and b = [0, 1], what is their cosine similarity?
Self-check
Given E = [[1,0],[0,1]] and W = [[2,3],[4,5]], what is entry [0, 1] of E × W?
Self-check
Given logits [1.0, 2.0], what is softmax([1, 2])[1]? (round to 4 d.p.)
Self-check
Given x = [1.0, 3.0, 5.0, 7.0], what is the mean that LayerNorm subtracts?
Self-check
Which Transformer operation uses dot products of Q and K vectors to decide which tokens to attend to?
Practise this lesson's 5 self-checks
One per screen: you reveal each answer and mark it yourself. Nothing is recorded.
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.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
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.
Loading your notes...