Search guided by a learned network
Value iteration plans with a perfect model of the world; DQN and PPO learn without planning at all. AlphaZero does both. For a game whose rules it knows, it searches ahead with Monte Carlo tree search (MCTS), and a neural network tells the search which moves look promising and how good a position is. The search's conclusions then become the network's training targets, so each one improves the other. Nothing else goes in: no human games, no hand-written evaluation, only the rules and games against itself.
This page runs it on Connect Four, with the game written here on two bitboards (pytorch/rl_alphazero.py). The network is small: three residual blocks of 48 channels, about 130,000 parameters, with a policy head giving a prior over the seven columns and a value head giving , the expected result for the player to move.
One simulation
The search grows a tree from the current position. Each simulation walks down from the root, at each node taking the move with the highest score
where counts visits, is the mean value found below that move so far, and . The first term exploits what the search has learned; the second favours moves the network likes and the search has tried little, and shrinks as they are visited. At a position not yet in the tree the walk stops, the network scores it (prior and value), and the value is passed back up the path, changing sign at each level because the players alternate. A finished game is scored by its result instead.
After many simulations the visit counts at the root are the search's answer: the move to play is the most visited, and the distribution of visits is a better policy than the prior it started from. The Visits as the search grows chart shows this at two fixed positions, from 1 to 1,024 simulations, with the prior as dots on the left.
Two positions to watch
On the empty board, Connect Four is a known first-player win with perfect play, starting in the middle column. In a threat to block, the first player has three in a row on the bottom row and the second player must drop in column 6 or lose on the next move. Search can find the block without any knowledge: every other move leads to a position where the opponent wins at once, so those moves get and stop being visited. The network, in contrast, has to learn to see it. Switch between the untrained network and the final one to compare how much the prior and the search each contribute. With the untrained network, whose prior is close to uniform, 1,024 simulations still put 973 visits on the block, all from the search. After 30 generations the prior alone gives the block 68 %, and on the empty board 90 % to the middle column, where perfect play starts; its value of the empty board is +0.17 for the first player.
Self-play and training
Each generation plays 100 games of the current network against itself, 80 simulations per move. Dirichlet noise is mixed into the root's prior so that self-play keeps trying new moves, and the first 8 moves of each game are sampled in proportion to the visit counts rather than played greedily, so the games differ. Every position is stored with its visit distribution and the game's final result for the player to move, and again mirrored left to right. The network is then trained on the last four generations' positions with
so the value head learns to predict who wins and the policy head learns to predict where the search will go. The Over 30 generations heat maps show the prior and the search's visits at the chosen position, one row per generation, and the curve below them the value head.
How strong it got
Each generation is evaluated against three fixed opponents over 20 games, half of them moving first: a player that picks uniformly random legal columns, and minimax searching 2 and 4 plies that only counts wins and losses (it never misses a win or a loss within its horizon, and breaks ties at random). The Play strength chart shows the score (a win counts 1, a draw ½) with search and, dashed, for the network alone playing its highest-prior move.
A small network trained for 30 generations on a CPU is far from strong, and the chart says so honestly. The search, at 80 simulations, beats the random player in essentially every game from generation 1, and wins most games against both minimax players from generation 2 on (generation 30: 19–0–1 against 2 plies, 15–1–4 against 4). That comes early because the tree itself finds wins and losses a few moves deep, as minimax does, and looks further along the lines the prior suggests; training then adds little against these opponents. The network on its own shows the learning: against 4-ply minimax it loses all 20 games untrained, and wins 8 to 13 of 20 in the later generations. With 20 games per point, a score moves by about ±0.1 from one generation to the next by chance alone.
Training itself is steady: the policy loss (the cross-entropy against the visit counts, averaged over each generation's steps) falls from 1.92 to 1.12 over the 30 generations, while the value loss moves between 0.34 and 0.62 without a trend, because the games it learns from change as play improves.
Where it came from
MCTS with the upper-confidence rule came from Kocsis and Szepesvári and from Coulom in 2006, and Rosin's PUCT added the prior. AlphaGo (2016) combined it with networks first trained on human games; AlphaGo Zero (2017) removed the human data, and AlphaZero (2018) applied the same algorithm unchanged to chess, shogi and Go. Expert Iteration (Anthony et al.) published the same idea of search as a teacher at the same time. MuZero later learned the rules too. The search-over-steps idea returns in language models in the Reasoning Explainer's Tree search tab.
Papers
- A general reinforcement learning algorithm that masters chess, shogi, and Go through self-play AlphaZero: one network, PUCT search and self-play from the rules alone, the algorithm this page runs.
- Mastering the game of Go without human knowledge AlphaGo Zero: the combined policy-value residual network and the loss on visit counts and game results.
- Mastering the game of Go with deep neural networks and tree search AlphaGo: MCTS guided by policy and value networks, before self-play replaced human games.
- Bandit based Monte-Carlo Planning UCT: tree search that treats each node's moves as a bandit, the ancestor of PUCT's exploration bonus.
- Multi-armed bandits with episode context PUCB, the prior-weighted bonus that AlphaGo's PUCT selection rule adapts.
- Thinking Fast and Slow with Deep Learning and Tree Search Expert Iteration: tree search as the teacher of a network that in turn guides the search.
- Mastering Atari, Go, Chess and Shogi by Planning with a Learned Model MuZero: the same search, with a learned model of the game's dynamics instead of its rules.