Coder Social home page Coder Social logo

alibizhan / fastchess Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomasahle/fastchess

0.0 0.0 0.0 65.32 MB

Predicts the best chess move with 27.5% accuracy by a single matrix multiplication

License: GNU General Public License v3.0

Python 100.00%

fastchess's Introduction

Teaching FastText to play Chess

FastChess is a chess engine predicting the next move using the http://fastText.cc text classification library. In other words, it is a simple one-layer + soft-max model, taking the board state as a vector and outputting a vector of probabilities for each possible move.

The project also contains a Monte Carlo Tree Search, following by Alpha Zero, which combines with the simple linear model to provide a higher quality of play.

You can play against FastChess on Lichess: https://lichess.org/@/fastchess-engine (requires log-in). It's current rating (January 2021) is 2052 Bullet and 1901 Blitz.

Screenshot

Screenshot

Run it!

You'll need the following libraries:

pip install git+https://github.com/facebookresearch/fastText.git
pip install git+https://github.com/niklasf/python-chess.git
pip install numpy

Afterwards you can play by

$ python play_chess.py
Do you want to be white or black? white
  8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
  7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
  6
  5
  4
  3
  2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
  1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
    a b c d e f g h

Your move (e.g. Nf3 or d2d4): e4

To disable MCTS and play directly against the fastText model, add the -no-mcts argument. See python play_chess.py --help for more options.

Train the model

There are two ways to train the model. The first one is to download a set of pgn files, like https://storage.lczero.org/files/training_pgns/ and run

python proc.py 'ccrl/**/*.pgn' -test proc.test -train proc.train
fasttext supervised -input proc.train -output proc.model -t 0 -neg 0 -epoch 1
fasttext test proc.model.bin proc.test 1
mv proc.model.bin model.bin
python play_chess.py -selfplay

The other way is to generate your own data, e.g. using stockfish. You can train your own model as:

python make_data.py -games 1000 | shuf > g1000
/opt/fastText/fasttext supervised -input g1000 -output model -t 0 -neg 0 -epoch 4

And then run the program as:

python play_chess.py model.bin

You can also generate more data by self-play (as default games are generated by stockfish)

python make_data.py -games 1000 -selfplay model.bin > games

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.