Skip to content

Matcraft94/chess_AI

Repository files navigation

Chess AI - AlphaZero-Style Neural Network

A chess engine powered by deep reinforcement learning, implementing an AlphaZero-style architecture with Monte Carlo Tree Search (MCTS).

Project Background

This project started as a personal learning journey into neural networks and deep learning. After studying the fundamentals, I became fascinated with the idea of creating an AI that could learn to play chess from scratch - not through handcrafted rules, but by learning patterns and strategies on its own.

The initial development was limited by computational constraints, but with a new machine (RTX 5070 Ti), I was finally able to complete the training pipeline and see the project come to life.

Features

  • AlphaZero-style architecture: Residual neural network with policy and value heads
  • Monte Carlo Tree Search: Strategic move selection through simulation
  • Two-phase training: Supervised learning from master games + self-play reinforcement
  • Optimized data pipeline: Parallel PGN preprocessing for fast training iterations
  • Visual interface: Pygame GUI with evaluation bar to play against the trained model
  • Configurable: YAML-based configuration for easy experimentation

Architecture

Neural Network

The model uses a residual convolutional neural network:

  • Input: 12-channel tensor (6 piece types × 2 colors) on 8×8 board
  • Backbone: Configurable residual blocks (default: 5 blocks, 128 channels)
  • Policy head: 4096-dimensional output (64×64 possible from-to moves)
  • Value head: Single scalar [-1, 1] for position evaluation

Training Pipeline

  1. Supervised Learning: Learn basic patterns from high-rated Lichess games
  2. Self-Play: Improve through games against itself using MCTS
  3. Continuous Improvement: Iterative self-play with updated models

Installation

# Clone the repository
git clone https://github.com/Matcraft94/chess_AI.git
cd chess_AI

# Create virtual environment
python -m venv venv
venv\Scripts\activate  # Windows
# source venv/bin/activate  # Linux/Mac

# Install dependencies
pip install -r requirements.txt

# Install PyTorch with CUDA (adjust for your CUDA version)
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu130

Usage

1. Prepare Training Data

Download PGN files from Lichess Database and preprocess them:

# Preprocess PGN to optimized format
python preprocess_pgn.py data/lichess_games.pgn -o data/processed/ -n 50000 --min-elo 1800

2. Configure Training

Edit config.yaml to adjust:

  • Model architecture (residual blocks, channels)
  • Training parameters (batch size, learning rate, epochs)
  • Data paths
  • Hardware settings

3. Train the Model

# Full training (supervised + self-play)
python train.py

# Only supervised learning
python train.py --supervised-only

# Only self-play (requires pretrained model)
python train.py --selfplay-only --pretrained saved_models/supervised.ckpt

Monitor training with TensorBoard:

tensorboard --logdir logs

4. Play Against the AI

# Launch GUI (loads best model automatically)
python play_chess_gui.py

# Play as black
python play_chess_gui.py --black

Controls:

  • Click to select and move pieces
  • R - Restart game
  • F - Flip colors
  • Q - Quit

Project Structure

chess_AI/
├── src/
│   ├── models.py          # Neural network architecture
│   ├── environment.py     # Chess game state management
│   ├── mcts.py            # Monte Carlo Tree Search
│   ├── dataset.py         # Data loading utilities
│   ├── lightning_module.py # PyTorch Lightning modules
│   └── utils.py           # Helper functions
├── train.py               # Main training script
├── preprocess_pgn.py      # PGN to tensor conversion
├── play_chess_gui.py      # Pygame interface
├── config.yaml            # Training configuration
└── requirements.txt       # Dependencies

Configuration

Key parameters in config.yaml:

model:
  num_residual_blocks: 5    # Depth of the network
  num_channels: 128         # Width of the network

supervised:
  batch_size: 512
  learning_rate: 0.001
  max_epochs: 50

selfplay:
  num_iterations: 50
  num_games_per_iteration: 100
  mcts_simulations: 50

Technical Details

Board Representation

The board is encoded as a 12×8×8 tensor:

  • 6 channels for white pieces (pawn, knight, bishop, rook, queen, king)
  • 6 channels for black pieces
  • Each channel is a binary 8×8 grid indicating piece positions

Move Encoding

Moves are encoded as a 4096-dimensional vector:

  • Index = from_square × 64 + to_square
  • Promotions default to queen (most common case)

MCTS Algorithm

  1. Selection: Traverse tree using UCB score
  2. Expansion: Add new node using neural network policy
  3. Evaluation: Get position value from neural network
  4. Backpropagation: Update visit counts and values

Hardware Requirements

  • Minimum: 8GB RAM, modern CPU
  • Recommended: 16GB RAM, NVIDIA GPU with 8GB+ VRAM
  • Optimal: 32GB RAM, RTX 5070 Ti or better

Training time estimates (50 epochs supervised + 50 iterations self-play):

  • RTX 5070 Ti: ~8-10 hours

Results and Observations

The model learns progressively:

  • After supervised learning: Basic opening principles, piece development, simple tactics
  • After self-play: Improved positional understanding, deeper tactical calculations

Current limitations:

  • Endgame play could be stronger (requires more training data)
  • Opening book integration would improve early game

Future Improvements

  • Implement opening book for consistent openings
  • Add endgame tablebases for perfect endgame play
  • Experiment with transformer architectures
  • Implement distributed training for faster iteration
  • Add time controls and UCI protocol support

References

License

MIT License - feel free to use, modify, and distribute.

Acknowledgments

This project was a fantastic learning experience in deep learning, reinforcement learning, and game AI. Special thanks to the open-source community for the tools and resources that made this possible.


Built with passion for chess and neural networks

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages