Hugging Face, End to EndLesson 14 of 75

Module 2 · Transformers, from characters to output

Logits, and the four knobs between them and words

In this lesson

  • 10 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 5 of 9 in this chapter

This chapter’s goalTrace a piece of text from characters through a tokenizer to logits and back to text with the transformers library, and say which of the Auto class, the tokenizer, the padding side, the chat template or the generation config is at fault when a model loads without error and produces nonsense.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableWatch4:51
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

What comes out of the model

Python
out = model(**enc)
print(out.logits.shape)   # [batch, sequence_length, vocab_size]

For a causal model with a 151,936-token vocabulary and a 12-token prompt, that is a tensor of 12 × 151,936 raw numbers. Only the last row matters for generating the next token: it holds one score per possible next token, unnormalised, typically between about −20 and +20.

Softmax turns that row into a probability distribution. Then something has to choose, and everything people call creativity or randomness lives in that choice.

Python
import torch
next_logits = out.logits[0, -1]
probs = torch.softmax(next_logits, dim=-1)
top = torch.topk(probs, 5)
for p, i in zip(top.values, top.indices):
    print(f"{p:.3f}  {tok.decode(i)!r}")

Run that on a half-finished sentence once. Seeing the actual candidates, and how often the top one holds 0.95 of the mass, makes every knob below intuitive rather than mystical.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Logits, and the four knobs between them and words’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/from-logits-to-text (Accessed: 29 November 2026).