From Search to Self-Play
Introduction
Project introduction
From Search to Self-Play
You have written every piece AlphaZero needs, including the rules, a minimax solver, the PUCT score, policy targets from visit counts, and the full loop of select, expand, evaluate and back up.
In the notebook these pieces play real games of tic-tac-toe. A minimax player gives the perfect opponent to measure against. Monte Carlo tree search with random rollouts comes first, then PUCT guided by a small PyTorch network that outputs a policy and a value, and finally the self-play loop that trains that network on its own games.
What you will see
The first draft of the search stores each value for the wrong player. Nothing crashes, yet with 200 simulations per move it loses about 9 games in 10 to a player that moves at random. The numbers inside the search show why, and one changed line fixes it.
A fresh network trained live for five short rounds of self-play goes from losing about half its games against a perfect player to losing only a handful. A network trained longer offline with the same code draws every game against perfect play and wins about 92% of its games against a random player.
Search, guided by a network
simulation 1 of 25X to move. Each number is how many simulations explored that cell.
The first simulation only expands the root.
How the notebook is organized
01
Search, break and fix
Rules, minimax, tree search with UCB, and the backup bug.
02
Add a network
PUCT with a policy prior, and a value instead of random rollouts.
03
Learn by self-play
Visit-count targets, the AlphaZero loss, live training, honest matches.
Train a tic-tac-toe player by self-play
Run the whole investigation in the browser, about 60 minutes.