A minimal implementation of the AlphaZero algorithm for training a chess bot to play at amateur level.
- Complete AlphaZero Implementation: Neural network with policy and value heads, MCTS, self-play data generation
- Chess Integration: Uses
python-chess
library for game logic and move validation - Training Infrastructure: Systematic training loop with checkpointing and progress tracking
- Configurable: Easy to adjust hyperparameters for different training scenarios
Start training with default configuration:
python main.py --train
Override configuration parameters:
python main.py --train --iterations 10 --games 50 --simulations 400
Resume training from a checkpoint:
python main.py --train --resume models/checkpoint_iter_5.pth
Play against a trained model:
python play_chess.py --model models/final_model.pth
Play as black:
python play_chess.py --model models/final_model.pth --color black
Adjust MCTS simulations:
python play_chess.py --model models/final_model.pth --simulations 500
Modify config.yaml
to adjust training parameters:
# Increase training intensity
training_loop:
num_iterations: 50
games_per_iteration: 100
# Adjust network size
network:
num_res_blocks: 20
num_channels: 512
# Change exploration
self_play:
mcts_simulations: 400
c_puct: 2.0
Each training iteration consists of:
- Self-Play Phase: The current model plays games against itself, generating training examples
- Dirichlet Noise: Random noise is added to encourage exploration of different moves
- Training Phase: The neural network is trained on collected self-play data
- Logging: Progress metrics are recorded and visualized
- Checkpointing: Model state is saved for resuming training
Training produces several outputs:
models/
: Directory containing model checkpoints and final trained modellogs/
: Directory containing training metrics and progress plotslogs/training_progress.png
: Visual progress charts updated after each iterationlogs/training_metrics_*.json
: Detailed training metrics in JSON format
The implementation consists of:
src/environment.py
: Chess game wrapper using python-chesssrc/network.py
: Neural network with ResNet-style architecturesrc/mcts.py
: Monte Carlo Tree Search implementationsrc/self_play.py
: Self-play game generationsrc/trainer.py
: Neural network training loopsrc/logger.py
: Progress tracking and visualizationmain.py
: Training orchestration scriptplay_chess.py
: Interactive chess interface for playing against the bot
- Increase MCTS simulations for stronger play (but slower training)
- More games per iteration for better data diversity