Lessons

Eight hands-on lessons

Each lesson takes twenty to forty minutes at the Mac Mini. You write a prediction before you look, then find out whether the machine agrees. Being wrong in an interesting way is the point. Every word used here is explained in the guide.

What you need

  • The Mac Mini, Terminal, and the project at ~/mario-rl with the game ROM in place.
  • A notebook. Each lesson asks you to write a prediction down before you watch. Do it on paper; you will be tempted to change it.
  • Lessons 3, 4 and 5 change a running training. Do lesson 2 first and keep its run going.

The two commands

cd ~/mario-rl
tools/start-training.sh NAME        # trainer + browser panel + viewer window
tools/stop-training.sh              # stops all of it, safe to run twice

The start script opens the panel at http://localhost:8765 and then runs the viewer window in the same Terminal. Closing the viewer with q does not stop training. The run's files land in runs/NAME/.

Viewer keys

KeyDoes
spacepause and resume training at the next rollout
htake over env 0's controller; press again to hand it back
arrows Z Xwhile driving: move, jump (A), run and fire (B)
sslow motion for env 0, 15 frames per second
F5 / F7save / load env 0's game state (or 5 / 7)
+ / -entropy coefficient ×1.5 / ÷1.5
[ / ]learning rate ÷1.5 / ×1.5
ccheckpoint now
qclose the viewer; training continues

The panel has the same controls as sliders, plus every reward weight and the charts. Each has a help icon; the same text is in the guide's knobs section.

Only env 0 is on screen. Fifteen more Marios are practising at full speed in the background, and the learning uses all sixteen. What you see is one sixteenth of the evidence.

Lesson 1

Watch the teacher play 2-1

Goal. See what a finished Phase 1 policy looks like before you watch one being made. The 2-1 teacher is the policy saved at the moment it passed its exam on World 2-1.

Predict first

Write down: How far will it get in one life? Will it die, and to what? Will it pick up coins or the mushroom? Will it ever walk left? Will it look like a person playing?

Steps

  1. Open Terminal and run:
    cd ~/mario-rl
    uv run python tools/play.py --model teachers/ram/2-1.pt --stage 2-1 --obs-tiles
    The flag --obs-tiles is needed because this teacher was trained with the 7 × 5 tile window; without it the tool refuses and says so.
  2. A game window opens at real speed. The Terminal prints one line per agent step: x position, timer, every nonzero reward component, and the action taken. One line per episode says how it ended.
  3. Watch three lives. Then run it again with --sample added, and again with --seed 1005.
  4. Close the window or press Ctrl-C to stop.

What to watch

  • The action column. How many different actions does it use in a whole life?
  • The reward lines when something happens: a stomp, a coin, the flag.
  • Whether it hesitates anywhere, and whether the hesitation is at the same spot every life.
What actually happens and why

With the default settings the tool plays greedy: the single most likely action every step, on seed 1000. That is exactly the exam the teacher passed, five lives in a row on seeds 1000 to 1004, so it should clear. It mostly holds run-right and run-jump-right; the guide's action chart says the 1-1 teacher spends about two thirds of its steps on those two.

It does not go out of its way for coins. A coin pays 5 and at most 20 coins count per life, while running pays 1 per pixel and the flag pays 1,500 plus a time bank of 3 per second left. Detours lose. The mushroom pays 150, which is worth a small hop but not a long walk back.

--sample draws the action from the ten probabilities instead of taking the top one, which is how training plays. Expect more deaths: sampling adds mistakes, and a policy that is 90% sure at a pit still jumps early one time in ten. --seed 1005 only changes how many idle frames pass before the first input (0 to 8), yet that is enough to shift every enemy's timing, which is why the exam uses five seeds.

It does not look like a person. It never waits to see what an enemy does; it has learned timings, not intentions. The tile window lets it see a pipe or a pit one tile ahead, and that is the whole of its vision.

Lesson 2

Start a fresh run and watch the first 100,000 steps

Goal. See a policy that knows nothing, and see the first habit form. Keep this run going for lessons 3 to 5.

Predict first

Write down: What will Mario do in the first thirty seconds? Which of the ten action bars will be tallest? What will the entropy number read? Roughly when will the first clear happen, in steps?

Steps

  1. Run tools/start-training.sh lesson2. A browser tab opens with the panel; the viewer window opens in the Terminal. The trainer prints its progress in a tmux session you can ignore.
  2. In the panel, find the action bars, the entropy number, and the outcome chart. Note the first values.
  3. Watch the viewer for two minutes. Press s if it is too fast to follow, and again to switch it off.
  4. Wait for the step counter to pass 100,000. At about 2,000 steps per second that is under a minute of training, longer with the viewer open.
  5. Leave it running.

What to watch

  • The action bars at the start, and which one grows first.
  • The entropy number: it starts near 2.3 nats, the maximum for ten equally likely actions.
  • Where Mario dies. The panel's median death x tells you which hazard is the current wall.
What actually happens and why

The network starts with its last layer near zero, so the ten probabilities are almost equal at 0.10 each. Mario jitters: a jump, a step left, a step right, a crouch. Four of the ten actions include Right and three include Left, so even random play drifts slowly right, which is why the first deaths happen at the first Goomba rather than at the spawn.

The reward does the rest. Every step to the right pays a pixel; every step alive costs a quarter point; a death costs 100 plus half the progress so far. Within about 100,000 steps, after roughly fifty updates, the right-moving actions have been paid more often than the others and their bars grow. This is the "right-hold" habit, and the alerts tool watches for it becoming the only habit: right-hold above 70% with jumps under 2% after a million steps is a warning sign, because a policy that never jumps cannot pass a pit.

Entropy falls slowly, not in a cliff. A cliff would mean the policy collapsed onto one button before it had seen enough, which the entropy bonus exists to prevent. The second run of the day passed 1-1 at step 901,120; its first training clears came well before that, because training play samples and sometimes gets lucky, while the exam wants the most likely action to clear five times in a row.

Lesson 3

Set the time weight to 0

Goal. Change one reward term while the run is alive, and learn how long a change takes to show, and where it shows first.

Predict first

Write down: With no cost for time, will Mario slow down, stop, or play the same? Which chart will change first, and how soon? Will lives get longer or shorter?

Steps

  1. In the panel, find the reward weights. Note the current value of time: −0.25 per step.
  2. Set it to 0. The change applies at the next rollout boundary, within a few seconds, and is written to runs/lesson2/events.jsonl with the step it took effect.
  3. Watch the reward components chart for two minutes, then the viewer for two minutes, then the episode length number.
  4. Leave it at 0 for at least 300,000 steps if you can, then set it back to −0.25.

What to watch

  • The time line in the reward components chart. It is the one thing that changes immediately.
  • Episode length and the stuck rate over the next few hundred thousand steps.
  • The event marker on the charts at the step you made the change.
What actually happens and why

The time line drops to zero at the marker; nothing else changes for a while. Rewards already collected keep their old values, and the network only moves a little per update, so behaviour lags a knob by hundreds of thousands of steps. This is the most important lesson about tuning: the term you changed moves first, then behaviour, then the rest.

The behaviour change is smaller than most people predict. The time term was tiny: a sprint step pays 10 to 14 and the time cost was a quarter point, so it was never what made Mario hurry. Progress still pays a pixel per pixel, and the stuck rule still ends a life after 120 steps without new ground, now with the same clawback as a death. What the time term did was make standing still strictly negative. With it gone, waiting is free, so over time the agent hesitates longer in front of hazards, lives get longer, and more of them end by the in-game timer running out, which counts as a death. In a long run this can turn into the stall the first run of the day showed at a pipe in 1-2.

Every knob change is an event with a step number, so a bend in any curve can be traced back to what you did. Restoring −0.25 is another event.

Lesson 4

Set the coin weight to 50, then watch the cap

Goal. See a reward term become big enough to change what the agent wants, and see the safety cap that limits it.

Predict first

Write down: What is the most coin reward one life can earn at 50 per coin? Compare it to the clear bonus of 1,500. Will the agent start hunting coins? Could it find a way to earn coins without finishing the stage?

Steps

  1. In the panel set coin from 5 to 50. Leave the per-life cap at 20 coins.
  2. Watch the coin line in the reward components chart, and Mario at the question blocks near the start of 1-1 and at the row of coin blocks after the first pipe.
  3. After a few hundred thousand steps look at the median death x and the outcome chart.
  4. Set the weight back to 5.

What to watch

  • Whether Mario starts bumping blocks it used to run past.
  • The coin line rising, and where it flattens: 20 coins × 50 = 1,000 per life is the cap.
  • Deaths moving to lower x, or lives ending stuck near the coin blocks.
What actually happens and why

At 50 a coin, twenty coins pay 1,000: two thirds of a clear bonus, for a stretch of stage that costs nothing to reach. The rule of thumb in the knobs help is to keep any side term's average per episode well under 30% of the clear bonus, and this breaks it on purpose. Over the following updates the agent learns that the blocks are worth a jump, and the coin line climbs.

The cap is the flat top on that line. After the twentieth coin in a life, further coins pay nothing, so no single life can earn more than 1,000 from coins. That is the cap doing its job. It cannot do the whole job, because coin blocks come back when Mario dies. At 5 a coin, a life of coins pays 100, less than the death it takes to reset them, so the die-and-repeat loop loses. At 50 a coin the loop pays 1,000 against a death of 100 plus half the progress, and near the start of the stage the progress is small. The loop wins, and the agent can learn to collect the early coins and then die on purpose, which shows as deaths at low x and a falling clear rate.

That is why both the weight and the cap matter, and why the clawback is charged on every ending that is not a clear. The first design paid coins nothing for exactly this reason. Setting the weight back to 5 makes the loop unprofitable again, but the habit takes time to unlearn.

Lesson 5

Raise the entropy coefficient and watch the action bars

Goal. Turn the curiosity dial and see it in the policy's probabilities, not just in Mario.

Predict first

Write down: Which way will the ten action bars move? Will the entropy number go up or down, and toward what limit? Will Mario get better or worse in the next five minutes?

Steps

  1. Note the entropy coefficient in the panel: 0.01 by default.
  2. In the viewer press + three times. Each press multiplies the coefficient by 1.5, so three presses take it to about 0.034. Or set 0.05 in the panel.
  3. Watch the action bars and the entropy number for five minutes, and the approximate KL and clip fraction.
  4. Press - three times to return to 0.01.

What to watch

  • The action bars evening out over the next few rollouts.
  • The entropy metric rising toward its ceiling of 2.30 nats.
  • Progress and the clear rate dipping while the policy is spread out.
What actually happens and why

The learning objective includes a bonus for entropy: the more evenly spread the ten probabilities, the larger the bonus, and this coefficient scales it. Raising it pays the network to hedge, so the tallest bars shrink and the small ones grow within a handful of updates. The entropy number climbs; 2.30 nats is ten equally likely actions and it cannot go higher.

Mario gets worse for a while, because a spread-out policy takes the wrong action more often. That is the trade the dial makes: more exploration now for a better chance of finding something new. It is the tool to use when the policy has collapsed onto one button too early, or after a demotion. The opposite move, lowering it, is for the end of a stage, when you want training play to match the greedy exam. Too high for too long and the agent never commits.

The approximate KL and clip fraction spike together at the change and then settle. That pair spiking after any knob change means the change was large for the learning rate.

Lesson 6

Take the controller, set up the 1-2 pipe, hand back

Goal. Use the takeover to put the agent in a situation of your choosing and watch what it does next, without teaching it anything.

Predict first

Write down: What will the policy do in front of the pipe: jump it, stand, walk into it, walk left? Will it do the same thing twice from the same saved spot?

Steps

  1. You need a run on 1-2. Either let lesson 2's run promote (the second run of the day passed 1-1 at step 901,120), or stop it and start one on 1-2 directly: tools/stop-training.sh then tools/start-training.sh lesson6 --stage 1-2. Give a new run ten minutes before this lesson, or the policy in front of the pipe is still random.
  2. In the viewer press h. The overlay shows HUMAN. Drive with the arrows, X to run, Z to jump. Go through the underground until the corridor after the piranha plants and stop at the base of the short pipe there, at x between 1,820 and 1,880 on the overlay.
  3. Press F5 to save that spot.
  4. Press h to hand the controller back. Watch until the life ends.
  5. Press F7 to reload the spot and watch again. Do it five times and tally what happened.

What to watch

  • Whether the policy tries the jump at all, and whether it tries it the same way each time.
  • The action bars in the panel while Mario stands at the pipe.
  • That the step counter keeps running: the other fifteen environments never stopped.
What actually happens and why

This pipe is where the first run of the day spent eight million steps. Its policy had no tile window, so it could not see the pipe; it had to remember by x coordinate where to jump, and it never did. Instead it learned that standing there until the stuck rule ended the life was worth more than trying and dying, because dying late in a stage clawed back half the progress and a stall cost 20. Two fixes followed: the clawback is now charged on stalls too, and the observation gained a 7 × 5 window of solid tiles centered one tile ahead of Mario, which is on by default in the start script.

What your policy does depends on how far along it is. A young one hesitates and samples; you will see different outcomes from the same save, because training play draws the action from the probabilities. A trained one jumps the same way every time. Either way, nothing you did with the controller changed the network: steps taken while HUMAN is showing are flagged and dropped from the learning batch. The takeover is a probe, not a lesson for the agent.

Save and load only touch env 0. The episode continues from the loaded frame with its bookkeeping reset as if a new life began, which is the same mechanism every episode uses to start at the stage's first frame.

Lesson 7

Watch an exploit

Goal. See two of the red team's tricks played on the real game, read exactly what the reward paid for each, and find those same terms in the panel. The guide's red-team section has the whole list.

Predict first

Write down: Which pays more, stomping a Koopa shell over and over, or taking the 1-1 bonus pipe instead of walking the same stretch? Should either of them pay more than the honest route? What would you expect a stomp to be worth, and a pipe?

Steps

  1. Run the shell trick. A window opens at 60 frames per second and the Terminal prints one ATTEMPT: line per try:
    cd ~/mario-rl
    uv run python docs/redteam/scripts/koopa_shell_restomp.py --show
    The script tries random button sequences at the green Koopa on 1-2, so it may take several attempts before a re-stomp happens. Watch for a stomp, a kick, and a second stomp on the moving shell.
  2. Run the pipe route. It starts Mario on top of the 1-1 bonus pipe, goes down, through the coin room, and out at x = 2,616, then prints a table of every reward term for the trip:
    uv run python docs/redteam/scripts/pipe_route.py --show
  3. Read the printed totals and write them next to your predictions.
  4. Start or attach to a training run and open the panel. In the reward weights, find kill, pipe, shortcut and x. These are the same terms the scripts just printed. Each has a help icon with the plain-words line.

What to watch

  • In the shell trial: the enemy's state byte, which the script prints, going to 4 on the first stomp, back to 0 on the kick, and to 4 again on the re-stomp.
  • In the pipe route: how much the shortcut term pays, and how that compares with the pixels the walk would have paid.
  • Whether the totals depend on the version of the reward code you are running (see the reveal).
What actually happens and why

The shell. As the reward stood when the audit ran, the second stomp on the same Koopa paid a second kill, because the detector counted every change of the enemy's state into "stomped". The audit measured it on 1-2 at x ≈ 655: state 0 to 4, kicked to 0, then 0 to 4 again. A policy that does it on purpose repeats it about every three seconds, and the per-life cap of 30 kills is the only limit, so the trick is worth up to 600 a life, more with the 1-ups the shell combo eventually pays. The fix, D17, pays one kill per enemy for as long as that enemy exists, and lets the farm lock, which already zeroed score points earned while standing still, zero kills and 1-ups too.

The pipe. This one surprised the red team in the other direction. With the shortcut term capped at 800, the pipe route paid 1,093.5 for the trip and walking the same span paid 1,661.4, so the pipe was worth 568 less. The agent was not exploiting anything; it was correctly refusing a bad deal, which is why no teacher ever takes the pipe. The leak was subtler: the shortcut was not counted as progress, so the clawback on a death did not halve it, and a pipe trip that ends in a death beat a walk that ends in a death by 199. The fix, D18, lets the shortcut pay the full 1,699 pixels it replaces and adds it to the progress the clawback halves. With that, the pipe pays 1,992.5 against 1,661.4 for walking: the pipe wins by 331 plus the time it saves, which is what a shortcut should be worth.

So which pays more? Per trip, the pipe; per life, the shell farm can add up to 600 before the cap. Neither comes near a clear, which pays 1,500 plus a time bank of about 1,000 plus the flag on top of the pixels. That is the standard every finding was measured against: the audit ranked tricks by how likely PPO is to find them times how much they pay against a clear, and only the warp bootstrap and the shell farm were on a scale that changes what the learner does.

About the numbers you see. The scripts run the current reward code. If the hardening task T13 has landed, the shell trial pays one kill and the pipe route prints a shortcut of 1,699; if not, you will see the audit's numbers above. Either way the printed table is the reward's own accounting, term by term, which is the point of the exercise.

Back to the guide

Lesson 8

Three personalities

Goal. See how a table of reward weights becomes a personality, using three real runs from the evening of 26 September. Then start one yourself from the panel's personality picker and check it against the curves here.

The three tables

PersonalityProgress, per pixelA whole level (about 3,200 px)One deathA ? blockItemsA clear
Refuses to play0.1about 320−30030design values1,500 plus the time bank
Treasure hunter0.5about 1,600−300200mushroom 250, flower 400, star 500, 1-up 1,0001,500 plus the time bank
Design default1.0about 3,200−10025mushroom 150, flower 150, star 1501,500 plus the time bank

The clear bonus is the same in all three. What differs is how much the road to it pays, and how much a mistake costs on the way.

Predict first

Write down, for each personality: What is the safest way to score points? Will Mario go for the flag, or for the things along the way? Which of the three reaches the 1-1 exam first, and which reaches 1-2? Give a number of steps for each guess.

Steps

  1. Open the panel and press New run. The sheet opens on a personality card. Read all three stories, then pick Refuses to play and start it. The run's name says which personality it was born with.
  2. Watch the first 300,000 steps, about three minutes. Look at the ? block tile in the ledger and the outcomes chart. Write down what most lives end as.
  3. Leave it running and read the curves below. They come from the real runs, so your run should rhyme with the first figure, not match it. Learning runs are never the same twice.
  4. When your run passes 500,000 steps, look for a demotion event in the panel's event list, then compare with the first figure.
  5. Stop it when you have seen enough and start a Treasure hunter. Watch the power, ? block and x tiles together for a few minutes.

What happened in the real runs

0% 25% 50% 75% 100% 0 500k 1.0M 1.5M 2.0M training steps how far it gets lives ending stuck lives that clear demoted to 1-1 exam 5/5 promoted to 1-2
Refuses to play. For a million steps most lives end stuck: it walks to the first ? blocks, hits them, and stands still until the stuck timer ends the life. Progress sits near 15% of the level. At 500k the curriculum demoted it from 1-2 to 1-1. Around 1.5M one life reached the flag, the 1,500-point clear entered the returns, and within 200,000 steps half of all lives cleared. It passed the exam twice, at 1.70M and 1.80M, and was promoted to 1-2 with the same weights it started with.
0% 25% 50% 75% 100% 0 500k 1.0M 1.5M 2.0M 2.5M 3.0M training steps how far it gets lives ending stuck lives that clear demoted to 1-1 exam 5/5 promoted to 1-2
The same knobs with progress at 0.5. Five times the pay per pixel. It still parked at the blocks for a while, but got farther on 1-2 before the demotion (median progress 0.24 against 0.07), and it passed the 1-1 exam at 1.60M and 1.70M.
0% 25% 50% 75% 100% 0 2.0M 4.0M 6.0M 8.0M 10.0M training steps stage 1-2 how far it gets lives that clear lives ending in death to 1-2 back to 1-1 to 1-2 again 1-2 exam 5/5 back
Treasure hunter, 10 million steps. The band on top is the stage it was training on: green 1-1, blue 1-2, gold 1-3. It passed 1-1 at 600k, went to 1-2, was sent back to 1-1 at 1.5M, passed again at 2.7M, and then spent six million steps on 1-2, where nine lives in ten ended in death. At 8.9M it passed the 1-2 exam five for five and was promoted to 1-3. At 9.5M it was sent back to 1-2, where its clear rate had fallen while it trained on the new stage.

What to watch in your own run

  • The share of lives ending stuck against the share ending in death. A personality that fears death chooses stuck.
  • The step at which clear first rises above zero, and how fast the return jumps after it.
  • The demotion at 500k on a run started on 1-2, and whether it comes back stronger.
  • For the treasure hunter: whether it grabs the mushroom before or after it starts running.
What actually happens and why

Refuses to play is doing the arithmetic right. Under its table a whole level of walking is worth about 320 and one death costs 300. The four ? blocks near the start of 1-1 pay 120 for almost no risk, and a life that then stands still loses only the stuck penalty of 20 and half of the little progress it banked. So the best plan it can see is: hit the blocks, wait. The stuck rate of 80% is not a bug in the learner. It is the reward being obeyed.

What broke the spell was a lucky life, not a weight change. With 16 Marios trying random things, one eventually reached the flag. The clear paid 1,500 plus a time bank near 800, ten times anything it had seen. The value network learned that the far end of the level is worth a lot, the policy followed the gradient toward it, and within 200,000 steps the runs were the majority. The curriculum's exam, five greedy runs on five fixed starts, then passed twice in a row, which a demoted run must do before promotion.

The demotion is the curriculum protecting the experiment. Every 500,000 steps on a stage above 1-1, if fewer than 2% of lives clear and the median life gets less than a quarter of the way, the run is sent to the stage before, seventy percent of its Marios at first, then all of them, until the earlier exam is passed. Both runs started on 1-2 and went back to 1-1 at exactly 501,760 steps. Both came back to 1-2 stronger. A switch to keep the stage is proposed as T17, for experiments that should stay where they were started.

The treasure hunter's greed is progress in disguise. Its items are spread along the level, so chasing them moves Mario right, and at 0.5 a pixel the walk pays too. It passed 1-1 three times faster than the other two. On 1-2 it took six million steps, against two million for the design default in run live-1, because it dies a lot: nine lives in ten. The big item bonuses make risky detours worth it, and the underground is full of them. It still got there, and it is now the panel's default personality because Eric liked what it did.

None of these is the right personality. They are three answers to the question the guide keeps asking: what does the reward make worth doing? A reward you can predict is one you understand. Change one number in the picker, write down what should change, and run it.

Back to the guide