AI Hints
Home›Fine-Tuning LLMs Tutorial›Models, Logits and Generation
Fine-Tuning LLMs · Lesson 14 of 120

Logits and Probabilities

What the model outputs.

⏱ 2 min read▶ Video at 21:57🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

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.

  • What comes out?
  • Scores called logits
  • Turn them into probabilities

Logits and softmax

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.

keyvalue
Logitsa score for every token
Highermore likely next
Softmaxscores → probabilities
Probabilitiesadd up to 1

The model's real guesses

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 torch
from transformers import AutoTokenizer, AutoModelForCausalLM

name = "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).logits
print("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%}")
Output
Logits shape: (1, 5, 49152)
' bed'     7.2%
' edge'    4.5%
' window'  3.3%
' table'   3.2%
' tree'    2.4%

Quick check

Let's check. What does softmax do? Pause, and think about it. The answer is A. It turns raw scores into probabilities that add up to one.

Quick check
What does softmax do?

Summary

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.

Key takeaways
  • Logits = scores
  • softmax → probabilities
  • topk for the best guesses
Next up: Generating Text — model.generate()