AZ-Lite

an AlphaZero-style chess engine in Python that learns from self-play

CHARTER VIII

It is taught by nobody. It is only ever played against itself.

AZ-Lite is an AlphaZero-inspired chess engine written in Python. It is deliberately small. The point was never to build something that beats a real engine, it was to build the AlphaZero idea end to end and be able to read every part of it afterwards.

The search is Monte Carlo tree search using PUCT, which is the rule that decides which move to explore next. Left alone, tree search is a brute-force method that has to look at everything. Here it is guided: a small neural network suggests which moves are worth considering, the policy, and how good a position looks, the value. The search spends its effort where the network points, and the network gets better because the search keeps finding moves it did not expect. Move scoring is done on the fly through learned move embeddings, which is what keeps the implementation compact enough to read.

It learns from nothing but itself. The engine plays its own games, writes them out as JSONL replay data, trains on that data, and saves a checkpoint. Then it does it again, now slightly stronger, and the games it generates are slightly better than the ones it trained on. There is no opening book, no database of grandmaster games, and no human evaluation function. It is handed the rules and left with them.

The whole pipeline is driven from one command line with three modes: selfplay to generate games, train to learn from them, and play to sit down against it. That matters more than it sounds. A lot of reinforcement learning code exists as fragments that only the author can assemble, and a project you cannot run end to end in a single afternoon is a project nobody else will ever learn from.

  • Python 96.6%
  • Makefile 2.2%
  • Shell 1.3%

Built from

  • Python
  • Monte Carlo tree search
  • Neural networks
  • Self-play reinforcement learning

What it is not

  • Not competitive with a real chess engine, and not trying to be. Stockfish is not the comparison; the comparison is a version of AlphaZero small enough to read in one sitting.
  • Strength is bounded by how much self-play you are willing to run. A demo trained on twenty games plays like an engine trained on twenty games.
  • Written to be readable and extensible rather than fast. Distributed self-play, a larger network and a graphical board are all left as places to extend it.

Screenshots

AZ-Lite: playing from the command line
Playing from the command line
AZ-Lite: self play generating games
Self play generating games

Roads out

Neighbouring holdings