SevyDevy

Module 2 · How a language model thinks · Lesson 9 of 32

How language models are trained

11 minFree GenAI course · BeginnerLesson 2.5By Saket Bhatnagar

Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.

In short

  • Pretraining: the model reads a giant library and learns language by guessing the next token, trillions of times.
  • Fine-tuning: it practises on example conversations, so it learns to answer like a helpful assistant.
  • Feedback: people (and checkers) rate its answers, and it is nudged towards the ones rated better.
Soon

Video lesson · coming soon

How AI models are trained, in 3 minutes

Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.

  1. 01 Knowing food is not serving food
  2. 02 From food lover to head chef
  3. 03 Pretraining, fine-tuning, feedback
  4. 04 The base model answers with more questions
  5. 05 Step through the training
  6. 06 Trained to sound right
  7. 07 Try it, then meet temperature

Rohan grew up in Indore eating his mother’s poha every morning. He read every cookbook in the house and watched a thousand cooking videos. He knows food deeply. But on his first day at a restaurant, a customer says “one plate poha, less oil, extra sev” and Rohan freezes. Knowing food and serving a customer are two different skills.

A language model has the same problem, and it is solved the same way. First a lot of reading, then practice with real orders, then feedback from the people who taste the food. Let’s walk through all three.

Why does a model need three stages?

In the last few lessons you saw how a model works: it cuts text into tokens, predicts the next one, and uses attention to decide which words matter. But a brand-new model knows nothing. Its billions of settings start as random numbers, so its first guesses are gibberish.

Training is the job of setting those numbers. It happens in three stages, and each one teaches something different. The first teaches language and knowledge. The second teaches manners and format. The third teaches judgement: which of two answers people actually prefer.

The three stages of training a language model

One long, costly reading stage builds the base model; two shorter stages turn it into a helpful assistant.

Diagram connections:

  • Huge text library to Pretraining (read)
  • Pretraining to Base model (produces)
  • Base model to Fine-tuning (practise)
  • Fine-tuning to Feedback (rate)
  • Feedback to Helpful assistant (release)
  • Huge text library: Web pages, books, code and articles, cleaned and filtered.
  • Pretraining: Guess the next token, check, nudge. Trillions of times.
  • Base model: Knows language well but only continues text.
  • Fine-tuning: Practises on example questions with good answers.
  • Feedback: People compare answers; the model shifts towards the better…
  • Helpful assistant: The chat model you talk to.

Stage 1: pretraining reads a giant library

Pretraining starts with text. A lot of it: public web pages, books, encyclopedias, news, forums and computer code. Teams clean it first, removing spam, copies and harmful pages. For today’s large models, the pile comes to trillions of tokens. Reading one book a day, a person would need many thousands of lifetimes to get through it.

The task is the one you already know from the next-word lesson. Hide the next token, let the model guess, check the real answer, and nudge the numbers so the right token becomes a little more likely. It is the guess, check and nudge loop from module 1, repeated trillions of times.

Nobody writes labels for this. The text is its own answer key, because the next word is always sitting right there. That is why it can scale to so much data. Along the way, the model picks up grammar, facts, styles, a few dozen languages, and even some reasoning, because all of those help it guess better.

This stage is huge. It runs for weeks or months on thousands of specialised chips, and for the biggest models the bill can run to hundreds of crores of rupees or more. Only a handful of companies and labs do it at the top end.

Here is the surprise. Ask a base model “What is the capital of Rajasthan?” and it may reply with more quiz questions: “What is the capital of Gujarat? What is the capital of Punjab?” To it, your question looks like the start of a quiz sheet, and continuing the sheet is the most likely next text. It knows the answer is Jaipur, but it does not know it is supposed to answer you.

Stage 2: fine-tuning teaches it to be an assistant

Now the apprenticeship. The team gathers example conversations: a question or task, followed by a good answer. Some are written by trained people, some are checked by them, and these days many are drafted by other models and then filtered. The count is far smaller than pretraining, roughly tens of thousands to a few million examples.

The model trains on these the same way, guessing the next token, but now the text is always “question, then helpful answer”. After enough of them, the pattern sinks in. When it sees a question, the most likely next text becomes an answer, in a polite and clear format. This kind is often called instruction tuning or supervised fine-tuning.

Fine-tuning mostly teaches habits, not new knowledge. It shapes tone, format, when to use bullet points, when to ask a follow-up question, and when to refuse something harmful. The knowledge still comes from the library read in stage one.

Stage 3: feedback teaches it what good looks like

Writing a perfect answer is hard. Picking the better of two answers is much simpler. You may not be able to cook a perfect biryani, but you can taste two plates and say which one wins. Stage three uses exactly that.

The model writes two or more answers to the same question. People compare them and mark the one they prefer: more correct, clearer, kinder, safer. Their choices train a separate scorer that learns to predict what people like. Then the main model practises against that scorer, and it is nudged towards answers that score well.

There are newer variations. Some methods skip the separate scorer and learn straight from the pairs of preferred and rejected answers. Some use another model as the judge, guided by a written list of rules. And for maths and code, an answer can often be checked automatically: the sum is right or wrong, the code passes its tests or fails. Training on those checks is a big reason recent models reason better, and it is how many “thinking” models are made.

Try it yourselfWalk a model through its trainingStep through the journey from raw text to a chat assistant. Watch what the model says at each stage.

What training means for you as a user

The three stages side by side
StageLearns fromRough sizeWhat it gains
PretrainingRaw text from a huge libraryTrillions of tokens; weeks to monthsLanguage, facts, styles, some reasoning
Fine-tuningExample questions with good answersThousands to millions of examplesAnswers questions, follows instructions, clean format
FeedbackPeople or checkers rating answersMany thousands of comparisonsPrefers helpful, honest, safe answers

Three things follow from this. First, the model’s knowledge stops at the date its library was collected, called the knowledge cutoff. Anything after that, it has never read. Second, training is finished before you ever type a word. Your chat does not teach the model anything; it only uses what is already in its numbers.

Third, the model learned to sound like a good answer. Most of the time, sounding right and being right go together. Sometimes they do not, and that gap is where made-up answers come from. Hold that thought until the lesson on hallucinations.

Check yourself

Check yourself

A base model, fresh from pretraining, is asked “Write a leave note for tomorrow.” What is it most likely to do?

Pick an answer to see why it is right or wrong.

Check yourself

Which stage teaches the model most of what it knows about the world?

Pick an answer to see why it is right or wrong.

Check yourself

Why does stage three ask people to compare two answers instead of writing perfect ones?

Pick an answer to see why it is right or wrong.

Remember these three

  1. Pretraining reads a huge library by next-token prediction and builds a base model that knows a lot but only continues text.
  2. Fine-tuning on example conversations turns it into an assistant; feedback on pairs of answers teaches it what people prefer.
  3. Training ends before you chat, so the model’s knowledge is frozen at a cutoff and it learned to sound right, not to be right.

Questions people ask

How are large language models trained?
In three broad stages. Pretraining teaches language by predicting the next token across trillions of tokens of text. Fine-tuning on example conversations teaches the model to follow instructions, and feedback from people or automatic checkers teaches it which answers are better.
What is the difference between pretraining and fine-tuning?
Pretraining is the huge, costly first stage on raw text, where the model gains general knowledge. Fine-tuning is a much smaller stage on curated examples, used to shape behaviour such as tone, format and following instructions.
What is RLHF in simple words?
RLHF means reinforcement learning from human feedback. People compare answers and pick the better one, a scorer learns their taste, and the model is rewarded for answers the scorer rates highly.
Does AI learn from my chats?
Not while you chat. Using a model does not change its numbers. Some companies may use saved conversations to train future versions, depending on their policy and your settings, so check the privacy settings of any app you use.