Search

Search papers and lessons

AlphaZero

Level 4 · 12 min read

AlphaZero learns a board game from self-play alone: one network proposes moves and predicts winners, and tree search uses those guesses to produce better training targets.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Reinforcement learning
  • Linear algebra
  • Architectures
  • Neural network basics
  • Sign in to see your progress

TL;DR

AlphaZero plays a board game by combining two things: a neural network that suggests which moves look good and who is likely to win, and a tree search that checks those suggestions by looking ahead. It learns only from games it plays against itself, with no human games and no hand-written strategy. The search makes the network's move choices stronger than the raw network, and the network makes the search cheaper, so each improves the other.

Where you'll see it: in the self-play training recipe behind strong game programs for chess, shogi and Go.

Intuition

Think of a chess player who has two habits. The first is a gut feeling: looking at a position, they sense which moves are promising and how the game is likely to end. The second is calculation: they look a few moves ahead and test their gut feeling against what happens. Early on, the gut is poor and the calculation is short. Each game they play is a lesson. If calculation shows that a gut-favoured move was a mistake, the gut should be corrected. If calculation confirms a surprising move, the gut should be pushed toward it.

AlphaZero is this loop, automated. The gut feeling is a single neural network with two outputs. The calculation is a Monte Carlo tree search that uses the network to guide which lines to explore and to score the positions it reaches. Every move in every self-played game is decided by search, and the search result becomes the training target for the network. The network is then trained to predict both the search's choice and the eventual winner.

Why this design? Older game programs either searched with a hand-written evaluation (chess engines of the 2000s) or learned an evaluation from human games. Hand-written evaluations need expert effort for every new game. Learning from human games caps the strength at the level of the human games. AlphaZero asks only for the rules of the game and a way to measure who won.

flowchart LR
  N[Network: move priors p and value v] --> S[Tree search guided by p, scored by v]
  S --> M[Chosen move and visit counts pi]
  M --> G[Play self-play game to the end]
  G --> T[Training targets: pi and winner z]
  T --> U[Update network weights]
  U --> N

The math

Setting

The game is a sequence of positions ss. In each position the player to move chooses an action aa from the legal moves. The game ends with an outcome z∈{−1,0,+1}z \in \{-1, 0, +1\} seen from the point of view of the player who was to move in position ss: +1+1 for a win, 00 for a draw, −1-1 for a loss. The rules are the only domain knowledge the method needs.

The network fθf_\theta takes a position and returns two things:

(p,v)=fθ(s),p=(pa)a,v∈[−1,1].(\mathbf{p}, v) = f_\theta(s), \qquad \mathbf{p} = (p_a)_{a}, \qquad v \in [-1, 1].

Here p\mathbf{p} is a probability vector over legal moves (the policy head, with ∑apa=1\sum_a p_a = 1), and vv is the predicted outcome for the player to move (the value head). The two heads share a trunk of convolutional layers with residual connections, which is why the network is a convolutional network at heart. The chess and shogi versions read the board as a stack of planes, one plane per piece type, so that the same local patterns can be detected anywhere on the board.

The search with the network inside

Each node ss in the search tree stores, for every legal action aa:

  • a visit count N(s,a)N(s, a), the number of simulations that took that action from ss,
  • a total value W(s,a)W(s, a), the sum of the value estimates backed up through that action,
  • the mean value Q(s,a)=W(s,a)/N(s,a)Q(s, a) = W(s, a) / N(s, a),
  • the prior P(s,a)=paP(s, a) = p_a from the network, computed once when ss is first expanded.

Let N=∑bN(s,b)N = \sum_b N(s, b) be the total visits to ss. Selection at ss picks the action with the largest score

a∗=arg⁡max⁡a[Q(s,a)+cpuct P(s,a) N1+N(s,a)].a^{\ast} = \arg\max_{a} \left[ Q(s, a) + c_{\text{puct}}\, P(s, a)\, \frac{\sqrt{N}}{1 + N(s, a)} \right].

The first term favours actions that have worked well. The second term favours actions the network likes and that have been tried few times. The constant cpuctc_{\text{puct}} sets how much exploration matters. This is the same exploit-versus-explore trade-off as the UCT rule in Monte Carlo Tree Search, with the uniform bandit prior replaced by the network's policy.

When a simulation reaches a new leaf sLs_L, the network evaluates it once, giving vv. That value is backed up along the path: each action on the path gains one visit and adds vv to WW, with the sign flipped at each step because the players alternate. Nothing is rolled out to the end of the game. The value head replaces the random playout used in plain MCTS.

After a fixed number of simulations at the root s0s_0, the search outputs a policy from the visit counts:

π(a∣s0)=N(s0,a)1/τ∑bN(s0,b)1/τ.\pi(a \mid s_0) = \frac{N(s_0, a)^{1/\tau}}{\sum_b N(s_0, b)^{1/\tau}}.

The temperature τ\tau controls how sharply moves are sampled from the visit counts. At τ=1\tau = 1 moves are chosen in proportion to visits, which keeps self-play varied; as τ→0\tau \to 0 the most-visited move is played. (The arXiv paper states that moves are selected in proportion to root visit counts; annealing τ\tau later in a game is the AlphaGo Zero practice and is an assumption here.) The vector π\pi is a better policy than p\mathbf{p} alone, because it has been corrected by lookahead. That is the key idea: search turns the network's rough guess into a sharper target.

Training

Each self-play game produces, for every position sts_t visited, a training example (st,πt,zt)(s_t, \pi_t, z_t). Here πt\pi_t is the search policy at that move and ztz_t is the final outcome seen from the player to move at sts_t. The network is trained on these examples with one loss:

L(θ)=(z−v)2−π⊤log⁡p+c ∥θ∥2.\mathcal{L}(\theta) = \big(z - v\big)^2 - \boldsymbol{\pi}^{\top} \log \mathbf{p} + c\,\lVert \theta \rVert^2.

Read it as three terms. The first pulls the value head toward the actual game result. The second, a cross-entropy, pulls the policy head toward the search's visit distribution. The third is weight decay with strength cc, which keeps the weights small. Both the value and the policy are supervised by the outcome of the search and the game, so no human labels are needed.

Note that the search generates the policy target, and the game outcome generates the value target. Neither depends on any human play.

Worked examples

Example
One PUCT selection step

At a node with 10 total visits, the root has three legal moves with these statistics. Use cpuct=1.5c_{\text{puct}} = 1.5. This constant is chosen for the example, not a value from the paper.

MovePrior PPVisits N(a)N(a)Wins W(a)W(a)Q=W/NQ = W/N
A0.5630.500
B0.3320.667
C0.2100.000
  1. Compute N=10≈3.1623\sqrt{N} = \sqrt{10} \approx 3.1623.
  2. Move A: exploration term 1.5⋅0.5⋅3.1623/(1+6)=2.3717/7≈0.33881.5 \cdot 0.5 \cdot 3.1623 / (1 + 6) = 2.3717 / 7 \approx 0.3388. Score 0.5000+0.3388=0.83880.5000 + 0.3388 = 0.8388.
  3. Move B: exploration term 1.5⋅0.3⋅3.1623/(1+3)=1.4230/4≈0.35581.5 \cdot 0.3 \cdot 3.1623 / (1 + 3) = 1.4230 / 4 \approx 0.3558. Score 0.6667+0.3558=1.02240.6667 + 0.3558 = 1.0224.
  4. Move C: exploration term 1.5⋅0.2⋅3.1623/(1+1)=0.9487/2≈0.47431.5 \cdot 0.2 \cdot 3.1623 / (1 + 1) = 0.9487 / 2 \approx 0.4743. Score 0.0000+0.4743=0.47430.0000 + 0.4743 = 0.4743.
  5. The largest score is B, so the next simulation goes through B.

Interpretation: B has the best record and a respectable prior, and it has few visits, so the search keeps probing it. C has the largest exploration bonus because it has the fewest visits, but its estimated value of 0 leaves it with the lowest score. A has the highest prior but is already well explored, so its bonus has shrunk.

Example
Turning visit counts into a training target

After 100 simulations at a root, three legal moves have these visit counts: A = 60, B = 30, C = 10. The network's policy head gave priors p=(0.5,0.3,0.2)\mathbf{p} = (0.5, 0.3, 0.2) and its value head predicted v=0.2v = 0.2. The game was later won by the player to move, so z=+1z = +1. Use τ=1\tau = 1.

  1. The search policy is π=(60,30,10)/100=(0.6,0.3,0.1)\pi = (60, 30, 10) / 100 = (0.6, 0.3, 0.1).
  2. Value error: (z−v)2=(1−0.2)2=0.64(z - v)^2 = (1 - 0.2)^2 = 0.64.
  3. Policy cross-entropy: −∑aπalog⁡pa=−(0.6ln⁡0.5+0.3ln⁡0.3+0.1ln⁡0.2)-\sum_a \pi_a \log p_a = -(0.6 \ln 0.5 + 0.3 \ln 0.3 + 0.1 \ln 0.2).
  4. Evaluate the logs: ln⁡0.5≈−0.6931\ln 0.5 \approx -0.6931, ln⁡0.3≈−1.2040\ln 0.3 \approx -1.2040, ln⁡0.2≈−1.6094\ln 0.2 \approx -1.6094.
  5. The sum is 0.6(−0.6931)+0.3(−1.2040)+0.1(−1.6094)≈−0.4159−0.3612−0.1609=−0.93800.6(-0.6931) + 0.3(-1.2040) + 0.1(-1.6094) \approx -0.4159 - 0.3612 - 0.1609 = -0.9380, so the cross-entropy term is 0.93800.9380.
  6. Total loss for this example, before weight decay: 0.64+0.938=1.5780.64 + 0.938 = 1.578.

Interpretation: the network put only 0.5 on move A, but the search put 0.6 there. The policy term is what moves the network toward the search's choice. The value term is large because the network was unsure (v=0.2v = 0.2) and the game was won, so the value head is pushed up for similar positions.

024681002468child visits nexploration term
  • prior 0.5
  • prior 0.1
With 100 parent visits, the exploration bonus shrinks as a child is visited, and it shrinks faster for a child with a low prior. Illustrative with c_puct = 1.5.

Tradeoffs and failure modes

SituationProblemBetter choice
Very large action space (for example Go with many legal points)Visiting each child at least once is costly, so the prior matters a lotKeep the prior strong and tune cpuctc_{\text{puct}}, or use progressive widening
Network is poor early in trainingSearch with a bad prior wastes simulations on bad movesKeep the temperature high in early moves, and rely on many simulations per move
Stochastic games (dice, hidden information)Plain tree search assumes known transitions, and the value target is noisyUse methods designed for chance nodes or imperfect information instead
Game is small and solvable by brute forceA neural network adds cost without benefitExact search such as [[minimax-game
Environment has no simulatorSearch needs to roll the state forward, and the rules are not availableLearn a model of the environment first, as in Model-Based Reinforcement Learning and MuZero
Self-play converges to a narrow styleThe training data stops covering other positionsAdd noise to the root priors during self-play, which the method already uses for exploration

The method also depends heavily on compute. Each training move runs many network evaluations, so the cost is set by how many simulations are run per move, not only by the size of the network. Reported strength came from very large amounts of self-play, and a small reproduction usually gives a weaker player.

Another failure mode is that the search can only be as good as the network's values. If the value head systematically misjudges some kind of position, the search inherits that error and passes it into the training target. The loop corrects errors that show up in games, but it can take a long time to correct errors that the self-play never reaches.

History

Silver and colleagues at DeepMind had built AlphaGo, which beat a top human Go player in 2016. AlphaGo used human game records to start training, then reinforcement learning, and a search guided by networks. Its successor, AlphaGo Zero, removed the human records. It learned Go from self-play alone, with a single network that had both a policy head and a value head. The change showed that human data was not required to reach the top level in Go.

AlphaZero generalised that approach. The same algorithm, with the same network design and the same search, was applied to chess, shogi and Go, with only the game rules changed. The preprint appeared in late 2017 and the peer-reviewed version followed in 2018 in the journal Science. It caught on because it dropped the need for human knowledge in chess and shogi, where hand-written engines were strong, and because one algorithm covered three games.

Check yourself

QuestionRecall: what does the policy head of the AlphaZero network output, and what does the value head output?Show answer

The policy head outputs a probability for each legal move, which the search uses as a prior. The value head outputs a single number in [−1,1][-1, 1], the predicted outcome for the player to move. Together they replace the hand-written evaluation and the random playouts of plain MCTS.

QuestionComputation: a node has N=16N = 16 total visits, and a move has prior P=0.25P = 0.25, N(a)=3N(a) = 3, W(a)=1.5W(a) = 1.5. With cpuct=2c_{\text{puct}} = 2, what is its PUCT score?Show answer

The mean is Q=1.5/3=0.5Q = 1.5 / 3 = 0.5. The exploration term is 2⋅0.25⋅16/(1+3)=2⋅0.25⋅4/4=0.52 \cdot 0.25 \cdot \sqrt{16} / (1 + 3) = 2 \cdot 0.25 \cdot 4 / 4 = 0.5. The score is 0.5+0.5=1.00.5 + 0.5 = 1.0.

QuestionWhy is the policy target taken from visit counts rather than from the network's own output?Show answer

The network output is a guess made in one forward pass. The visit counts come from many simulations that each check a move against the network's values, so the counts favour moves that held up under lookahead. Training the policy head toward them moves the network toward better moves than it could produce alone. This is the source of the improvement loop.

QuestionWhy does the value target come from the game result rather than from the search value?Show answer

The game result is a true outcome, so it is not limited by the network's own errors. Using the search value alone would teach the network to agree with its own search, with no outside signal. Using the outcome gives the value head a target that reflects what actually happened in the game.

QuestionApplied: you want to apply the AlphaZero recipe to a card game with hidden hands. Which parts of the method break down, and what would you change first?Show answer

Tree search with known next states assumes the player can see the full state. Hidden hands mean the state is not fully known, so the search needs to handle information sets rather than single positions. The first change would be to replace the search with a method that handles imperfect information, while keeping the self-play loop and the two-headed network. Without that change, the search would plan against cards the opponent may not hold.

Sources