When we run a model on some text, what comes out? Not words. A big list of scores, called logits. Let's turn those scores into probabilities, and see the model's real guesses.
For every position in the text, the model gives one score for every token in its vocabulary. These raw scores are called logits. A higher logit means a more likely next token. A function called softmax turns logits into probabilities, between zero and one, that add up to one.
We tokenize, the cat sat on the. Torch no grad says we're not training, which saves memory. The model returns logits. We take the last position, and apply softmax. Top k finds the five most likely tokens. Run it. The logits have one row of forty nine thousand scores, for each of our five tokens. And the top guesses are bed, edge, window, and table. Notice how spread out they are. A small base model isn't very sure.
app.pypython
import torchfrom transformers import AutoTokenizer, AutoModelForCausalLMname = "HuggingFaceTB/SmolLM2-135M"tok = AutoTokenizer.from_pretrained(name)model = AutoModelForCausalLM.from_pretrained(name)inputs = tok("The cat sat on the", return_tensors="pt")with torch.no_grad(): logits = model(**inputs).logitsprint("Logits shape:", tuple(logits.shape))probs = torch.softmax(logits[0, -1], dim=-1)top = torch.topk(probs, 5)for p, i in zip(top.values, top.indices): print(f"{tok.decode(i)!r:10} {p.item():.1%}")
Let's recap. A model outputs logits, one score per token. Softmax turns them into probabilities. And the highest probability is the model's best guess. Next, generating whole sentences.