Sub topic 1
What a Language Model Does: Predicting the Next Token
Prerequisites
You should be able to read basic Python, understand vectors and matrix multiplication, and recognize a probability distribution. You should also know that a neural network learns numerical parameters from examples.
Predicting the Next Token
A language model predicts the next token from the tokens before it. A token is a small piece of text, such as a word, part of a word, punctuation mark, or space. The model produces a probability for every token in its vocabulary.
For example, after The sky is, likely next tokens include blue, clear, and falling. The model does not choose a complete sentence at once. It chooses one token, adds it to the context, and predicts again.
Tokenization: Turning Text into Subword Tokens
Tokenization converts text into token IDs. Subword tokenization allows a fixed vocabulary to represent both common words and unfamiliar words. A common word may be one token, while a rare word may be split into several pieces.
The sequence
unhappinessmight become tokens similar to un, happi, and ness. The exact split depends on the tokenizer. Token IDs are integers used to look up vectors inside the model.
text = "Models predict tokens."
tokens = text.split()
token_ids = [12, 47, 891]
print(list(zip(tokens, token_ids)))Neural Network Basics: Layers, Weights, and Learning
A neural network is a sequence of layers. Each layer transforms numbers using weights, which are learned numerical parameters. During training, the network compares its prediction with the correct next token, measures the error, and adjusts its weights. This repeated adjustment is gradient descent.
Embeddings: Representing Tokens as Vectors
An embedding is a learned vector representing a token. Tokens with related uses often develop related vector patterns, although the vector does not have a simple human-readable meaning. The model adds information to these vectors as they pass through its layers.
Self-Attention: Letting Tokens Look at Each Other
Self-attention lets each token combine information from other tokens in the same sequence. For a decoder-only language model, a causal mask prevents a token from looking at future tokens. Each position can therefore use only itself and earlier positions.
The mechanism creates queries, keys, and values. Query-key similarity determines how much attention each position gives to another position. The weighted values become the updated representation.
Exercise
Why must a decoder-only language model use a causal mask during training?
Show answer
To prevent the prediction at one position from using tokens that occur later in the sequence.