FixTheRobot Explore Robotics / LLM / LM7 / LM7-L2

This lab works on phones, but writing code is much easier on a laptop.

LLM · Instruction Fine-Tuning · LM7-L2 · Intermediate

Implement Loss Masking

In instruction tuning the model is shown a prompt and a response. It should learn to produce the response, not to imitate the prompt. The loss therefore skips the prompt positions and the padding, but keeps the response and the end-of-turn token.

You will see why, build the shifted labels with the ignore value −100, and write the masked mean loss.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math (and slamkit for small matrix helpers: matmul, transpose, solve, inverse, identity). Images, point clouds and detections arrive as lists inside the dictionary D; an image is a list of rows, each row a list of pixels. There is no NumPy or OpenCV: you write the algorithm yourself, which is the point of the lab. Print with print().

    Hints

    What this lab teaches

    Stage 1 shows which positions should count. Stage 2 builds the inputs and masked targets. Stage 3 computes the masked loss. The losses and log-probabilities are invented numbers; no model is trained. Your code is graded on hidden sequences.

    Theory for this lab

    Instruction Fine-Tuning labs

    1. LM7-L1: Format Instruction Data with Templates
    2. LM7-L2: Implement Loss Masking: this lab.
    3. LM7-L3: Instruction-Tune a Tiny Model and Evaluate It

    Educational simplification: short sequences of integers, tables of log-probabilities with a few columns, a single sequence per case. Real training does the same on batches of long sequences with a vocabulary of tens of thousands, using the ignore index of the cross-entropy function. All data in this lab are invented.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.