TinyLanguageBuilder / The Tiny Textbook

Chapter 5: Sampling

A trained model never says a word. It says how likely every word is. Turning that list of chances into actual text is a separate step, and the settings of that step decide whether the model plays it safe, wanders off, or repeats itself.

1. From chances to a choice

After reading a prompt, the model produces one raw score per token in its vocabulary, called a logit. A softmax (chapter 3) turns the scores into probabilities. To write text you pick one token, add it to the prompt, and ask again. There are two obvious ways to pick:

The dial between those two is the temperature. Before the softmax, divide every score by the temperature T:

probability ∝ escore / T

A small T exaggerates the gaps between scores, so the favourite takes nearly everything (as T goes to 0 you get greedy). A large T shrinks the gaps, so the chances even out (as T grows you approach pure noise). T = 1 leaves the model's own probabilities untouched. A second dial, top-k, throws away everything except the k best tokens before rolling the die, which cuts off the long tail of silly options.

2. Get a model

Loading the engine…

3. Turn the dials

    Bars are the chances after your temperature and top-k, so they always add up to 100% across all tokens (only the best 12 are drawn). The tick on each bar appears after you roll 1,000 times, and shows how often that token actually came up: it should sit close to the bar's end, which is what "weighted die" means. Slide the temperature down and the favourite swallows everything; slide it up and the bars flatten. The score shown beside each token is its raw logit, which never changes with temperature.

    4. Same prompt, three temperatures

    Now let the model write 60 tokens, using the prompt above, three times with the same random seed but different temperatures.

    Cool, T = 0.3

    Normal, T = 0.8

    Hot, T = 1.6

    Look for the pattern across several seeds. The cool text is tidy but samey and may loop. The normal text is the useful middle. The hot text drifts into strange words and broken spellings, because the long tail of unlikely tokens now gets picked often. The best setting depends on the job: low for facts and code, higher for brainstorming.

    5. What to take from this, and what not to

    Training and sampling are separate. Temperature changes nothing inside the model; the same trained numbers can be used cautiously or wildly. You can try different settings for free, after training.

    Low temperature is not "more correct". It makes the model more predictable, and a confident wrong answer is just as predictable as a right one. It also makes loops more likely.

    This model is tiny. It has about 60,000 numbers and read a few megabytes of children's stories, so even its best text wanders. Big models are built the same way, trained on vastly more text; sampling works identically on them.

    That is the whole pipeline of a language model: text to tokens (1), tokens to vectors (2), vectors mixing through attention (3), a loss and small steps that tune it (4), and sampling to turn its chances back into text (5). The main app builds on all five, then adds fine-tuning and preference tuning to steer behaviour.

    6. Using it for your own purpose

    Everything above ran on a children's-story sample, but nothing about the model is specific to it. You can point the same machinery at your own material, in two ways.

    1. Train it on your own text. In the main app, choose My own text file under Set up, pick a size and press Train. The model learns the vocabulary and style of that file: recipes, song lyrics, product descriptions, a diary. From a terminal the same thing is tlm train --data mytext.txt --out runs/mine. Then use Continue some text (chapter 5's sampling under the hood) or tlm generate.

    2. Teach it a task with your own examples. Under Teach it a task, choose My own examples and give it a .jsonl file: one example per line, each with a prompt and the response you want. The page has a sample file to start from. Fine-tuning then uses exactly the loop from chapter 4, but measures the loss only on your responses. A tenth of your examples is held back and never trained on, so the page can show you your answer next to the model's.

    The same two steps work from the command line: tlm sft --data examples.jsonl.

    What to expect

    That is the point of a tiny model: it is small enough that you can see, and change, every step of how it learns.