PythiloLesson 4 of 9 · progress kept in this browser

Sampling

A language model gives scores, logits, for every possible next token. How you pick from them changes the text.

  • Greedy: always the highest score. Safe, often repetitive.
  • Temperature: divide the logits before the softmax. Below 1 sharpens towards the top choice, above 1 flattens towards random.
  • Top-k: keep only the k best tokens and sample among them, so very unlikely words never appear.
import torch

logits = torch.tensor([2.0, 1.0, 0.1])
for t in (0.5, 1.0, 2.0):
    p = torch.softmax(logits / t, dim=0)
    print(t, p)

torch.multinomial(p, 1) draws an index with those probabilities. A seeded torch.Generator makes a draw repeatable.

Your task

Write probabilities(logits, temperature=1.0) returning the softmax of logits / temperature. Write top_k(logits, k) returning a copy where every score but the k largest is minus infinity. Write sample(logits, temperature=1.0, k=None, generator=None) returning one drawn index as an int, applying top-k when k is given.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.