Training an LLM
An assistant like Gemini is built in stages. Pre-training on huge amounts of text makes a base model, fine-tuning teaches it to follow instructions, and human feedback shapes what a good answer looks like.
9 minFree
- Pre-train
- Fine-tune
- Human feedback
- Use
Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.
Training log
Nothing trained yet. Press Play.
Background: the idea in plain words
A normal program is written line by line by a person. Nobody could write the rules of language that way: there are far too many. So an LLM (a large language model, the kind of AI behind chat assistants) isn't programmed. It's trained: its billions of weights (the numbers inside the model) start random and are adjusted, step by step, until it predicts text well. That's the same loop you saw in How a neural network learns, at a vastly larger scale.
The basic exercise is simple. Take a piece of text, hide the next token (a small piece of a word), and ask the model to guess it. For example, show "the monsoon reaches Chen" and hide "nai". If the guess is poor, nudge the weights so the right token becomes a little more likely. Then do it again with the next piece of text.
Chat assistants are usually trained in three stages:
- Pre-training. The model learns to predict hidden tokens in a very large, filtered text dataset. For text generators, that means predicting the next token. This is by far the most expensive stage: Google's crash course says it can take months, with huge amounts of compute (computer power) and electricity. The result is a base model (also called a foundation model).
- Fine-tuning. Extra training on a much smaller set of examples for a specific job. Instruction tuning is fine-tuning on prompts (the text you give a model) paired with good responses, so the model learns to follow instructions.
- Learning from human feedback. People compare answers, a reward model learns their preferences, and reinforcement learning (learning from rewards) pushes the LLM towards answers people prefer. This is called RLHF (reinforcement learning from human feedback).
Each stage does a different job. Pre-training gives the model most of its knowledge. Fine-tuning teaches it to answer a request instead of just continuing the text. Human feedback shapes its tone and helpfulness.
One more thing to know before you start. When training ends, the weights are fixed. Chatting with the model does not change them.
Press Play to follow one model through all three. Then switch Try it to Same question: base model vs tuned model.
What to notice
- Knowledge mostly comes from pre-training. Facts, grammar, languages and coding patterns are all picked up while predicting the next token. Later stages mostly change behaviour, not knowledge.
- A base model completes text; it doesn't answer. Ask it a question and it may continue with another question. Fine-tuning teaches it what should follow a request.
- Fine-tuning is the same loop with different data. Same loss, same backpropagation. Only the examples change, and there are far fewer of them.
- RLHF learns from preferences, not correct answers. For "write a polite refund reply", there's no single right text. It's easier for people to say "A is better than B", and the reward model turns those choices into a score.
- Using a model doesn't train it. When you chat with an LLM, its weights stay the same. It "remembers" earlier messages only because they're sent again in the prompt. See Context windows.
Go deeperWhat you can change as a builder
Prompt engineering
Change the instructions and examples you send. No training at all, and the model's parameters don't change. Always try this first.
Fine-tuning
Train a foundation model further on examples of your task. The model keeps the same number of parameters but behaves more like your examples.
Distillation
Use a big model to label data, then train a smaller, cheaper model on it. Faster predictions, usually slightly less accurate.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Training | Adjusting a model's weights, step by step, until its predictions are good |
| Pre-training | The first and biggest stage: learning to predict the next token on a huge amount of text |
| Base model | The model after pre-training only. It continues text but has not learned to answer requests |
| Fine-tuning | Extra training on a much smaller set of examples for one job |
| Reward model | A second model that learns to predict which of two answers a person would prefer |
| RLHF | Reinforcement learning from human feedback: pushing the model towards answers people prefer |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.