Concepts / Tokens and Special Tokens in Language Models

Tokens and Special Tokens in Language Models

In self-supervised language modeling, the target for each training example is the token that actually follows in the text. No human writes that target; the text already contains it.

  • Programming

A Common Misreading to Test First

Machine learning covers many kinds of models. Some detect objects in images, some forecast the weather, some predict stock prices, and some recommend products. Language models are one family among these. Yet language models became the center of the scaling effort that led to the ChatGPT moment. The reason is a property of how they can be trained: they can learn from self-supervision, while many other model types need supervision. This article explains what that property means, why it matters, and where people commonly misread it.

What do you think happens?

Does self-supervision belong under unsupervised learning, since both seem to work without a human-made answer key?

  • Yes, self-supervision is a type of unsupervised learning

  • No, the two differ in whether labels are present and where they come from

Reveal answer
Answer:

No, the two differ in whether labels are present and where they come from.

Both approaches can use raw, unlabeled text, so 'no answer key from a human' is not the dividing line. The source's key test is whether the learning setup relies on labels, and where those labels originate. Self-supervision uses labels that come from the text itself, while unsupervised learning, as the source describes it, does not need labels at all.

The Labeling Bottleneck in Supervised Learning

In supervised learning, you show a model examples of the behavior you want and attach a label to each one. Once trained, the model can be applied to new data. A fraud detection model, for instance, learns from transactions that are each tagged as fraud or not fraud, and then predicts the tag for transactions it has never seen. The success of many AI systems in the 2010s rested on this approach.

The weakness is cost. Obtaining labels is expensive and slow, and this is the data labeling bottleneck that limits how large a supervised dataset can grow. The bottleneck gets worse as tasks become harder. Labeling everyday objects is something most people can do without training, but labeling whether a medical scan shows signs of cancer demands expert time and is far more costly.

Labels Inferred From the Input Itself

Self-supervision removes the need for an external answer key. Instead, the model infers the labels from the input data itself. Language modeling is a clean case of this. Every input sequence contains two things at once: the tokens the model should predict, and the surrounding tokens that serve as context for predicting them. Because the text supplies both, a large body of ordinary writing can be turned into training material without anyone annotating it.

Key point

In self-supervised language modeling, the target for each training example is the token that actually follows in the text. No human writes that target; the text already contains it.

Tracing One Sentence Into Training Samples

Take a new sentence to see the mechanism: Cats chase mice. The sentence is first wrapped in a beginning marker and an end marker. Counting the ordinary tokens gives four, which is consistent with treating the final period as its own token alongside Cats, chase, and mice. Each position in the sequence then becomes one training sample. The learner sees the context to the left of a position and must predict the token that comes next.

Worked example

Cats chase mice, sample by sample

Turn the sentence Cats chase mice. into next-token training samples, with <BOS> at the start and <EOS> at the end.

  1. 1
    Wrap the sequence

    The sequence becomes <BOS> Cats chase mice . <EOS>, with the beginning marker before the first token and the end marker after the last.

  2. 2
    Sample 1

    Context: <BOS>. Target: Cats. The model sees only the start marker and must predict the first word.

  3. 3
    Sample 2

    Context: <BOS> Cats. Target: chase. The context has grown by one token.

  4. 4
    Sample 3

    Context: <BOS> Cats chase. Target: mice.

  5. 5
    Sample 4

    Context: <BOS> Cats chase mice. Target: the period.

  6. 6
    Sample 5

    Context: <BOS> Cats chase mice . Target: <EOS>. The end marker is itself a target, which teaches the model when to stop.

Result

One sentence produced five training samples. No one wrote a label for any of them; each target was the token that actually followed in the text.

Read the diagram from left to right. Each step keeps everything that came before and adds one more token to the context. The token being predicted at one step becomes part of the context at the next. A longer passage of text yields many more samples in the same way, which is why this approach can scale to very large collections of writing.

Special Tokens That Mark Sequence Boundaries

The markers at the edges of each sequence are special tokens. The beginning marker, written <BOS>, shows where a sequence starts. The end marker, written <EOS>, shows where it stops. Each marker is typically handled by the model as one token, not as a set of characters. These markers matter because a language model usually works with many sequences, and it needs a clear way to tell where one ends and the next begins.

The two markers do different jobs. <BOS> is the context for the very first prediction, so the model always has a fixed starting point. <EOS> is a target in the final sample, so the model is trained to produce it when a sequence should end. The source puts it directly: the end marker teaches the model when to stop.

Comparing Supervised, Self-Supervised, and Unsupervised Learning

Now return to the belief from the start. The distinction is not whether a model is trained on raw data, since both self-supervised and unsupervised approaches can use it. The distinction is whether the learning setup relies on labels, and where those labels come from. The comparison below applies that single test to the three approaches.

Supervised learning has labels supplied by people, which is where the bottleneck comes from. Self-supervised learning also has labels, but they are derived from the text, so no human annotation step is needed. Unsupervised learning, as the source describes it, needs no labels at all. Because self-supervision does have labels, it belongs with supervised-style training signals on this test, even though it avoids the human answer key. That is why filing it under unsupervised learning is the common misreading.

Mistakes Learners Make With These Ideas

  • Common mistake

    Treating self-supervision as unsupervised learning because no human answer key is involved

    Why it fails

    The test is whether labels are present and where they come from, not whether a human wrote them. Self-supervised training does have labels; they are inferred from the input.

    Do this instead

    Ask two questions: does each example carry a label, and where does that label come from? Self-supervision answers yes and the input text.

  • Common mistake

    Thinking one sentence yields one training example

    What it looks like

    Cats chase mice. yields five samples, one for each position where a next token is predicted, including the end marker.

    Do this instead

    Count the positions where a next token must be predicted. Each one is a separate (context, target) pair.

  • Common mistake

    Assuming <BOS> and <EOS> are just decoration added to the text

    Why it fails

    The start marker supplies a fixed starting context, and the end marker is a training target that teaches the model when to stop.

    Do this instead

    Treat each marker as a token that participates in both context and target, not as formatting.

  • Common mistake

    Believing the labels in self-supervision must be written by someone for each sentence

Applying the Pattern to a New Sentence

The same procedure works for any sentence. Consider Dogs bark loudly. Wrap it as <BOS> Dogs bark loudly . <EOS>. The ordinary tokens are Dogs, bark, loudly, and the period, so the sequence yields five samples: the first predicts Dogs from <BOS>, the second predicts bark, the third predicts loudly, the fourth predicts the period, and the fifth predicts <EOS>. Every target came straight from the text, and nothing needed labeling.

In the real world

Because the same procedure applies to any text, a large collection of ordinary writing can be converted into training samples automatically. The scale of the data is limited by how much text exists, not by how many people are available to tag it.

Practice Tracing a Sentence

EASY

Take the sentence Birds sing. Wrap it with <BOS> and <EOS>, then list every (context, next-token target) pair the sentence produces. How many training samples result, and what is the target of the last one?

Hints
  • Count the ordinary tokens first. Treat the period as its own token.
  • Each position where a next token must be predicted produces one sample, and the end marker counts as a target.
  • The last target should be the end marker, and its context should be the whole sentence including the period.

Key Takeaways

  • Self-supervision avoids the data labeling bottleneck by inferring labels from the input text, so no human has to supply an answer key for each example.
  • In language modeling, one sentence yields several training samples: each position supplies a context of preceding tokens and a next-token target taken from the text itself.
  • The key test for distinguishing the three approaches is whether labels are present and where they come from: supervised uses human-supplied labels, self-supervised derives labels from the text, and unsupervised needs no labels.
  • The special tokens <BOS> and <EOS> mark where a sequence starts and ends; <BOS> gives the first prediction its context, and <EOS> is a target that teaches the model when to stop.

Related concepts

More concepts starting with T