Temperature, Top-k & Top-p Explained: Why AI Answers Change | LLM Course #28
Beginner-level AI & ML video in Tamil by Programming Line, 5 minutes long, published 2 Dec 2026.
How does an LLM pick the next word? In Lecture 28 of the LLM Masterclass we take our trained ChotuGPT and the sentence "After my morning chai, I feel ___" and turn its probabilities into text: greedy decoding, sampling, temperature, top-k and top-p (nucleus) sampling, with the model's real probability bars at temperatures 0.5, 1.0 and 1.5. Then we write next_token_probs() and generate() in PyTorch. 💻 Code for this lecture (branch lecture-28): https://github.com/programmingline/chotugpt/tree/lecture-28 Lecture 28 of 32 — LLM Masterclass · Module 6: Generation and speed ⚠️ ChotuGPT is a simplified teaching model. The probabilities and samples on screen are the real outputs of our small model trained in Lecture 24; the 'sure / unsure' sketch in the top-k section is an illustration. In this lecture: • Decoding: turning the next-token probabilities into one chosen token, again and again • Our model: fresh 87%, calm 6%, awake 4%, happy 2% • Greedy decoding: always the top token, the same text every time, often dull for long text • Sampling as a weighted dice; seed 1 gives "I feel awake. I met Anu at the shop." • Temperature: softmax(scores / T); T = 0.5 gives fresh 99%, T = 1.5 gives fresh 64% and broken words • Top-k: keep the k most likely tokens and renormalise (k = 3: fresh 90%) • Top-p / nucleus sampling (Holtzman et al.): the smallest set reaching p; p = 0.9 keeps fresh and calm • PyTorch code: next_token_probs(), generate() with torch.multinomial and a seeded generator Chapters: 00:00 Lecture 27 quiz answer 00:13 Decoding 00:24 One token at a time 00:42 Greedy decoding 01:01 Adding chance 01:07 Sampling: a weighted dice 01:26 Temperature 01:56 Top-k sampling 02:15 Top-p (nucleus) sampling 02:42 Three samples per setting 02:57 next_token_probs in PyTorch 03:27 Quiz 03:47 In practice 03:59 Recap & next lecture ✅ Lecture 27 quiz answer: C, which answers people prefer. The reward model learns from human rankings, then scores new answers. ❓ Quiz: Raising the temperature makes the output… A) Always the same B) More random and varied C) Shorter. Comment your answer. I reveal it in Lecture 29! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 27 — RLHF, reward models and DPO ⏭ Next: Lecture 29 — KV cache, context windows and fast inference Sources: A. Holtzman, J. Buys, L. Du, M. Forbes, Y. Choi, "The Curious Case of Neural Text Degeneration", arXiv:1904.09751 (2019), ICLR 2020 (nucleus / top-p sampling; maximization-based decoding leads to degeneration). A. Fan, M. Lewis, Y. Dauphin, "Hierarchical Neural Story Generation", ACL 2018 (top-k sampling). #LLM #Temperature #TopP…more
How does an LLM pick the next word? In Lecture 28 of the LLM Masterclass we take our trained ChotuGPT and the sentence "After my morning chai, I feel ___" and turn its probabilities into text: greedy decoding, sampling, temperature, top-k and top-p (nucleus) sampling, with the model's real probability bars at temperatures 0.5, 1.0 and 1.5. Then we write next_token_probs() and generate() in PyTorch. 💻 Code for this lecture (branch lecture-28): https://github.com/programmingline/chotugpt/tree/lecture-28 Lecture 28 of 32 — LLM Masterclass · Module 6: Generation and speed ⚠️ ChotuGPT is a simplified teaching model. The probabilities and samples on screen are the real outputs of our small model trained in Lecture 24; the 'sure / unsure' sketch in the top-k section is an illustration. In this lecture: • Decoding: turning the next-token probabilities into one chosen token, again and again • Our model: fresh 87%, calm 6%, awake 4%, happy 2% • Greedy decoding: always the top token, the same text every time, often dull for long text • Sampling as a weighted dice; seed 1 gives "I feel awake. I met Anu at the shop." • Temperature: softmax(scores / T); T = 0.5 gives fresh 99%, T = 1.5 gives fresh 64% and broken words • Top-k: keep the k most likely tokens and renormalise (k = 3: fresh 90%) • Top-p / nucleus sampling (Holtzman et al.): the smallest set reaching p; p = 0.9 keeps fresh and calm • PyTorch code: next_token_probs(), generate() with torch.multinomial and a seeded generator Chapters: 00:00 Lecture 27 quiz answer 00:13 Decoding 00:24 One token at a time 00:42 Greedy decoding 01:01 Adding chance 01:07 Sampling: a weighted dice 01:26 Temperature 01:56 Top-k sampling 02:15 Top-p (nucleus) sampling 02:42 Three samples per setting 02:57 next_token_probs in PyTorch 03:27 Quiz 03:47 In practice 03:59 Recap & next lecture ✅ Lecture 27 quiz answer: C, which answers people prefer. The reward model learns from human rankings, then scores new answers. ❓ Quiz: Raising the temperature makes the output… A) Always the same B) More random and varied C) Shorter. Comment your answer. I reveal it in Lecture 29! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 27 — RLHF, reward models and DPO ⏭ Next: Lecture 29 — KV cache, context windows and fast inference Sources: A. Holtzman, J. Buys, L. Du, M. Forbes, Y. Choi, "The Curious Case of Neural Text Degeneration", arXiv:1904.09751 (2019), ICLR 2020 (nucleus / top-p sampling; maximization-based decoding leads to degeneration). A. Fan, M. Lewis, Y. Dauphin, "Hierarchical Neural Story Generation", ACL 2018 (top-k sampling). #LLM #Temperature #TopP