๐Ÿ RL Snake

GitHub

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.

PPO agent mastering 10ร—10 Snake after curriculum training

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.

Open in marimo

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.

Open in marimo

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.

Open in Colab

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)

curriculum (6โ€“22)
98%
single-size
50% (chance)
Board (zero-shot)6163264100
single-size (6ร—6) โ€” toward-food %8751505050
single-size (10ร—10) โ€” toward-food %9259505050
curriculum (6โ€“22) โ€” toward-food %88โ€”969898

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

The final agent

Cross-size agent architecture: egocentric input, attention pool, dueling head

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

ConvNet v3
12.32
ConvNet+RND
11.96
Feature DQN
5.50
ConvNet v1
1.49

16ร—16 board ยท 16k games

ConvNet v3
10.83
Feature DQN
5.92
ConvNet v1
0.20

The curriculum journey  watch the replay โ†’

PhaseAlgorithmBoardMax ScoreNotes
0Tabular Q-Learning5ร—524 โœ“Perfect on small board
1Double Q-Learning5ร—524Stable convergence
2Behaviour Cloning + PPO8ร—846Human demos bootstrap policy
3PPO + Curriculum (5โ†’10ร—10)10ร—1064Progressive board growth

Curiosity (ICM), in one table

QuestionAnswer
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

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)