Encoder Block
P10.transformer-architecture.01 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Multi-Head Attention, Positional Encoding
Where next?
Later in Transformer Architecture
This module unlocks
Go up a level
P10.transformer-architecture.01 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Multi-Head Attention, Positional Encoding
Later in Transformer Architecture
This module unlocks
Go up a level
Attention lets every token adjust its meaning by looking at its neighbours, but on its own that mixing is raw and unstable: repeat it a few dozen times and the original signal drifts away while gradients vanish. The Transformer's remedy is a disciplined wrapper — keep the original alongside every change through a residual connection, and re-normalise after every addition. Wrapped this way, attention plus a small feed-forward network become the encoder block, the self-contained unit that stacks into BERT and GPT. In this module you assemble that full block on the active corpus and inspect the tensor each sub-layer produces — the attention output, the residual sums, the norms, the feed-forward — with Q, K, V and the attention weights themselves on the attention modules.
Why it matters — This exact unit, stacked 12–96 times, IS the body of BERT and GPT — everything else is embellishment.
Reference: Vaswani et al. 2017
Reads /api/transformer/encoder/pipeline — loading
Computing…
Pre-LN vs Post-LN. This page shows Post-LN — LN(x + Sublayer(x)) — the original arrangement, which needs learning-rate warmup for stability. Modern models mostly use Pre-LN — x + Sublayer(LN(x)) — which trains stably without warmup because the residual path stays an identity. Why LayerNorm, not BatchNorm? LayerNorm normalises per token row, so it is independent of batch composition and sequence length; RMSNorm (LLaMA) drops the mean subtraction for speed.
Self-check
How many sub-layers does an encoder block contain?
Self-check
What is the shape of X for 3 tokens with d_model = 8?
Self-check
Which activation sits between the FFN's two linear layers?
Self-check
What gradient problem do residual connections solve?
Self-check
If output − input is near zero everywhere, what did the block do?
Self-check
Why does Post-LN apply LayerNorm after the residual addition?
Two labs take this page further, on numbers you can edit: the first sends a gradient back down a deep stack of layers and shows how much of it is left at each depth, with and without the residual path; the second assembles one block from its parts, Post-LN as BERT has it or Pre-LN.
Open in Labs: A gradient down twelve layers
Open in Labs: One encoder block, assembled
Practise this lesson's 6 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.
Implement the block's core mechanic yourself. The rung is a three-panel workspace: instructions, a code editor, and output + test results. Run checks the visible tests; Submit grades against hidden edge cases — the residual is added before layer-norm, the output has unit variance, and a constant vector does not blow up (the ε keeps the division stable).
Loading exercise…
A condensed walkthrough of the full pipeline for one sentence, from raw text to a single sentence embedding. Try to name each stage yourself, then reveal the summary.
(seq_len × d_model) matrix.d_model-dimensional sentence vector.You have traced a sentence from raw text to a fixed-width embedding — the same path every Transformer encoder walks.
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...