Part X · Other kinds of models
Ch 36. Reading both ways
A blank in the middle
Every model so far has filled in a blank at the end of the text. Put the blank in the middle and GPT-2 has a problem: it only reads the words before the blank. In The cat sat on the ___ of the fridge, the clue comes after the gap.
Every model so far has filled in a blank at the end of the text. Put the blank in the middle and GPT-2 has a problem: it only reads the words before the blank. In The cat sat on the ___ of the fridge, the clue comes after the gap, where GPT-2 can’t look.
A different kind of transformer, the encoderencoder: A transformer that reads the whole text at once, words before and after, to understand it rather than continue it. Ch 36, reads the whole text at once. The best known is BERT, trained by hiding some words and guessing them from the words on both sides.[1] Here are real guesses from BERT and GPT-2:
A different kind of transformer, the encoderencoder: A transformer that reads the whole text at once, words before and after, to understand it rather than continue it. Ch 36, reads the whole text at once. The best known is BERT, trained by hiding 15% of the tokens in its training text and guessing each one from the words on both sides.[1][1] Here are real guesses from BERT and GPT-2:
For the fridge, GPT-2’s best guess is floor, a fine way to continue “The cat sat on the”, while BERT’s is top. GPT-2 sends the shopper for bread to the hospital; BERT, which can see “to buy some bread”, says market.
The sentence about the dog shows the gap at its widest. In “The ___ wagged its tail and barked”, GPT-2 has only “The” to go on, and its top guess is a new line, at 1.4%. BERT reads the rest and says dog, at 61%. Notice too that BERT’s tokenizer split wagged into “wa” and “##gged”, where ”##” marks a piece that continues the word before it.
The same blocks, a different mask
The difference is one setting in attention (Ch 9). In GPT-2, each token can only look at the tokens before it. In BERT, every token can look at every other token. Switch between the two:
The difference comes down to one setting in attention (Ch 9). In GPT-2, a decoder, each token can only look at itself and the tokens before it. In BERT, every token can look at every other token. Switch between the two:
Everything else is the recipe you already know: tokens, embeddings, attention, MLPs, and a stack of blocks. BERT base even has the same size blocks as GPT-2 small: 12 of them, each working on vectors of 768 numbers.[1][1][1]
The mask, written down
Attention scores every pair of tokens and turns each row of scores into weights with a softmax. The only difference between the two models is a mask added to the scores first:
For GPT-2, is 0 when token comes no later than token , and otherwise, so later tokens get a weight of exactly 0. For BERT, every is 0. Over the 10 tokens of the fridge sentence, GPT-2’s mask allows 55 of the 100 pairs and BERT’s all 100.
BERT pays for that in training. Only the 15% of tokens it hides give it something to predict, while
GPT-2 learns from every token. And of the hidden tokens, BERT swapped 80% for a [MASK] token, 10%
for a random token, and left 10% unchanged, so that it couldn’t count on seeing [MASK], which
never appears in real text.[1]
Good at understanding, not at writing
The price of looking both ways is that an encoder needs the whole text before it starts, so it can’t write one token at a time. Encoders are used for understanding instead: search, spam filters, and embeddings like the map in Ch 5.
The price of looking both ways is that an encoder needs the whole text before it starts. It can’t write a story one token at a time, because the tokens after the one it’s writing don’t exist yet. So encoders are used for understanding: search, sorting email into spam or not, and embeddings. The embedding model behind the map in Ch 5 is a small encoder of the same design as BERT.
The original transformer had both halves: an encoder to read a sentence in one language and a decoder to write it in another.[2][2][2] Many systems still pair them, like an encoder that finds the relevant documents (Ch 24) and a decoder that writes the answer.
Recipe card
Recap
- GPT-2 is a decoder: each token sees only earlier tokens, which is what lets it write left to right.
- An encoder like BERT lets every token see every other one, so it can use the words on both sides of a blank.
- Encoders are used for understanding: search, classification, and embeddings.
- Attention adds a mask before the softmax: for later tokens in GPT-2, nothing in BERT.
Next question: every one of GPT-2’s weights works on every token. Could a model be huge but use only part of itself for each token?
Sources
- Jacob Devlin et al. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL 2019.
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
Sources
- Jacob Devlin et al. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL 2019.
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
Sources
- Jacob Devlin et al. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL 2019.
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.