Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 11 of 12
Module 6 Practice
Practice working with temperature and sampling to understand how they affect LLM output.
Mini Project: Temperature Explorer
Mini Project: Temperature Explorer
# Mini Project: Explore how temperature affects output diversity
import random
import math
def softmax_with_temp(logits, temperature):
if temperature == 0:
probs = [0.0] * len(logits)
probs[logits.index(max(logits))] = 1.0
return probs
scaled = [l / temperature for l in logits]
max_l = max(scaled)
exp_l = [math.exp(l - max_l) for l in scaled]
total = sum(exp_l)
return [e / total for e in exp_l]
def sample_n_times(tokens, logits, temperature, n=5):
"""Sample the next token n times at a given temperature."""
probs = softmax_with_temp(logits, temperature)
results = []
for _ in range(n):
selected = random.choices(tokens, weights=probs)[0]
results.append(selected)
return results, probs
# Scenario 1: Factual question (peaked distribution)
print("=== Scenario 1: Factual Question ===")
print("Context: 'The capital of France is'")
factual_tokens = ["Paris", "Lyon", "Marseille", "Nice", "Bordeaux"]
factual_logits = [8.0, 1.5, 1.2, 0.5, 0.3]
for temp in [0, 0.3, 0.7, 1.0, 1.5]:
samples, probs = sample_n_times(factual_tokens, factual_logits, temp, 5)
print(f" Temp={temp}: {samples}")
print()
# Scenario 2: Creative task (flat distribution)
print("=== Scenario 2: Creative Task ===")
print("Context: 'The first word of a poem about the moon:'")
creative_tokens = ["Silvery", "Pale", "Glowing", "Ancient", "Quiet", "Bright"]
creative_logits = [2.0, 1.8, 1.6, 1.4, 1.2, 1.0]
for temp in [0, 0.3, 0.7, 1.0, 1.5]:
samples, probs = sample_n_times(creative_tokens, creative_logits, temp, 5)
print(f" Temp={temp}: {samples}")Diagram
Loading diagram…
Practice Diagram
Key Takeaways from Module 6
- LLMs generate text autoregressively — one token at a time, with each token conditioned on all previous tokens
- The model's forward pass produces a probability distribution over the entire vocabulary for the next token
- Temperature scales this distribution: low temperature = more predictable, high temperature = more creative
- Sampling (rather than greedy selection) is used for most applications to produce natural, varied output
- Top-p sampling only considers tokens until their cumulative probability reaches p — balancing coherence and variety
- Generation stops when a stop token is produced or maximum length is reached
- Streaming works because tokens can be sent to the user as they are generated one by one
Key Takeaways
- Practice working with temperature and sampling to understand how they affect LLM output.
- LLMs generate text autoregressively — one token at a time, with each token conditioned on all previous tokens
- The model's forward pass produces a probability distribution over the entire vocabulary for the next token
- Temperature scales this distribution: low temperature = more predictable, high temperature = more creative
Finished this lesson?
0 of 12 in Module 6