Labs · BERT, part by part
One encoder block, assembled
Attention, add & norm, feed-forward, add & norm — the parts the labs before this one take apart, chained into the block BERT-base stacks twelve times (BERT-large, 24).
x is the block's input, one row per token. MHA is multi-head attention, LN is LayerNorm and FFN the feed-forward network. n₁ is each row after the first add and LayerNorm: what the feed-forward reads. out is the block's output. On Pre-LN the first add's sum is r₁. d_model is how many numbers each row holds; d_ff is how many the feed-forward's middle layer holds.
In plain words: one encoder block lets every token look at the others (MHA, multi-head attention), adds what it found back onto the token (the residual connection (shortcut)), normalises each row (LN, LayerNorm), then passes each token on its own through a small two-layer network (FFN, the feed-forward network) and adds and normalises again. The readouts compare each output row with its input row by their cosine (1 = the same direction, 0 = unrelated).
LayerNorm after each residual add, as BERT has it. With γ = 1 and β = 0 (LayerNorm as it starts) every output row sits at mean 0, variance 1.
One encoder block, sublayer by sublayer
Reads /api/labs/encoder-block — loading
Computing…
Attention and the feed-forward are the block's two sublayers. The weights here are random (seeded), not trained: the maps show the mechanism, not what BERT learned. With 2 heads, each head reads all 8 of a row's numbers and projects them down to d_k = d_model ÷ heads = 4 numbers of its own.