Lesson 1 · Unit 02
Examples for a language model
Templates, padding, masks and the labels that are not scored
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?
Correct answer
Only the answer tokens
Why
Users will write the questions; the model only ever has to write answers. Scoring the question tokens spends the fine-tune on learning to imitate customers. The question still goes in as input: it just is not graded.
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.
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.