Labs · BERT, part by part

Embeddings summed

BERT's input is three rows added — the token's, its position's and its sentence's — then one LayerNorm before the first block. BERT learns all three tables; this lab uses the fixed sinusoidal position table and a zero sentence-A row, so its numbers match the ones the modules under Learn it compute.

xi=LN ⁣(Etoken[ti]+PEi+Esegment[si])x_i = \mathrm{LN}\!\left(E_{\text{token}}[t_i] + \mathrm{PE}_i + E_{\text{segment}}[s_i]\right)Opens at 6 tokens · d_model = 8 · a vocabulary of 10 · 2 segments

x_i is what the first block receives for token i. t_i is the i-th token, as its row number in the vocabulary, and s_i its segment (A or B: which of the two sentences it is in). E_token and E_segment are the token and segment tables, one row per word or segment; PE_i is position i's row of the sinusoidal position table. LN is LayerNorm. d_model is how many numbers each row holds.

In plain words: before BERT reads a sentence, each token becomes a row of numbers made by adding three rows — one for the word, one for its position, one for the sentence it is in — and then normalising each row (LayerNorm). BERT learns all three tables in training. Here the position rows are the fixed sinusoidal table and sentence A's row is zero, so the lab's numbers match the ones the modules under Learn it compute; the sum, the swap and the LayerNorm work the same way. Each row holds d_model = 8 numbers. The input starts with the [CLS] token, the extra token BERT puts first, and [SEP] marks where a sentence ends.

The token rows are random draws from the seed, not learned: the words on them carry no meaning here, and a new seed draws new rows. Segments A and B stand in for two sentences — in A sentence pair, “the cat” is sentence A and “sat” sentence B.

[CLS] the cat [SEP] sat [SEP] — sentence A, then sentence B: every token of B gets segment row B.

Token + position + segment, then LayerNorm

Reads /api/labs/embeddings — loading

Computing…

Embeddings summed — Labs — TransformerLab