Lesson 1 of 11 · Inside an LLM
What Is a Large Language Model?
Inside a large language model: tokens, embeddings, transformer layers, probabilities and parameters, and how to score a sentence with the chain rule in Python.
- 6 min read
- Intermediate
- Reviewed
- What a language model is, in precise terms
- The path a prompt takes through an LLM, step by step
- What parameters are and where they live
- How a model scores whole sentences, using probabilities and log-probabilities
A model of language
In the Generative AI Fundamentals course you saw that an LLM predicts the next token. This course goes a level deeper: how LLMs are built and trained, how their output is controlled, and how to judge them.
Scoring a sentence
Because a language model knows the probability of each next token, it can score a whole sentence by multiplying the probabilities of each token in turn. This is called the chain rule of probability.
P("the cat sat") = P("the") × P("cat" given "the") × P("sat" given "the cat") × P(end given "the cat sat")
The program below does this with a tiny table of next-word probabilities.
import math
# A tiny "language model": next-word probabilities learned from some text.
# (A real LLM computes these with billions of parameters instead of a table.)
next_word = {
"<start>": {"the": 0.6, "a": 0.4},
"the": {"cat": 0.5, "dog": 0.3, "sat": 0.2},
"a": {"cat": 0.4, "dog": 0.6},
"cat": {"sat": 0.7, "the": 0.3},
"dog": {"sat": 0.6, "the": 0.4},
"sat": {"<end>": 1.0},
}
def sentence_probability(words):
previous = "<start>"
probability = 1.0
for word in words + ["<end>"]:
p = next_word.get(previous, {}).get(word, 0.0001) # unseen pairs get a tiny chance
probability *= p
previous = word
return probability
for sentence in ["the cat sat", "a dog sat", "cat the sat"]:
p = sentence_probability(sentence.split())
print(f"{sentence:12} probability {p:.6f} log-probability {math.log(p):6.2f}")A fluent sentence gets a much higher probability than a jumbled one. Real LLMs do the same thing, with a vocabulary of over 100,000 tokens and a context of thousands of previous tokens instead of one.
Inside an LLM: the path of a prompt
| Step | What happens | What comes out |
|---|---|---|
| 1. Tokenize | Text is split into tokens, each with an ID number | [464, 3797, 3332] |
| 2. Embed | Each token ID is swapped for a long list of numbers (a vector) that represents it | One vector per token, often thousands of numbers long |
| 3. Transformer layers | Dozens of layers repeatedly update every vector, using attention to mix in information from other tokens | Vectors that now carry meaning in context |
| 4. Output layer | The final vector is turned into a score (logit) for every token in the vocabulary | 100,000+ scores |
| 5. Probabilities | The scores are converted to probabilities (softmax), and one token is chosen | The next token |
Parameters
Parameters are where everything the model learned is stored. There is no separate database of facts: knowledge, grammar and skills are all spread across these numbers. That is why a model can be very knowledgeable and still misremember a detail.
Common mistakes
Use the chain rule: the probability of a sentence is the product of the probabilities of each next token.
import math # The model's probability for each next token of "the dog sat", in order # (including the final end-of-text token) probabilities = [0.6, 0.3, 0.6, 1.0] # 1. Multiply them to get the probability of the whole sentence # 2. Print it with 3 decimal places # 3. Print its log-probability (math.log) with 2 decimal places
Where is an LLM's knowledge stored?
Summary
- A language model assigns probabilities to text; an LLM is a very large transformer-based one.
- A sentence's probability is the product of next-token probabilities (the chain rule); models use log-probabilities.
- A prompt goes through tokenizing, embedding, transformer layers with attention, an output layer and softmax.
- Parameters store everything the model learned; there is no separate fact database.
Next: pretraining, the giant first stage where all of this is learned.
Finished reading? Tick it off to track your progress.