Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.6 — Sampling: Temperature, Top-k and Top-p
LLM Engineering · Example 1 of 2

Temperature reshapes the probabilities

Same five logits at three temperatures. Greedy ignores the temperature.

A model produces a score for every possible next token. Softmax turns scores into probabilities, and a sampler picks one. Temperature, top-k, top-p and the seed control how adventurous that pick is, which is why the same prompt can give different answers.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write softmax_t(logits, t) and print the probabilities of [2.0, 1.0, 0.0] at T=1 with 4 decimals.

02

Write top_k_tokens(probs, k) where probs is a dictionary of token to probability, returning the k most likely tokens as a list (ties broken by name). Test it with {"a": 0.5, "b": 0.3, "c": 0.2} and k=2.

Explain it without notes

01

Explain the path from logits to a chosen token.

02

What does temperature do, mathematically and in practice?

03

How do top-k and top-p differ, and why use them?

04

Why does the same prompt give different answers, and how do you make behaviour more repeatable?

Temperature reshapes the probabilities
Sign in to run this example in your browser.

Expected output

T=0.5: Paris=0.9340  London=0.0465  Rome=0.0171  the=0.0023  banana=0.0000
T=1.0: Paris=0.7067  London=0.1577  Rome=0.0956  the=0.0352  banana=0.0048
T=2.0: Paris=0.4661  London=0.2202  Rome=0.1715  the=0.1040  banana=0.0383
greedy pick (always the top score): Paris