Search

Search papers and lessons

Scheduled Sampling

Level 3 · 10 min read

Scheduled sampling trains a sequence model on a mix of true previous tokens and its own predictions, moving from the first to the second over time, to close the gap that causes exposure bias.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

Scheduled sampling is a training method for sequence models, such as recurrent networks that produce text one token at a time. During training, the model is fed the true previous token with some probability and its own previous prediction with the remaining probability, and that mix shifts from mostly true tokens at the start of training to mostly its own predictions at the end. It exists to close the gap between training with teacher forcing and generating on its own, which is the gap behind Exposure Bias.

Builds on: Teacher Forcing, Autoregressive Model · Leads to: Exposure Bias

Where it's used

Image captioning. A captioning model reads image features and writes a sentence one word at a time. Early in training the model needs the true words to learn the basic structure of a caption, so teacher forcing works well. Later, the model is trained with some of its own words in the input, so that a single wrong word does not send the rest of the caption off track. The original paper by Bengio and colleagues (2015) tested this on image captioning and on speech recognition tasks.

Speech recognition and translation. Sequence-to-sequence systems that turn audio or a sentence into text have the same problem: at test time each output word depends on earlier outputs that may be wrong. Scheduled sampling was proposed for this setting, in encoder-decoder models that generate output step by step.

Chatbots and language models with long answers. A model trained only with teacher forcing may write a good first sentence and then drift, because its later steps were never trained on its own imperfect continuations. Mixing in its own outputs during training is one of the standard remedies, although modern transformer models usually rely on other methods, such as more data and post-training.

How it works

Take a tiny sequence model that predicts the next word of the phrase "the cat sat". Its words are tokens, each one a unit of text. The true targets are "cat", then "sat", then an end-of-sequence token. The rule for each step is: with probability ϵ\epsilon, feed the true previous word; otherwise feed the model's own most likely word. For this walkthrough, hold ϵ=0.8\epsilon = 0.8 fixed for the whole sentence. (If ϵ\epsilon were 1, every step would feed the true word, which is pure teacher forcing. Step 5 explains how ϵ\epsilon changes over training.)

  1. Start the sequence. The input is the start token. The model predicts a distribution over words (a softmax over the vocabulary turns its scores into probabilities), and the most likely word is "the". Check: the prediction is compared with the target "the", so there is a loss to compute.
  2. Choose the input for step 2. Draw a random number uu between 0 and 1. Suppose ϵ=0.8\epsilon = 0.8 and u=0.5u = 0.5. Since u<ϵu < \epsilon, the model is fed the true previous word, "the". Check: a value of uu above 0.8 would have fed the model its own prediction instead.
  3. Predict step 2. With input "the", the model predicts "cat" with high probability. The target is "cat", so the loss is small. Check: the loss at each step is computed with the true target, whichever input was used.
  4. Choose the input for step 3. Draw u=0.9u = 0.9. Now u>ϵu > \epsilon, so the model is fed its own previous prediction, "cat" (it happens to be right). Suppose instead it had predicted "dog". Then the input at step 3 is "dog", and the target is still "sat".
  5. Lower ϵ\epsilon over training. Across many training steps (each a pass of the update on a batch), ϵ\epsilon is lowered, for example to 0.2. Starting it at 1 gives pure teacher forcing at the beginning. Then most inputs are the model's own predictions, and the model learns to recover from its own errors. Check: when ϵ=0\epsilon = 0, the model sees only its own predictions, which is the situation at generation time.
flowchart LR
  T[True previous token] -->|probability epsilon| M[Model step]
  P[Model's own previous prediction] -->|probability 1 minus epsilon| M
  M --> L[Loss against the true next token]
  L --> D[Epsilon decreases over training]

The math

Definition: Scheduled sampling
Let y1,…,yTy_1, \dots, y_T be the true target sequence. At step tt, let y^t−1\hat{y}_{t-1} be the model's prediction from the previous step. The input to step tt is the true token yt−1y_{t-1} with probability ϵi\epsilon_i, and y^t−1\hat{y}_{t-1} with probability 1−ϵi1 - \epsilon_i, where ii is the training step. The loss is the usual next-token cross-entropy against yty_t.

Step 2 in symbols: the random choice of input

Draw ut∼Uniform(0,1)u_t \sim \mathrm{Uniform}(0, 1) independently at each step. The input is

xt={yt−1if ut<ϵiy^t−1otherwisex_t = \begin{cases} y_{t-1} & \text{if } u_t < \epsilon_i \\ \hat{y}_{t-1} & \text{otherwise} \end{cases}

With ϵi=0.8\epsilon_i = 0.8, the probability that the true token is used is P(ut<0.8)=0.8P(u_t < 0.8) = 0.8. In the walkthrough, u2=0.5<0.8u_2 = 0.5 < 0.8, so x2x_2 is the true token "the".

Step 3 in symbols: the loss

The model defines pθ(yt∣x1,…,xt)p_\theta(y_t \mid x_1, \dots, x_t), the probability of the true next token given the inputs so far. The loss at step tt is

ℓt=−log⁡pθ(yt∣x1,…,xt)\ell_t = -\log p_\theta(y_t \mid x_1, \dots, x_t)

Note that the target is always yty_t, the true token. Only the input changes.

Step 5 in symbols: the schedule

The paper proposes schedules in which ϵi\epsilon_i decreases with the training step ii. Two of the forms are a linear decay, ϵi=max⁡(ϵmin⁡,1−c i)\epsilon_i = \max(\epsilon_{\min}, 1 - c\,i) for a small constant cc, and an exponential decay, ϵi=ki\epsilon_i = k^{i} with 0<k<10 < k < 1. For the walkthrough, ϵ\epsilon moves from 1 to 0.2 across training, so the input is mostly the model's own predictions at the end.

Worked examples

Example
1. One step, by hand

Let ϵ=0.5\epsilon = 0.5. The previous true token is "the" and the model's previous prediction is "a".

  1. Draw u=0.3u = 0.3.
  2. Since 0.3<0.50.3 < 0.5, the input is the true token "the".
  3. If the model's prediction for the next word is "cat" and the true target is "cat", the loss is −log⁡p(cat)-\log p(\text{cat}), which is small when p(cat)p(\text{cat}) is near 1.
  4. Interpretation: this step trained the model on the true input, so it behaves as teacher forcing for this step.
Example
2. A schedule over a whole epoch, standard case

Use the exponential schedule ϵi=0.9i\epsilon_i = 0.9^{i}, and look at the first three training steps.

  1. Step i=0i = 0: ϵ0=0.90=1\epsilon_0 = 0.9^0 = 1. Every input is the true token, which is teacher forcing.
  2. Step i=1i = 1: ϵ1=0.91=0.9\epsilon_1 = 0.9^1 = 0.9. About 90 percent of the inputs are true tokens.
  3. Step i=10i = 10: ϵ10=0.910≈0.349\epsilon_{10} = 0.9^{10} \approx 0.349. About a third of the inputs are the model's own predictions.
  4. Step i=30i = 30: ϵ30=0.930≈0.042\epsilon_{30} = 0.9^{30} \approx 0.042. Almost all the inputs are the model's own predictions.
  5. Interpretation: the schedule starts stable, with teacher forcing, and gradually hands control to the model. The decay rate 0.90.9 is a choice that the training run has to tune.
Example
3. A caption that recovers, realistic

A captioning model is training on a photo of a dog on a beach. The true caption is "a dog runs on the beach".

  1. Early in training, ϵ=0.95\epsilon = 0.95. The model sees the true word "dog" after "a" most of the time, so it learns the grammar of the caption.
  2. Suppose that at some step the model predicts "cat" after "a", and uu falls above 0.95. The next input is "cat", not "dog".
  3. The loss for the following word still uses the true target. The model is asked to produce "runs" after "a cat", which is hard but informative.
  4. Later in training, ϵ=0.3\epsilon = 0.3, so the model often sees its own words. It has learned to continue a caption from a wrong start, which is what happens at generation time.
  5. Interpretation: the schedule trains the model on the situations it will face when it generates, while the true targets keep the learning signal correct.

Try it: with ϵ=0.5\epsilon = 0.5 and u=0.7u = 0.7, which input is used at this step, and which token is it? Answer: the model's own previous prediction is used, because 0.7>0.50.7 > 0.5, and the input is that predicted token, not the true one.

Common mistakes and tradeoffs

It is tempting to think that scheduled sampling makes the model generate well by itself. In fact it only exposes the model to its own predictions during training. The loss still uses the true targets, so a model can learn to continue from its own mistakes without learning which continuation is right.

It is tempting to think that a higher ϵ\epsilon is always safer. In fact a model trained with ϵ\epsilon near 1 is the same as teacher forcing, and it suffers the exposure bias the method is meant to fix. The benefit comes from the decay, not from a fixed mix.

It is tempting to think the schedule is a law of nature. In fact the decay curve is a hyperparameter. A decay that is too fast can throw the model into its own errors before it has learned the task, and one that is too slow leaves the gap largely unclosed.

When to use it. Use scheduled sampling when a recurrent or encoder-decoder model is trained with teacher forcing and its outputs drift at generation time. When not to use it. Large transformer language models trained on very large corpora often work well with teacher forcing and other methods, so the extra schedule is not always worth tuning. Alternatives. Free-running training, which feeds only the model's predictions; and Reinforcement Learning methods that score whole sequences, which avoid the per-step mismatch but need more machinery.

History

Teacher forcing was used early for recurrent networks, because it makes training fast and keeps the gradient signal simple. Researchers noticed the gap at generation time, which later became known as exposure bias. In 2015, Samy Bengio, Oriol Vinyals, Navdeep Jaitly and Noam Shazeer proposed scheduled sampling for sequence prediction with recurrent networks, and reported gains on image captioning and speech recognition. The method caught on because it needed only a small change to the training loop, and it made the link between training and generation explicit.

Recap

  • Teacher forcing trains a sequence model on true previous tokens, which is fast and stable but leaves the model unprepared for its own errors.
  • Scheduled sampling chooses at each step between the true previous token and the model's own prediction, with a probability ϵ\epsilon that falls during training.
  • The loss always uses the true target, so the method changes only the inputs the model sees.
  • The schedule is a tuned hyperparameter, and its goal is to reduce the gap behind Exposure Bias.

Read next: Exposure Bias explains the gap that this method is designed to close.

Check yourself

QuestionIn one sentence, what does scheduled sampling change about the inputs during training, compared with teacher forcing?Show answer
At each step the input is the model's own previous prediction with some probability, instead of always the true previous token, and that probability grows over training.
QuestionWith ϵ=0.6\epsilon = 0.6, what is the probability that the input at a step is the true previous token?Show answer
It is 0.6, because the true token is used when a uniform draw uu satisfies u<0.6u < 0.6, which has probability 0.6.
QuestionWhy does scheduled sampling still compute the loss against the true target, even when the input was the model's own prediction?Show answer
The training signal has to say what the correct next token is. If the target were the model's own prediction, the model would learn to reproduce its mistakes and the loss would not measure the task.
QuestionA model trained with teacher forcing writes a good first sentence, then repeats itself. Scheduled sampling is proposed. What would you expect to change, and what would you check first?Show answer
You would expect the model to be more robust to its own errors during generation, so the repetition should reduce. Check first that the schedule is not decaying too quickly, because a fast decay can leave the model with its own errors before it has learned the task.

Sources