rohitg00/ai-engineering-from-scratch · error · ValueError
temperature must be positive
Error message
temperature must be positive
What it means
Error "temperature must be positive" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/35-gpt-model-assembly/code/main.py:206
threshold = values[..., -1:]
return torch.where(logits < threshold, torch.full_like(logits, float("-inf")), logits)
def generate(
model: GPTModel,
prompt: torch.Tensor,
max_new_tokens: int,
temperature: float = 1.0,
top_k: int | None = None,
seed: int | None = None,
) -> torch.Tensor:
"""Autoregressive generation with multinomial sampling, temperature, top-k.
Holds the active window to model.cfg.context_length by sliding the oldest
tokens out when the running sequence overflows.
"""
if temperature <= 0:
raise ValueError("temperature must be positive")
if seed is not None:
torch.manual_seed(seed)
was_training = model.training
model.eval()
tokens = prompt.clone()
try:
with torch.no_grad():
for _ in range(max_new_tokens):
window = tokens[:, -model.cfg.context_length:]
logits = model(window)
next_logits = logits[:, -1, :] / temperature
next_logits = top_k_filter(next_logits, top_k)
probs = F.softmax(next_logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
tokens = torch.cat([tokens, next_token], dim=1)
return tokens
finally:View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/35-gpt-model-assembly/code/main.py:206 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/ef3988b392873099.
Report an issue: GitHub.