Module · 5 lessons

Generation & Sampling

A trained GPT outputs a probability distribution — turning it into text is a choice. Learn greedy decoding and why it gets stuck, then temperature, top-k, and top-p (nucleus) sampling, and build a configurable text generator for your trained mini-GPT in NumPy.

At a glance

Level
Advanced
Lessons
5 lessons
Time to complete
1 week
Cost
Free forever · no sign-up

Welcome to Generation & Sampling, the eighth module. A trained language model doesn’t produce text — it produces, at every step, a probability distribution over the next token. Turning that distribution into an actual sequence of characters is a separate decision, and the strategy you choose changes everything about how the output reads: repetitive or varied, coherent or unhinged. This module is about making that choice well.

You’ll start with greedy decoding — always taking the single most likely token — and see exactly why it produces flat, looping text. Then you’ll add controlled randomness: temperature to sharpen or soften the distribution, top-k sampling to draw only from the k most likely tokens and cut off the unlikely tail, and top-p (nucleus) sampling to keep just enough of the distribution’s mass to stay coherent while adapting to how confident the model is. Each is a few lines of NumPy on top of the logits your trained mini-GPT already produces. The guided project pulls them together into a single, configurable text generator, and you’ll compare the same model’s output across settings to feel what each knob does.

Start with Lesson 1, where the safest possible decoding strategy turns out to be a trap.

Lessons in this module

Achievement

Complete all 5 lessons to finish the Generation & Sampling module.

Start module
Sponsor

Keep DATATWEETS free. Help fund practical data, AI, and engineering lessons for learners worldwide.

Buy Me a Coffee at ko-fi.com