Transformer Tokens & Embeddings
Transformers read and write sequences of tokens. But what is a token sequence?
How can transformer models input and output text, images, video, MIDI and audio?
Tokenizer Example
To train or use a transformer, we need to
- Encode input data to a sequence of tokens
- Decode output tokens back to the input data format
Here's an extremely simple text tokenizer:
This simple tokenizer has limited value. It can only encode and decode the 68 characters in its vocabulary:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.!?"'0123456789
What is a token?
Assuming a vocabulary size of , a token is just an integer value that satisfies:
Remember NotaGen from the Symbolic Music Generation assignment? NotaGen generates abc notation, which is just text. Now we know one way that models can produce text.
Byte Pair Encoding
Text-based LLMs typically use Byte Pair Encoding (BPE), which tokenizes groups of characters.
- tiktoken (created at OpenAI)
- SentencePiece (created at Google)
A robust BPE text tokenizer will help the model see common subword character sequences. In English "izer" is a common subword, so a BPE tokenizer might split "tokenizer" into tokens like token, and izer (instead of e.g. toke and nizer). This helps models generalize and "understand" grammar.
Interactive Tokenizers
Music Tokenization
MIDI Tokenization
Audio Tokenization
- https://github.com/facebookresearch/encodec (Meta/Facebook)
- https://github.com/descriptinc/descript-audio-codec
- https://www.youtube.com/watch?v=mV7bhf6b2Hs (YouTube video explains how these work)
Embeddings
Once we have tokens, the transformer will use them to look up column vectors from an embedding table. Each token index corresponds to a column vector that represents that token numerically.
During the training process, the values in the embedding table are gradually adjusted. If we train successfully, these values will encode the semantic significance of the tokens in a high-dimensional space.
Resources
- 3Blue1Brown: But what is a GPT? Visual intro to transformers
- Andrej Karpathy (ML Researcher from Tesla, OpenAI)