Embeddings
Token lookup + sinusoidal positional encoding.
Check yourself ↓ three questions and what to remember, at the end
Before the decoder can do anything with a character it needs a vector. Below, the character's token id picks one row of the embedding table, a positional vector that depends only on where the character sits is added to it element by element, and the result becomes one row of the matrix that block 1 reads.
Loading the animation…
Concept
The decoder doesn't know about characters or words — it knows about vectors. Two steps turn your input into vectors it can work with:
- Tokenise: chop the input string into tokens (here, individual characters from a 64-character alphabet) and map each one to an integer id.
- Embed: look up each id in a table of vectors. The table is one of the things the model learns. Each token gets the same vector every time it appears — that vector is what the model knows about that token.
There's still a problem: a single attention layer is blind to order. It treats the positions it can see as an unordered set, so the last position of "ab" and of "ba" would come out the same. The causal mask leaks some order (position 0 sees only itself), and a stack of two or more layers can recover order from that, but explicit positions are far more reliable (see NoPE in Architectures ch 2). To fix that we add a positional encoding to each row — a deterministic pattern that tells the model "this is position 0 / 1 / …". The original Transformer used a fixed sinusoidal pattern; we do the same here.
Try changing the input below. Watch the rows shift around (each row is one token's vector) and the positional pattern stay still (it depends on position only, not on what you typed).
Maths
Let be the embedding table, with rows (one per token) and columns (the model dimension). For an input sequence of token ids , the lookup gives one row per position:
The sinusoidal positional encoding (Vaswani et al. 2017, §3.5) is the matrix with entries
The block-0 input is the elementwise sum of the two: .
Code
// src/lib/transformer/embeddings.ts (excerpt)
export function tokenEmbedding(ids: number[], embedding: Matrix): Matrix {
return ids.map((id) =>
id < 0 || id >= embedding.length
? new Array(embedding[0]?.length ?? 0).fill(0)
: embedding[id]!.slice(),
);
}
export function positionalEncoding(seqLen: number, dModel: number): Matrix {
const halfD = Math.floor(dModel / 2);
const factor = -Math.log(10000) / dModel;
const div = Array.from({ length: halfD }, (_, i) => Math.exp(2 * i * factor));
const out: Matrix = [];
for (let pos = 0; pos < seqLen; pos++) {
const row = new Array(dModel).fill(0);
for (let i = 0; i < halfD; i++) {
row[2 * i] = Math.sin(pos * div[i]!);
if (2 * i + 1 < dModel) row[2 * i + 1] = Math.cos(pos * div[i]!);
}
out.push(row);
}
return out;
}
The token-embedding lookup is one line; the positional encoding is twelve. That's the whole "input layer" of the Transformer.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: No: a different positional vector is added at each position
No. Both a's pick the same embedding row, but adds a different positional vector at each position, so the rows differ. Without that, a single attention layer could not tell “ab” from “ba”.
Pick one, then reveal.
Reveal the answer
Answer: 8 (exact)
8. At every even element is and every odd one is , so half the elements are 1. Position 0 is a position like any other, not "no position".
Commit to a number, then reveal.
Reveal the answer
Answer: The output head: the logits are the final stream times Eᵀ
The output head is tied to the embedding: the logits are , so a token's row is both how it is read in and how it is scored on the way out. That is why receives its gradient twice in training.
Pick one, then reveal.
Comments
Be the first to leave a comment on this section.
Sign in (top-right) to leave a comment.