Worked examples — Embedding layers and tied weights
This is the do-it-by-hand companion to the parent note. Every symbol here is used exactly the way the parent defined it: is the embedding matrix (one row per token), is how many tokens exist, is how long each token's vector is, and is the "context vector" the transformer produces just before predicting the next token. If any of those feel shaky, re-read the parent first — here we only compute.

The scenario matrix
Every problem this topic can throw at you falls into one of these cells. The examples below are each tagged with the cell they cover, and together they hit all of them.
| Cell | What makes it tricky | Covered by |
|---|---|---|
| A. Plain lookup | Just index a row | Example 1 |
| B. One-hot equivalence | Show lookup = matrix multiply | Example 2 |
| C. Tied output logits | All dot products at once | Example 3 |
| D. Sign behaviour | Negative vs positive alignment | Example 4 |
| E. Zero / degenerate input | , or identical rows | Example 5 |
| F. Limiting value | One logit , softmax saturates | Example 6 |
| G. Real-world word problem | Memory budget of an actual model | Example 7 |
| H. Exam twist | Gradient accumulates from two paths | Example 8 |
[!definition] The one word we must earn: dot product
Before any output-side example, we need the dot product. Take two lists of numbers of the same length, say and .
The dot product means: multiply matching slots, then add:

That is the whole engine of a tied output layer: logit for token = dot product of the context with token 's drawer vector.
[!example] Example 1 — Cell A: plain lookup
Statement. With tokens ["cat","dog","the","ran"], , and
retrieve the embedding of "the".
Forecast: guess — do we compute anything, or just read a row?
- Find the ID.
"the"is the 3rd word, and IDs start at , so ID . Why this step? The parent stressed IDs are zero-indexed — off-by-one here silently returns the wrong word. - Open drawer 2. Take row index : . Why this step? Embedding is retrieval, not multiplication — there is nothing to calculate.
Answer: .
Verify: The result must be one of the four rows exactly (no arithmetic can invent new numbers). Row 2 matches. ✓
[!example] Example 2 — Cell B: lookup equals one-hot × matrix
Statement. Show that retrieving "dog" (ID ) equals , where .
Forecast: guess — will the one-hot pick out exactly row 1, or blend rows?
- Write the multiply slot by slot. Column of the answer is . Why this step? This is what "row-vector times matrix" means — sum down each column, weighted by the one-hot.
- Kill every zero. is everywhere except slot , so every term dies except . Why this step? This is why we never build the one-hot in practice — we'd multiply thousands of numbers by zero. Indexing skips straight to the survivor.
- Read off the survivor. Answer .
Verify: Equals from direct lookup — identical to Example 1's method on row 1. ✓
[!example] Example 3 — Cell C: tied output logits
Statement. Context vector , tied weights , bias . Find all four logits and the softmax.
Forecast: which token wins — cat, dog, the, or ran?
- One dot product per token. Logit . Why this step? Tying turns "predict next token" into "score each drawer by alignment with context" — Section: dot product.
- Compute:
- Softmax (exponentiate, then normalise so they sum to 1): . Why this step? Softmax turns "alignment scores" into probabilities that add to 1 — see 5.1.03-Cross-entropy-loss for how these are then scored.
Answer: ran (ID 3) wins at .
Verify: Probabilities sum to ; the biggest logit () maps to the biggest probability. ✓
[!example] Example 4 — Cell D: sign behaviour of alignment
Statement. Same . Compare two contexts against the (): and .
Forecast: guess the sign of each score before computing.
- Score A. . Why this step? points against the first axis, and so does (its first slot is ). Two negatives → positive → aligned.
- Score B. . Why this step? points with the axis, opposite to → anti-aligned → negative logit → low probability.
Answer: vs — flipping the context sign flips who the model wants.
Verify: The two scores are exact negatives, as expected when only the sign of flips. ✓ See the two-arrow figure again for the geometry.
[!example] Example 5 — Cell E: degenerate inputs
Statement. (a) What are all logits when ? (b) If cat and dog had identical rows, what can the model do about their logits?
Forecast: guess whether a zero context prefers any token.
- (a) Zero context. Every . Softmax of all-equal . Why this step? A dot product with the zero vector is always — no drawer aligns with "no direction," so the model is maximally unsure. This is the degenerate case.
- (b) Identical rows. If , then for any context always. Why this step? Tied weights use the same drawer for scoring, so duplicate rows are permanently indistinguishable outputs — a warning that embeddings must actually differ (Mistake 1 in the parent).
Answer: (a) uniform each; (b) cat and dog get equal probability forever.
Verify: Uniform probabilities sum to (). ✓ Equal logits ⇒ equal softmax entries by definition. ✓
[!example] Example 6 — Cell F: limiting / saturating behaviour
Statement. Keep but let grow without bound. What does softmax do as ? As ?
Forecast: does the winner's probability approach or some smaller ceiling?
- Write softmax. (three others each contribute ). Why this step? Only relative logit gaps matter; the three zeros contribute total.
- Push . dwarfs , so , others . This is saturation. Why this step? Once one logit runs away, softmax becomes nearly one-hot — the model is essentially certain.
- Push . , so and the other three share it: each . Why this step? A token with a hugely negative logit is effectively removed from the race.
Answer: as .
Verify: At , (matches Example 5a). ✓ Limits computed symbolically. ✓
[!example] Example 7 — Cell G: real-world memory budget
Statement. A model has , (GPT-2 small). How many parameters do the input+output embeddings use without tying, and how many with tying (plus a length- output bias)? What fraction is saved?
Forecast: guess — roughly half, or more?
- Untied. Input , output , total . Why this step? Two separate matrices of the same shape.
- Tied. Share one matrix, add a bias of length : . Why this step? One matrix reused (transposed) for both directions — the parent's core claim.
- Saved fraction. .
Answer: of the embedding/projection block vanishes. Relevant to 6.2.04-Parameter-efficient-fine-tuning.
Verify: Numbers computed exactly in VERIFY; saving half, minus the tiny bias term. ✓
[!example] Example 8 — Cell H: exam twist (two gradient paths)
Statement. Suppose the loss gradient landing on as an input table is for dog's row, and the gradient landing on the same row through the output projection (already transposed back) is . With weight tying, what single update does row dog receive?
Forecast: does one path override the other, or do they combine?
- Recall the tying rule. . Why this step? One matrix used in two places ⇒ gradients add (chain rule over a shared variable) — see 4.4.03-Self-attention-mechanism for where came from.
- Add slot by slot. . Why this step? This "richer supervision" is exactly why tied models often generalise better, not just smaller.
Answer: row dog gets combined gradient .
Verify: Element-wise sum; sanity: middle slot cancels partly (). ✓