Bitpath

Lesson 1 · Unit 02

Examples for a language model

Templates, padding, masks and the labels that are not scored

13 exercisesabout 3 min

What you practise

  • Prompt and completion, chat formats with roles, tokenising, padding and the attention mask, labels shifted by one and the ignore index (-100) that masks what should not be learned

Try a question from this lesson

Pick one

You fine-tune a model to answer support questions. Each example is a question followed by the ideal answer, as one token sequence. Which predictions should be scored?

Hint

At serving time, who writes the question and who writes the answer?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

One sequence, with role markers

A training example is a single run of tokens: the prompt, then the completion. Chat models wrap each turn in role markers, special tokens that say who is speaking and where the turn ends.

Each model family has its own template. The exact markers differ; the idea does not.

<|user|>
What is the capital of France?
<|assistant|>
Paris.<|end|>

Concept

Labels, and the value that is skipped

The labels of a language model are its input ids shifted by one: each position is graded on predicting the next token.

Positions you do not want to learn from get the label -100. PyTorch's cross-entropy skips that value by default (ignore_index=-100): no loss, no gradient. Masking the prompt and the padding is done with this one number.

Python
labels = torch.tensor([3, -100, -100, 7])
loss = F.cross_entropy(logits, labels)

10 more exercises in the app

  • 4× multiple choice
  • 2× fill the blanks
  • 1× type the answer
  • 1× true or false
  • 1× match the pairs
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy