How LLMs Learn

In the previous tutorials, we learned about tokens, vectors, embeddings, transformers, attention, and parameters.

Now we can ask an important question:

How does an LLM actually learn these patterns?

Let’s understand the learning process in a simple way.


🏋️ Training Comes Before Inference

Before we use an LLM, it goes through training.

During training, the model learns patterns from a large amount of data.

Training Data

Training Process

Learned Parameters

Trained LLM

After that, during inference, we give input and receive output.


🗃️ What Data Does an LLM Learn From?

An LLM may be trained on large collections of text and other information.

For example:

  • Articles
  • Books
  • Documentation
  • Code
  • Conversations
  • Public datasets
  • Licensed datasets

The exact training data depends on the model.

The important idea is:

The model learns patterns from many examples.


📌 The Basic Learning Task

A common training task is token prediction.

The model receives text and tries to predict missing or next tokens.

For example:

The capital of India is

Model predicts next token

Delhi

If the model predicts the wrong token, the training process calculates how wrong the prediction was.


🧱 What is Loss?

Loss is a number that tells us how far the model’s prediction is from the expected answer.

Conceptually:

Prediction

Compare with expected answer

Loss

A higher loss means the model’s prediction was worse.

A lower loss means the model’s prediction was better.


⚙️ Updating Parameters

During training, the model adjusts its parameters to reduce loss.

Prediction

Loss

Parameter update

Try again

This happens again and again across a very large amount of data.

Over time, the model becomes better at predicting useful text patterns.


🏋️ Pre-training

Pre-training is the large first training stage.

During pre-training, the model learns broad patterns in language and information.

Large dataset

Pre-training

Base model

A base model may be good at continuing text, but it may not yet behave like a helpful assistant.


🏋️ Instruction Tuning

Instruction tuning helps the model respond better to instructions.

For example:

Explain this concept
Summarise this paragraph
Write code for this task
Return the answer as JSON

The goal is to make the model better at following what the user asks.


🏋️ Human Feedback

Some models are also improved using human feedback or preference feedback.

The idea is to compare different model responses and guide the model toward more useful behaviour.

This can help with:

  • Following instructions
  • Being clearer
  • Avoiding unsafe responses
  • Reducing unhelpful answers

It does not make the model perfect.


📌 Does the Model Memorise Everything?

No.

An LLM is not a normal database.

It does not usually store information like this:

Question

Stored answer

Instead, knowledge and behaviour are distributed across many learned parameters.

This is why a model can generate useful answers, but it can also forget, confuse, or hallucinate details.


🧩 Key Points

  • How does an LLM learn?

    It learns by processing training examples and updating parameters.

  • What is pre-training?

    The first large training stage where the model learns broad patterns.

  • What is instruction tuning?

    Additional training that helps the model follow instructions.

  • Does training happen when we ask a question?

    Normally, no.

    Asking a question is inference, not training.


🚀 What Comes Next?

Now we understand how an LLM learns before it is used.

Next, we will understand:

Context Window