Mark which tokens contribute to a supervised reply loss
Watch prefixes in words such as preferred and rejected. · 210 words
Supervised reply loss should credit only the assistant's target tokens. In this trainer, mark ignored target positions with -100 and keep the token IDs for desired outputs. Its loss function is configured to leave the -100 positions out of the score. If a training sample has several turns, apply this rule to each turn: user tokens are ignored, while assistant reply tokens are targets. For one reply, report the mean of its target-token losses. In a batch, averaging across all target tokens gives a longer reply more influence than averaging one mean loss per reply.
Here is a complete bookstore example. The request is "Recommend one beginner book about the night sky." Under a simplified tokenizer, the reply "Try Moon Basics." has tokens [Try, Moon, Basics, .]. Use a period as the final token of the request. The complete training sequence is [Recommend, one, beginner, book, about, the, night, sky, ., Try, Moon, Basics, .]. Before the usual one-position shift for next-token prediction, the labels are [-100, -100, -100, -100, -100, -100, -100, -100, -100, Try, Moon, Basics, .]. Only the last four tokens contribute. If their individual losses are 2.0, 1.0, 1.5, and 0.5, their sum is 5.0 and the mean reply loss is 5.0 / 4 = 1.25.
Check your recall
Which tokens contribute to the bookstore example's reply loss, and what is the mean loss?
Check your answer
The tokens "Try", "Moon", "Basics", and the final period contribute. Their losses total 5.0, so the mean supervised reply loss is 1.25.
Sources and credits
Original KeyTouch lesson. Background reading · Nathan Lambert · Reinforcement Learning from Human Feedback.