What it is
A transformer updates every token using the tokens around it, all at once
Before 2017, the standard model for text was a recurrent neural network, which reads one token at a time and carries a running summary forward. For translation, an encoder network read the whole source sentence into one fixed-length vector, and a decoder network wrote the translation from that vector. Bahdanau et al. (2014) argued that "the use of a fixed-length vector is a bottleneck", and added attention, a way for the decoder to look back at every source word and weight the ones it needs for the word it's about to write.
The transformer kept attention and dropped the recurrent part. In the paper's words, it's "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely". With no token-by-token reading, every position can be computed at the same time, which suits GPUs. The paper's base model trained in 12 hours on 8 GPUs.
The input doesn't have to be text. Anything you can cut into a sequence of pieces and turn into vectors works.
- Text goes in as tokens, which are words or pieces of words.
- An image goes in as square patches, which is the idea in An Image is Worth 16x16 Words (Dosovitskiy et al., 2020), the paper that introduced the Vision Transformer.
- Audio goes in as short slices of a spectrogram, which is what Whisper (Radford et al., 2022) does.
- The compressed image that a diffusion model denoises goes in as patches, which is what diffusion transformers (Peebles and Xie, 2022) do.
Keep reading
The rest of this article is free with an account
Create a free account to read the remaining 6 sections. It takes about twenty seconds and there's nothing to pay. The same account opens the slide decks and CV reviews.
- 02Attention lets each token pull in information from the tokens it needs
- 03A transformer layer is attention plus a feed-forward network, with a shortcut around each
- 04Attention's cost grows with the square of the input length
- 05Pick a transformer for almost anything with enough data, and know the neighbors
- 06See attention in code
- and 1 more
