FixTheRobot Explore Robotics / Robot Learning / M2 / M2-L1

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

Robot Learning · Supervised Learning for Robots · M2-L1 · Beginner

Fit Steering with Gradient Descent

Experiment log · Scout at Meridian General Hospital

Experiment log, Meridian General Hospital, 09:40. Scout follows the long ward corridor, and the team has two hours of logs: heading error, lateral offset, speed and range to the next waypoint, together with the steering command the old controller issued. The new steering model is supposed to be a plain linear fit trained by gradient descent. The first attempt printed nan on the third epoch.

You will rebuild the training code in three steps and test each one before trusting the next: the loss and its gradient, the scaling of the features, and the descent loop itself. The orange curve in the last stage is the original failure, reproduced.

Your task

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

    Plain Python with math and heapq. Your code runs in a background worker: an endless loop is stopped after a few seconds and the page stays usable. It runs in your browser and is never sent anywhere.

    Hints

    What this lab teaches

    Stage 1 is the loss and its gradient for a linear model: mean squared error, the chain rule, and the two mistakes that make a training loop climb or explode (a flipped sign and a sum instead of a mean). Stage 2 is feature scaling done the way a deployed model must do it: statistics from the training log only, applied unchanged to anything new, with a guard for a column that never changes. Stage 3 is batch gradient descent from zero, graded on the loss curve itself: it must never go up and it must end at the least-squares minimum.

    Grading is result-based. Your functions run on hidden datasets with other sizes, other numbers of features and other learning rates, and are compared with exact values computed in JavaScript. The last stage also shows why scaling matters: the largest eigenvalue of the loss curvature, computed from the real data, caps the learning rate, and the raw features cap it about two hundred times lower than the standardised ones.

    Theory for this lab

    Supervised Learning for Robots labs

    1. M2-L1: Fit Steering with Gradient Descent: this lab.
    2. M2-L2: Train a Tiny Neural Network
    3. M2-L3: Catch the Overfit Model

    Educational simplification: the steering law in the logs is exactly linear plus small Gaussian noise, with no saturation of the command and no feedback between the model and the data. Real steering logs have nonlinearities, outliers and operators who react to the robot. The loss here is convex, which is why a stable learning rate always converges; neural-network losses are not (lab M2-L2). Lessons M2-T1, M2-T2 and M2-T3 give the theory.

    Get the next lab

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