Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Sampling Strategies

In the previous ten chapters, we trained a GPT model. In Chapters 11 to 14, we study how to use the model.

The basic function of a GPT model is to generate text, but how to generate it takes some care.

The simplest approach is greedy search: at every step, pick the token with the highest probability. But the text generated this way is often rigid and easily falls into repetition.

The solution is sampling: pick tokens at random according to the probability distribution. But complete randomness may generate incoherent content.

Below are several commonly used sampling strategies, which seek a balance between generation quality and diversity.

Temperature sampling

Temperature is the simplest parameter for controlling sampling. Before the Softmax, divide the logits by the temperature TT:

pi=exp⁡(zi/T)∑jexp⁡(zj/T)p_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}
  • T=1T = 1: standard sampling, choosing by the original probability distribution;

  • T=0T = 0: greedy search, always choosing the most probable token;

  • T>1T > 1: the probability distribution is flatter and more random;

  • T<1T < 1: the probability distribution is sharper and more deterministic.

Top-k sampling

Top-k sampling samples only from the kk tokens with the highest probability and sets the probability of all other tokens to 0.

  • Advantage: filters out noisy tokens in the long tail and improves generation quality;

  • Disadvantage: kk is fixed and cannot adapt to different contexts.

Top-p sampling

Top-p sampling samples from the smallest set of tokens whose cumulative probability exceeds pp.

  • Advantage: the size of the candidate set adapts, with a wide range when the distribution is flat and a narrow range when it is sharp;

  • Disadvantage: the algorithm is slightly more complex.

Combined use

In practice, several strategies are usually combined:

  • temperature + top-k: adjust the temperature first, then filter;

  • temperature + top-p: adjust the temperature first, then filter adaptively;

  • temperature + top-k + top-p: triple control.

GPT sampling trainer

Hyperparameters

Model parameters

The model parameters must stay consistent with the pre-training stage.

Modeling

Loading the model

ROMEO:
What says the senate of the court?

PETRUCHIO:
I will not stay the state of the country.

POMPEY:
I would not be so so so so so so so so so this son,
And the shall be the straight of the death.

POLIXENES:
I will not be so.

POMPEY:
I would not be so so s

Temperature sampling

ROMEO:
He may the body majesty. But, thy state, that I had
men my true princely from our grown.

QUEEN MARGARET:
Unferal the news have been and lords these better
What he sates on the best and will place
That set I think to be a repus before.

QUEEN ELIZABETH:
M

Top-k sampling

ROMEO:
What's my mistress. They say, the senate is to
hold my bear the will be come to be the sometime
As a seat, are any bearing treads,
We are nother'd their severences, by me,
As it brace off this bloody, and worthy
To stay the perform'd. The darest is nothin

Top-p sampling

ROMEO:
Ay, and no more of in this looks: but I not
fare not to book'd in out thy body
To fight, that was for your honours and may consul!

SLY:
He lost the company of my death, some party,
The shall be stop and stay what now means are mother;
For honour provides

Combined sampling

ROMEO:
I saw the did three thy banish'd.

POMPEY:
I have did my lord, I speak a stop, and I say
The consul: that have my loved in their
Thou wish'd of mine and that she wars and with their sease,
Thou art the bearing borness and sentender to
bring the country to

In this chapter, we implemented three sampling strategies: temperature controls the randomness, top_k filters the long tail, and top_p adapts the candidate set. Used together, they can also balance generation quality and diversity flexibly.

Exercises

  1. Try adjusting the combination of temperature, top_k, and top_p and observe how the generated text changes. Which combination is best for generating Shakespeare-style scripts?