Concepts / Language Modeling Fundamentals

Language Modeling Fundamentals

Language modeling offers a way around this bottleneck.

  • Programming

Why Predicting the Next Word Is Hard to Label

Imagine you want to teach a model to predict the next word in a sentence. The most direct approach is to collect many examples and have people write down the correct answer for each one. That works, but it is slow, expensive, and hard to scale. Every new task seems to demand a new round of human annotation. Researchers call this the data labeling bottleneck, and it is one of the main reasons machine learning projects stall before they reach useful performance.

Language modeling offers a way around this bottleneck. The model does not need a person to write down the answers, because the raw text already contains them. In this article you will see how that works, follow one sentence step by step as it becomes training data, and look at why bigger models ask for bigger datasets.

Self-Supervision as Label Generation

Self-supervision is an approach that sidesteps much of the labeling cost. Instead of asking a human to attach an explicit label to each example, the model is set up so that it can work out the labels from the input data itself. The raw material already contains the answers. The training process simply needs to be arranged so that those answers are revealed to the model in the right way.

Definition

Self-supervision: a training setup in which the model infers its own labels from the input data, so that no separate human annotation step is required.

Splitting Input Into Context and Target

The key idea is to split each piece of input into two roles. One part serves as the context the model is allowed to look at. The other part serves as the target that the model must predict. Because the target comes from the input itself, no separate annotation step is required. The same text that feeds the model also tells you what the correct output should have been.

Language modeling fits this pattern naturally. Each input sequence provides two things at once: the tokens the model should predict, and the surrounding tokens the model can use as context when making that prediction. Nobody needs to write the answers down, because the sequence already contains them.

Raw sentenceunlabeled textSplit by rolecontext or targetContext tokensthe model may look at theseTraining pairinput and label from thesame textTarget tokenthe label the model mustpredictfeedsbecomes inputbecomes label
How is the input split so that the model's target label comes from the input itself? Raw text is divided into a context part the model sees and a target part it must predict.

Tracing One Sentence Into Samples

Before working through a new sentence, consider the example this material uses: the sentence 'I love street food.' is reported to produce six training samples from its own text. Each sample pairs some preceding material with the piece that comes next, which is what makes the sentence useful without any labels being added.

To see the mechanism without relying on that count, try a different sentence. Take 'The cat sat down.' and treat each word and the final period as a token. Starting from the first token, every position after the first gives a sample: the words before it form the context, and the current word is the target. Work through the trace below, then compare it with the diagram that follows.

Worked example

Turning 'The cat sat down.' into training samples

Treat the tokens as The, cat, sat, down, and the period. List every training sample the sentence yields under the rule that each position after the first is a target once.

  1. 1
    Identify the tokens

    The five tokens are The, cat, sat, down, and the period.

  2. 2
    Skip the first token as a target

    The first token has no preceding material to serve as context, so it does not produce a sample by itself.

  3. 3
    Build sample 1

    Context is 'The' and the target is 'cat'.

  4. 4
    Build sample 2

    Context is 'The cat' and the target is 'sat'.

  5. 5
    Build sample 3

    Context is 'The cat sat' and the target is 'down'.

  6. 6
    Build sample 4

    Context is 'The cat sat down' and the target is the period.

Result

Four training samples come from one unlabeled sentence, and no human wrote any of the targets. Each target was already sitting in the text, one position after its context.

Sample 1context: The | target: catSample 2context: The cat | target:satSample 3context: The cat sat |target: downSample 4context: The cat sat down |target: periodwindow growswindow growswindow grows
How does a sliding window over one sentence produce multiple (context, next-token) pairs, and which token is the label in each? In every step the window grows by one token, and the next token becomes the label.

Why Capacity Needs Data

A second question follows naturally once models can be trained without labels: how much data does a model need? The size of a model changes what counts as small or large. At the time of writing, a model with 100 billion parameters was considered large, and the source expects that such a figure may one day seem modest. Size is therefore relative, but the relationship between size and data is what matters here.

Larger models have more capacity to learn. Capacity is the room a model has to absorb patterns from its training material. A model with more capacity can represent more detail, but it can only use that room if it is given enough examples to fill it. Without enough data, the extra capacity has nothing to learn from, so it contributes little to performance. For that reason, larger models need more training data to reach their best results.

Large modelSmall datasetUnused capacityWasted computeLarge modelLarge datasetFilled capacityUseful performancetrained ontoo few examples to fillcompute spenttrained onexamples fillcapacity used
What happens to the large model's parameters and compute when the dataset is too small, compared with a right-sized dataset? On a small dataset, most of the capacity sits unused and the compute is largely wasted.

Training a large model on a small dataset wastes compute that a smaller model could have used well. The large model spends its training effort on a dataset that cannot exercise most of its capacity, while a smaller model matched to the same data would have put the same compute to good use.

Mistakes Beginners Make With Self-Supervision

  • Common mistake

    Believing that self-supervised training still needs a human to write each label

    What it looks like

    Assuming someone must annotate every next-word target in a corpus before training can start

    Why it fails

    The target comes from the input itself. Each input sequence supplies both the tokens to predict and the context used to predict them.

    Do this instead

    Identify which part of the raw text plays the context role and which part plays the target role.

  • Common mistake

    Assuming the first token of a sentence produces a sample

    What it looks like

    Counting 'The' as a target with no context before it

    Why it fails

    Each token after the first serves as a target once. The first token has no preceding words to act as context.

    Do this instead

    Start generating samples at the second position, using everything before it as context.

  • Common mistake

    Treating a bigger model as automatically better on any dataset

    What it looks like

    Choosing the largest available model for a small corpus and expecting strong results

    Why it fails

    Larger models have more capacity, and that capacity only helps when enough training data fills it. Otherwise the extra capacity contributes little and the compute is wasted.

    Do this instead

    Match model size to the amount of training data available, and add data when you scale the model up.

Checking Your Sample Count

What do you think happens?

How many training samples does the sentence 'The dog ran.' yield if each word and the final period are tokens?

Reveal answer
Answer:

Three samples

The tokens are The, dog, ran, and the period, giving four tokens. Every position after the first yields one sample: The to dog, The dog to ran, and The dog ran to the period. That is three samples.

EASY

Take the sentence 'Models learn patterns.' and treat each word and the final period as a token. List every (context, target) training sample it produces, and state which token is the label in each sample.

Hints
  • Count the tokens first, then skip the first token as a target.
  • Each sample's context is every token before the target.

Key Takeaways

  • Self-supervision lets a model infer its labels from the input itself, which helps overcome the data labeling bottleneck.
  • In language modeling, each input sequence supplies both the context and the target, so no separate annotation is needed.
  • One sentence yields several training samples, with each token after the first serving as a target once.
  • Larger models have more capacity and need more training data; training a large model on a small dataset wastes compute.

Related concepts

More concepts starting with L