A full journey through modern reinforcement learning on Snake โ from tabular Q-learning through ConvNets to a mechanistic investigation of size generalization โ with interactive notebooks, real training logs, and honest empirical results.
Final PPO agent after the 5ร5 โ 10ร10 curriculum
Interactive Notebooks
Curiosity Killed the Snake
Does the Intrinsic Curiosity Module (Pathak et al., 2017) actually help? A 2ร3ร2ร3 ablation across DQN, PPO, three reward modes, two boards, three seeds. Includes the death-oversampling trap and the terminal-mask fix.
ConvNet vs Feature DQN
Can raw grid pixels beat 24 hand-crafted features? A 30-trial W&B hyperparameter sweep, architecture walkthrough, and RND exploration ablation. ConvNet v3 reaches mean 12.32 on 10ร10 vs Feature DQN's 5.50.
A Snake Agent That Scales
Play the cross-size agent trained only on boards 6โ22 โ it navigates up to 100ร100 zero-shot. Loads the shipped 824 KB model; pick any board size and watch it go.
Size Generalization โ one agent, any board
A "size-agnostic" ConvNet (fully convolutional + global pooling, so it runs on any board) trained on 6ร6 should play 16ร16 โ same game, bigger board. It doesn't: it random-walks by board ~16. A mechanistic investigation into why, ending in a fix โ one agent, trained only on boards 6โ22, plays 100ร100 zero-shot.
Zero-shot transfer to 100ร100 ยท toward-food % (never trained above 22)
| Board (zero-shot) | 6 | 16 | 32 | 64 | 100 |
|---|---|---|---|---|---|
| single-size (6ร6) โ toward-food % | 87 | 51 | 50 | 50 | 50 |
| single-size (10ร10) โ toward-food % | 92 | 59 | 50 | 50 | 50 |
| curriculum (6โ22) โ toward-food % | 88 | โ | 96 | 98 | 98 |
toward-food % = fraction of greedy moves that reduce distance to food (50% = chance). The curriculum agent also eats ~86 food per game on 100ร100, a board it never trained on. A bigger single training board just moves the cliff outward (6ร6 โ chance by 16, 10ร10 โ by ~32); only training across a range of sizes removes it.
What we found
- Decodable โ used. The pooled representation still encodes food-direction at every size (a refit probe reads it at 0.90), yet the frozen policy can't act on it โ the food-steering margin collapses 5ร while the danger margin stays invariant.
- Probe fixes โ behaviour fixes. Architectural changes (attention pooling, coordinate channels) each improved their mechanistic probe while doing nothing โ or hurting โ real-play transfer. A cautionary tale about probe-guided design.
- Root cause is coverage, not architecture. 71โ88% of big-board play is outside the 6ร6 training support โ "far from all walls" is geometrically impossible on 6ร6 yet dominates big boards. No architectural trick fixes a policy on states it never saw.
- The fix. A size-invariant representation (egocentric relative coordinates + attention pooling โ big boards look like training near the head) plus training coverage across a size range โ clean extrapolation 4.5ร past the largest training board.
- Performance rises with board size because larger boards remove constraints: wall interactions fall 54%โ1%, collision rate ~22ร lower, survival ~90ร longer.
The final agent
Tiny (~209K params, 824 KB) and shipped in the repo. Full narrative โ every probe, ablation, and dead-end โ in FINDINGS.md; overview in size_transfer/README.
Earlier experiments
The road here โ from a tabular agent on a 5ร5 grid, through curriculum PPO that first mastered 10ร10, to ConvNets that beat hand-crafted features. (Full detail is in the two notebooks above.)
ConvNet vs hand-crafted features ยท mean food eaten
10ร10 board ยท 10k games ยท 3 seeds
16ร16 board ยท 16k games
The curriculum journey watch the replay โ
| Phase | Algorithm | Board | Max Score | Notes |
|---|---|---|---|---|
| 0 | Tabular Q-Learning | 5ร5 | 24 โ | Perfect on small board |
| 1 | Double Q-Learning | 5ร5 | 24 | Stable convergence |
| 2 | Behaviour Cloning + PPO | 8ร8 | 46 | Human demos bootstrap policy |
| 3 | PPO + Curriculum (5โ10ร10) | 10ร10 | 64 | Progressive board growth |
Curiosity (ICM), in one table
| Question | Answer |
|---|---|
| Does ICM help DQN with dense reward? | No โ |ฮ| < 0.05 |
| Does ICM rescue DQN under sparse reward? | No โ |ฮ| < 0.15 |
| Does dense shaping matter for DQN? | Yes โ ~15% score drop without it |
| Does ICM help PPO under sparse reward? | Yes โ +24% on pure_sparse 10ร10 |
| Does ICM increase state coverage? | No โ both saturate at ~99% |
ConvNet sweep findings
- n_steps=1 universally better with PER โ appeared in every top sweep config; multi-step returns hurt with prioritised replay
- ch1=64 / ch2=128 โ larger second conv layer consistently wins over ch1=32/ch2=64
- epsilon_end=0.01, gamma=0.995 โ exploit harder, value distant food more
- RND null result โ saturation on sparse boards; curiosity helps weak baselines, not well-tuned agents
Running Locally
git clone https://github.com/Saheb/rl-snake.git
cd rl-snake
uv sync
uv run marimo edit notebooks/curiosity.py
uv run marimo edit notebooks/convnet.py
uv run marimo edit notebooks/size_generalization.py # play the cross-size agent (6โ100)