AI Hints
Home›Generative AI Tutorial›Tuning and Alignment
Generative AI · Lesson 18 of 77

Sampling and Temperature

The dial that controls creativity.

⏱ 2 min read▶ Video at 44:33🎓 Free
Watch this lesson in the Generative AI courseWatch on YouTube ↗Full Generative AI course

When a model has a chance for every next word, how does it choose one? It could always pick the most likely word. But then every answer would be boring, and identical. The choice rule is called sampling, and one setting, temperature, controls how adventurous it is.

Sampling and Temperature
From the video: Sampling and Temperature
  • How does the model choose a word?
  • Always the safest word is boring
  • Temperature: the creativity dial

The temperature dial

Here are real chances from GPT two, for the next word after, once upon a time, there was a little. At a low temperature, the model sharpens the differences. The most likely words become even more likely, and the answers become focused, and predictable. At a medium temperature, the chances stay as the model believes them. At a high temperature, the chances flatten out. Unlikely words get a real opportunity, and answers become surprising, and sometimes strange.

Once upon a time, there was a little …
girl
7.0%
bit
3.9%
boy
2.6%
man
1.2%
more
0.9%
talk
0.7%
of
0.7%
black
0.7%
The temperature dial
From the video: The temperature dial

Choosing the temperature

So which temperature should you use? For facts, maths, and code, use a low temperature, near zero, so answers stay accurate and consistent. For emails and everyday writing, a medium temperature is a good balance. For brainstorming, poems, and fresh ideas, use a higher temperature, to get surprising variety. Most tools set a sensible default, but now you know what the dial does.

TaskTemperatureWhy
Facts, maths, codelow (0 to 0.3)accuracy and consistency
Everyday writingmedium (0.5 to 0.8)natural and balanced
Brainstorming, poetryhigh (0.9 and up)variety and surprise

Other sampling controls

Two more controls are common. Top K says, only consider the K most likely words. Top P, also called nucleus sampling, says, only consider the smallest group of words, whose chances add up to P percent. Both cut off the long tail of silly options, while keeping some variety. You will often see them next to temperature, in the settings of a model.

Top-Konly the K most likely words
Top-Pthe smallest group adding up to P%
Cut the silly tailkeep sensible variety
Used with temperaturethree dials, one goal

Summary

Good. A model gives every word a chance, and sampling chooses one. Temperature reshapes the chances. Low is focused, high is creative. Top K and top P trim the unlikely tail. Next, we meet the limit that every model has, how much text it can hold in mind at once.

Key takeaways
  • Sampling picks the next word
  • Low temp = focused, high = creative
  • Top-K and top-P trim the tail
Next up: The Context Window — how much a model can remember