Concepts / Building a Language Model from Scratch

Building a Language Model from Scratch

Keep the text and the model separate in your mind. The text is the material the model learns from during training. The network is the structure that ends up holding what it learned.

  • Programming

What a Large Language Model Is

Many people have used a phone keyboard that suggests the next word as they type. You type the start of a message, and the keyboard offers a few words that are likely to come next. That habit of guessing what comes after a given stretch of text is the same habit at the heart of a large language model. The difference is one of scale and ambition: an LLM is built to handle a far wider range of language, and it learns that skill from a very large body of text rather than from a small list of common phrases.

Definition

A large language model is a deep neural network designed to understand, generate, and respond to humanlike text.

Three parts of that definition do most of the work. The first is that an LLM is a neural network, which means it is a learned mathematical system made of many connected adjustable pieces, not a hand-written list of rules about grammar. The second is that it handles text, so its inputs and outputs are language. The third is that it is designed to do this in a humanlike way, meaning its output is meant to read naturally and respond to what was written.

The phrase deep neural network signals that the network has many layers of processing. That depth is what allows the model to pick up subtle patterns across a long passage instead of only the words that sit right next to each other.

Key point

Keep the text and the model separate in your mind. The text is the material the model learns from during training. The network is the structure that ends up holding what it learned.

Two Meanings of Large

The word large in the name carries two separate meanings. Both are about scale, but they measure different things. Once you can tell them apart, it becomes easier to reason about what makes a model powerful and what goes into building one.

Parameter countHow many adjustable weightsthe network hasTraining textThe immense body of textthe model learns fromNetwork structureWhere the weights sitinside the networkNext-word tasksEvery position in the textbecomes a predictionexerciselearning fills the weights
How the size of the model (its parameters) differs from the size of the training data, and how the two relate.

The first meaning is the number of parameters in the model. The second is the immense dataset used to train it. The parameters are the size of the structure; the dataset is the size of the experience that shapes it. A model with many parameters but very little text to learn from has a structure with little to hold, and a large body of text fed into a tiny network has little capacity to store what it reads.

Parameters as Adjustable Weights

Parameters are the adjustable weights inside the network. Picture a large mixing board in a recording studio with thousands of sliders. Each slider sets how much one signal contributes to the final sound. Training is the process of moving the sliders until the output matches what you want. In an LLM, the sliders are the parameters, and the thing you want the output to match is the next word in a sequence.

Next-Word Prediction as a Training Task

The task used to train these models is next-word prediction. The model is shown a sequence of words and asked to guess the word that comes next. Language is naturally sequential: each word arrives after the ones before it, and meaning builds up as the sequence unfolds. Next-word prediction takes advantage of that order, so learning to guess well forces the model to pick up context, grammatical structure, and the relationships between words.

Notice what this means in practice. The model does not need a teacher to label each sentence with its meaning. The next word in the text itself serves as the answer. This is why the text can be so plentiful: every position in a passage becomes a small prediction exercise.

Tracing One Prediction Into a Training Signal

A worked example makes the training loop concrete. Follow one prediction from start to finish. The sentence fragment below is an illustration of the process, not a sample of what the source discusses.

Worked example

One next-word prediction, start to finish

The model is shown the text 'The cat sat on the' and must predict the next word. The text actually continues with 'mat'.

  1. 1
    Supply the context

    The words 'The cat sat on the' are fed to the model. This is the stretch of text that comes before the position being predicted.

  2. 2
    Make a guess

    The network, whose behavior is set by its current parameter values, produces a guess for the next word. Suppose the guess is 'car'.

  3. 3
    Compare with the real word

    The guess is compared with the word that actually follows in the text, which is 'mat'. The text itself supplies the answer, so no human labeling is needed.

  4. 4
    Produce a training signal

    The gap between the guess 'car' and the actual word 'mat' is the training signal. It records that the current parameters led to a wrong guess.

  5. 5
    Adjust the parameters

    The signal feeds back into the parameters, nudging them so that a similar future prediction is more likely to land on the right word.

Result

One prediction turned into one small correction. The parameters changed slightly, and the model is now a little better placed to guess well from similar context.

Text contextThe words seen so farModel guessThe predicted next wordComparisonDoes the guess match?Training signalThe gap between guess andactual wordAdjust parametersWeights nudged toward theright answerBetter future guessesRepeat over many positionsActual next wordThe word the text reallycontainsfed into the modelcomparedcomparedmismatch producesfeeds backnudges
The text supplies context, the model guesses, the guess is compared with the word that actually follows, and the result nudges the parameters.

Repeating the Loop Across Text

A single correction is not the whole story. The source describes the process as a loop: read the diagram from left to right, and then repeat the cycle across an enormous amount of text. Each position in a passage yields its own prediction and its own signal, and each pass moves the parameters a little. Over many passes the parameters gradually settle into values that capture a great deal about language.

Position 1Context: The catPosition 2Context: The cat satPosition 3Context: The cat sat onPosition 4Context: The cat sat on theNext passageSame cycle on new textupdate parametersupdate parametersupdate parameterscontinue over the corpus
How the model moves through many text positions, predicting and correcting at each one, and how each pass changes the parameters.

Mistakes When Reading the Definition

  • Common mistake

    Treating the training text as the model itself

    What it looks like

    Saying the model 'is' the books and web pages it was trained on.

    Why it fails

    The text is the material the model learns from during training. The network is the structure that ends up holding what it learned.

    Do this instead

    Keep the text (training input) and the network (learned structure) as two separate things.

  • Common mistake

    Using large to mean only one thing

    What it looks like

    Assuming a large model simply means a lot of text was used, or simply means many parameters.

    Why it fails

    The word large refers both to the number of parameters and to the immense dataset used to train the model.

    Do this instead

    Name both meanings when describing an LLM, and note that they measure different things.

  • Common mistake

    Assuming the model is given labeled answers

    What it looks like

    Believing a person must annotate every sentence with its meaning before the model can learn.

    Why it fails

    In next-word prediction the next word in the text itself serves as the answer.

    Do this instead

    Think of each position in the text as a self-supplied prediction exercise.

  • Common mistake

    Dismissing next-word prediction as too simple to matter

Practice: Follow the Signal

What do you think happens?

In the worked example, the model guessed 'car' but the text says 'mat'. What happens to the parameters next?

  • The parameters stay exactly the same

  • The parameters are nudged so that a similar future guess is more likely to be right

  • The text is changed to match the model's guess

Reveal answer
Answer:

The parameters are nudged so that a similar future guess is more likely to be right

The gap between the guess and the actual next word is the training signal, and that signal feeds back into the parameters. The text is never edited to fit the guess.

EASY

Without looking back, explain in two or three sentences how the phrase 'next-word prediction' turns a single guess into a change inside the model. Name the three stages: the guess, the comparison with the real word, and the adjustment of parameters.

Hints
  • Start with what the model is given as context.
  • Then say what the guess is compared against.
  • End with what changes inside the network.

Putting the Pieces Together

An LLM is a deep neural network that works with humanlike text. Its size is described two ways: by the number of adjustable parameters and by the immense dataset it learns from. Its parameters are tuned by a simple task, predicting the next word, in which the text supplies its own answers. Each guess is compared with the real next word, and the resulting signal nudges the parameters. Repeated across a vast body of text, this loop shapes the model.

Key Takeaways

  • A large language model is a deep neural network designed to understand, generate, and respond to humanlike text.
  • The word large refers both to the number of parameters in the model and to the immense dataset used to train it.
  • Parameters are adjustable weights that are tuned during training so the model can predict the next word in a sequence.
  • Next-word prediction works as a training task because the text supplies the correct answer, and each guess yields a training signal that nudges the parameters.

Related concepts

More concepts starting with B