Concepts / Training Objectives for Language Models

Training Objectives for Language Models

A masked language model is trained to predict missing tokens anywhere in a sequence, using context from both before and after each gap, as in filling in the blank.

  • Programming

Two Tasks, Two Views of Context

Imagine two prediction tasks built on the same kind of text. In the first, a model sees the sentence 'My favorite __ is blue' with one word hidden and must supply the missing word. In the second, a different model sees only the start of a sentence, 'My favorite color is', and must say what word should come next. Both tasks ask a model to guess a token from the language around it, yet the information each model is allowed to use is not the same. That difference in what the model can see is the central idea behind the two main types of language models.

What do you think happens?

In the sentence 'My favorite __ is blue', which words does the model have available to help it fill the gap?

  • Only the words before the gap, 'My favorite'

  • Only the words after the gap, 'is blue'

  • Both the words before and after the gap

Reveal answer
Answer:

Both the words before and after the gap

The words 'is' and 'blue' that follow the gap carry as much information as 'My favorite' that precedes it. A masked model can use the whole neighborhood of the gap.

The rest of this article traces what each model can see at each step of its training objective. Once you can say which tokens are visible and which are hidden, the differences in applications follow naturally.

Tracing a Masked Prediction

A masked language model is trained to predict missing tokens anywhere in a sequence, using context from both before and after each gap, as in filling in the blank. The training objective is not about continuing a sentence. The model is handed a sequence with some tokens hidden and must recover what was hidden.

Myvisible, before the gapfavoritevisible, before the gap__masked, to predictisvisible, after the gapbluevisible, after the gapleft contextleft contextright contextright context
Which tokens are visible to the model when it predicts a masked token in the middle of a sentence, and how do the left and right sides both feed in?

In the diagram, the masked position is the only one the model must fill. Every other token is available as evidence, and the tokens on both sides of the gap feed into the same prediction. Reading the diagram from the gap outward, the left and right neighbors are both inputs to one decision.

Worked Example: Filling the Blank

Worked example

Predicting the hidden word in 'My favorite __ is blue'

A masked model is shown 'My favorite __ is blue' and must supply the hidden word. Which context does it use, and why does the right side matter?

  1. 1
    Identify the masked position

    The gap sits between 'favorite' and 'is'. This is the only position the model must fill.

  2. 2
    Collect the left context

    The words 'My favorite' come before the gap and give the model the start of the phrase.

  3. 3
    Collect the right context

    The words 'is blue' come after the gap. The model is allowed to use them, and they constrain what the missing word can be.

  4. 4
    Combine both sides

    The model uses the whole neighborhood of the gap together to decide what belongs there, rather than reading only from the left.

Result

The model predicts a word that fits both 'My favorite' on the left and 'is blue' on the right. The right side is part of the evidence, not something it has to ignore.

Where Masked Models Are Used

A well-known example of the masked family is BERT, which stands for bidirectional encoder representations from transformers. Masked language models were commonly used for nongenerative tasks such as sentiment analysis and text classification. They are also useful when a task depends on the overall context of a passage.

In the real world

Code debugging is a case where the whole neighborhood matters. To find an error, a model must read the code before the error and the code after it, because the cause of a problem can depend on what surrounds it on either side.

Tracing an Autoregressive Prediction

An autoregressive language model is trained to predict the next token using only the preceding tokens. Here the model is restricted to the left side of the position it is predicting. Given 'My favorite color is __', it must produce the word that continues the sentence, and it may not look ahead to see what the sentence eventually says.

Mypredict: favoriteMy favoritepredict: colorMy favorite colorpredict: isMy favorite color ispredict: blueappend predictionappend predictionappend prediction
At each step, which tokens are visible and which are hidden, and how does the window of visible context grow as the sequence advances?

Read the steps from top to bottom. At each step the visible context is everything written so far, and the position being predicted is the one after it. Nothing to the right of the unknown position is available, so the window of visible context only ever extends forward as the sequence grows.

Feeding Predictions Back In

Once a prediction is made, it can be appended to the sequence, and the model can then predict the token after that. This is the mechanism behind the ability to continually generate one token after another. Each new token becomes part of the context for the next prediction.

Visible tokens sofarstarting contextPredict next tokenuses preceding tokens onlyAppend tokenbecomes new contextContinue generatingrepeat the cycleinputpredicted tokenfeed back insequence grows
How does a generated token get appended and fed back in to produce the next token?

The source describes this feedback loop as the reason autoregressive models suit generation: the model's job mirrors the way a sentence is written from left to right. The source does not describe how the next token is chosen from the model's predictions, so this article does not go into that step.

Comparing the Two Objectives

The two model types differ in two linked ways: which tokens they may condition on, and what their training objective asks them to produce. A masked model fills a gap using context on both sides. An autoregressive model extends a sequence using only what came before.

Fill the gapobjectivePredict next tokenobjectiveLeft and rightcontextcontext usedPreceding tokens onlycontext usedClassification,sentiment analysistypical applicationsText generationtypical applications
How do the two training objectives differ in what is hidden, what context is used, and which applications each one suits?
AspectMasked language modelAutoregressive language model
Training objectivePredict missing tokens anywhere in a sequencePredict the next token
Context usedTokens both before and after each gapOnly the preceding tokens
Typical applicationsNongenerative tasks such as sentiment analysis and text classificationText generation
ExampleBERTContinuing 'My favorite color is' with the next word

Both model types guess tokens from surrounding language, but they are allowed to see different parts of it.

The source states that autoregressive models are the models of choice for text generation, and for this reason they are far more popular than masked language models. Masked models remain the natural fit when the task is to understand or label a passage using its full context.

Generative AI and Language Models

A language model works from a fixed, finite vocabulary, yet it can combine those vocabulary items into an unlimited number of possible outputs. A model that can produce open-ended outputs in this way is called generative, and this is the origin of the term generative AI. Language models are therefore a central example of generative AI, because their outputs are not limited to a fixed list of answers.

Common Misreadings of Training Objectives

  • Common mistake

    Thinking a masked model only uses the words before the gap

    What it looks like

    Reading 'My favorite __ is blue' as if only 'My favorite' were available

    Why it fails

    The words 'is' and 'blue' after the gap carry as much information as the words before it, and the masked objective allows the model to use them.

    Do this instead

    Treat both sides of the gap as evidence for the prediction.

  • Common mistake

    Assuming an autoregressive model can look ahead to the end of the sentence

    What it looks like

    Expecting the model predicting after 'My favorite color is' to know that the sentence ends with 'blue'

    Why it fails

    The autoregressive objective restricts the model to preceding tokens, so it may not look ahead to see what the sentence eventually says.

    Do this instead

    Only the tokens already written are visible at each step.

  • Common mistake

    Treating masked and autoregressive models as interchangeable for any task

    Why it fails

    The two objectives differ in what context is allowed, so each suits different applications.

    Do this instead

    Match the objective to the task: masked models for nongenerative tasks such as classification, autoregressive models for text generation.

  • Common mistake

    Equating generative AI with any model that handles text

    Why it fails

    The source describes generative AI through the ability to produce open-ended outputs from a fixed vocabulary, which is what makes language models generative.

    Do this instead

    Ask whether the model's outputs are open-ended, not merely whether it processes language.

Trace the Visible Context

MEDIUM

For each prompt below, decide which model type applies and list exactly which tokens the model may use. (a) A model sees 'The movie was __ and the ending was wonderful' and must fill the gap. (b) A model sees 'The movie was very' and must produce the next word.

Hints
  • For part (a), ask whether the words after the gap are allowed to be used.
  • For part (b), ask whether the model may look at anything to the right of the position it is predicting.
  • Check your answer against the objectives: masked models use both sides, autoregressive models use only preceding tokens.

Key Takeaways

  • A masked language model is trained to predict missing tokens anywhere in a sequence, using context from both before and after each gap.
  • An autoregressive language model is trained to predict the next token using only the preceding tokens, so it reads and writes in the same left-to-right direction.
  • Masked models are commonly used for nongenerative tasks such as sentiment analysis and text classification, while autoregressive models are the models of choice for text generation.
  • Language models are generative because a fixed, finite vocabulary can produce an unlimited number of open-ended outputs, which is why they are described as generative AI.

Related concepts

More concepts starting with T