An open-source implementation of the AlphaGoZero algorithm
Python C++ TypeScript Shell Makefile CSS HTML
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cc Adds GTP & Minigui support to C++ code (#165) Apr 19, 2018
cluster Choose two players to play a match (#134) Mar 22, 2018
minigui Adds GTP & Minigui support to C++ code (#165) Apr 19, 2018
oneoffs new oneoff script to constructing PVs of a set of positions (#157) Apr 18, 2018
sgf Added comments that games are public domain Feb 1, 2018
testing Update the docs. Feb 12, 2018
tests Rotate dataset with py_func (#166) Apr 20, 2018
yomi Choose two players to play a match (#134) Mar 22, 2018
.bazelrc Add separate configs for building C++ minigo for 9x9 and 19x19 boards. ( Apr 1, 2018
.gitignore Minigui (#148) Apr 3, 2018
.pylintrc update pylint rule to ignore too-many-arguments warning Feb 24, 2018
CODE_OF_CONDUCT.md Add code of conduct. Jan 30, 2018
CONTRIBUTING.md Several python cleanups. Feb 1, 2018
LICENSE initial commit Nov 10, 2017
README.md Miscellaneous improvements as I'm going through the codebase (#135) Mar 22, 2018
RESULTS.md update results with links to new files Feb 17, 2018
WORKSPACE Write TF examples & SGF files at end of C++ selfplay (#162) Apr 17, 2018
__init__.py Several python cleanups. Feb 1, 2018
cloud_logging.py Several python cleanups. Feb 1, 2018
coords.py Add docstrings and remove convert() which is no longer necessary Feb 10, 2018
dual_net.py add a hook for echoing training performance to stdout (#153) Apr 7, 2018
evaluation.py comments (#115) Feb 26, 2018
features.py Autofmt all the python with autopep8 Jan 31, 2018
go.py Miscellaneous improvements as I'm going through the codebase (#135) Mar 22, 2018
gtp_extensions.py Adds GTP & Minigui support to C++ code (#165) Apr 19, 2018
gtp_wrapper.py Minigui (#148) Apr 3, 2018
local_rl_loop.py Merge pull request #113 from brilee/estimator_rewrite Feb 26, 2018
main.py Add flags infrastructure to main.py Apr 19, 2018
mcts.py Write TF examples & SGF files at end of C++ selfplay (#162) Apr 17, 2018
preprocessing.py Rotate dataset with py_func (#166) Apr 20, 2018
requirements-analysis.txt Added requirements-analysis.txt, added tqdm for long for loops Feb 1, 2018
requirements.txt Add flags infrastructure to main.py Apr 19, 2018
rl_loop.py Backfill global step (#151) Apr 14, 2018
rl_runner.py Update rl_loop Feb 25, 2018
selfplay_mcts.py Replace all coords calls with the new functions Feb 10, 2018
sgf_wrapper.py deduplicated sgf_parsing code in oneoff_utils (#150) Apr 5, 2018
shipname.py add function to check number of games finished since last training wa… Feb 8, 2018
strategies.py Minigui (#148) Apr 3, 2018
symmetries.py Autofmt all the python with autopep8 Jan 31, 2018
test.sh Merge branch 'master' into harness-z Feb 12, 2018
tsconfig.json Minigui (#148) Apr 3, 2018
utils.py Autofmt all the python with autopep8 Jan 31, 2018

README.md

Minigo: A minimalist Go engine modeled after AlphaGo Zero, built on MuGo

Test Dashboard

This is a pure Python implementation of a neural-network based Go AI, using TensorFlow. While inspired by DeepMind's AlphaGo algorithm, this project is not a DeepMind project nor is it affiliated with the official AlphaGo project.

This is NOT an official version of AlphaGo

Repeat, this is not the official AlphaGo program by DeepMind. This is an independent effort by Go enthusiasts to replicate the results of the AlphaGo Zero paper ("Mastering the Game of Go without Human Knowledge," Nature), with some resources generously made available by Google.

Minigo is based off of Brian Lee's "MuGo" -- a pure Python implementation of the first AlphaGo paper "Mastering the Game of Go with Deep Neural Networks and Tree Search" published in Nature. This implementation adds features and architecture changes present in the more recent AlphaGo Zero paper, "Mastering the Game of Go without Human Knowledge". More recently, this architecture was extended for Chess and Shogi in "Mastering Chess and Shogi by Self-Play with a General Reinforcement Learning Algorithm". These papers will often be abridged in Minigo documentation as AG (for AlphaGo), AGZ (for AlphaGo Zero), and AZ (for AlphaZero) respectively.

Goals of the Project

  1. Provide a clear set of learning examples using Tensorflow, Kubernetes, and Google Cloud Platform for establishing Reinforcement Learning pipelines on various hardware accelerators.

  2. Reproduce the methods of the original DeepMind AlphaGo papers as faithfully as possible, through an open-source implementation and open-source pipeline tools.

  3. Provide our data, results, and discoveries in the open to benefit the Go, machine learning, and Kubernetes communities.

An explicit non-goal of the project is to produce a competitive Go program that establishes itself as the top Go AI. Instead, we strive for a readable, understandable implementation that can benefit the community, even if that means our implementation is not as fast or efficient as possible.

While this product might produce such a strong model, we hope to focus on the process. Remember, getting there is half the fun. :)

We hope this project is an accessible way for interested developers to have access to a strong Go model with an easy-to-understand platform of python code available for extension, adaptation, etc.

If you'd like to read about our experiences training models, see RESULTS.md.

To see our guidelines for contributing, see CONTRIBUTING.md.

Getting Started

This project assumes you have the following:

The Hitchhiker's guide to python has a good intro to python development and virtualenv usage. The instructions after this point haven't been tested in environments that are not using virtualenv.

pip3 install virtualenv
pip3 install virtualenvwrapper

Install TensorFlow

First set up and enter your virtualenv and then the shared requirements:

pip3 install -r requirements.txt

Then, you'll need to choose to install the GPU or CPU tensorflow requirements:

Setting up the Environment

You may want to use a cloud project for resources. If so set:

PROJECT=foo-project

Then, running

source cluster/common.sh

will set up other environment variables defaults.

Running unit tests

BOARD_SIZE=9 python3 -m unittest discover tests

Automated Tests

To automatically test PRs, Minigo uses Prow, which is a test framework created by the Kubernetes team for testing changes in a hermetic environment. We use prow for running unit tests, linting our code, and launching our test Minigo Kubernetes clusters.

You can see the status of our automated tests by looking at the Prow and Testgrid UIs:

Basics

All commands are compatible with either Google Cloud Storage as a remote file system, or your local file system. The examples here use GCS, but local file paths will work just as well.

To use GCS, set the BUCKET_NAME variable and authenticate via gcloud login. Otherwise, all commands fetching files from GCS will hang.

For instance, this would set a bucket, authenticate, and then look for the most recent model.

export BUCKET_NAME=your_bucket;
gcloud auth application-default login
gsutil ls gs://$BUCKET_NAME/models | tail -3

Which might look like:

gs://$BUCKET_NAME/models/000193-trusty.data-00000-of-00001
gs://$BUCKET_NAME/models/000193-trusty.index
gs://$BUCKET_NAME/models/000193-trusty.meta

These three files comprise the model, and commands that take a model as an argument usually need the path to the model basename, e.g. gs://$BUCKET_NAME/models/000193-trusty

You'll need to copy them to your local disk. This fragment copies the latest model to the directory specified by MINIGO_MODELS

MINIGO_MODELS=$HOME/minigo-models
mkdir -p $MINIGO_MODELS
gsutil ls gs://$BUCKET_NAME/models | tail -3 | xargs -I{} gsutil cp "{}" $MINIGO_MODELS

Selfplay

To watch Minigo play a game, you need to specify a model. Here's an example to play using the latest model in your bucket

python rl_loop.py selfplay --readouts=$READOUTS -v 2

where READOUTS is how many searches to make per move. Timing information and statistics will be printed at each move. Setting verbosity (-v) to 3 or higher will print a board at each move.

Playing Against Minigo

Minigo uses the GTP Protocol, and you can use any gtp-compliant program with it.

# Latest model should look like: /path/to/models/000123-something
LATEST_MODEL=$(ls -d $MINIGO_MODELS/* | tail -1 | cut -f 1 -d '.')
BOARD_SIZE=19 python3 main.py gtp -l $LATEST_MODEL -r $READOUTS -v 3

(If no model is provided, it will initialize one with random values)

After some loading messages, it will display GTP engine ready, at which point it can receive commands. GTP cheatsheet:

genmove [color]             # Asks the engine to generate a move for a side
play [color] [coordinate]   # Tells the engine that a move should be played for `color` at `coordinate`
showboard                   # Asks the engine to print the board.

One way to play via GTP is to use gogui-display (which implements a UI that speaks GTP.) You can download the gogui set of tools at http://gogui.sourceforge.net/. See also documentation on interesting ways to use GTP.

gogui-twogtp -black 'python3 main.py gtp -l gs://$BUCKET_NAME/models/000000-bootstrap' -white 'gogui-display' -size 19 -komi 7.5 -verbose -auto

Another way to play via GTP is to watch it play against GnuGo, while spectating the games

BLACK="gnugo --mode gtp"
WHITE="python3 main.py gtp -l path/to/model"
TWOGTP="gogui-twogtp -black \"$BLACK\" -white \"$WHITE\" -games 10 \
  -size 19 -alternate -sgffile gnugo"
gogui -size 19 -program "$TWOGTP" -computer-both -auto

Training Minigo

Overview

The following sequence of commands will allow you to do one iteration of reinforcement learning on 9x9. These are the basic commands used to produce the models and games referenced above.

The commands are

  • bootstrap: initializes a random model
  • selfplay: plays games with the latest model, producing data used for training
  • gather: groups games played with the same model into larger files of tfexamples.
  • train: trains a new model with the selfplay results from the most recent N generations.

Bootstrap

This command creates a random model, which appears at . gs://$BUCKET_NAME/models/$MODEL_NAME(.index|.meta|.data-00000-of-00001)

export MODEL_NAME=000000-bootstrap
python3 main.py bootstrap --model-save-path="gs://$BUCKET_NAME/models/$MODEL_NAME"

Self-play

This command starts self-playing, outputting its raw game data in a tensorflow-compatible format as well as in SGF form in the directories

gs://$BUCKET_NAME/data/selfplay/$MODEL_NAME/local_worker/*.tfrecord.zz
gs://$BUCKET_NAME/sgf/$MODEL_NAME/local_worker/*.sgf
BOARD_SIZE=19 python3 main.py selfplay gs://$BUCKET_NAME/models/$MODEL_NAME \
  --readouts 10 \
  -v 3 \
  --output-dir=gs://$BUCKET_NAME/data/selfplay/$MODEL_NAME/local_worker \
  --output-sgf=gs://$BUCKET_NAME/sgf/$MODEL_NAME/local_worker

Gather

python3 main.py gather

This command takes multiple tfrecord.zz files (which will probably be KBs in size) and shuffles them into tfrecord.zz files that are ~100 MB in size.

Gathering is done according to model numbers, so that games generated by one model stay together. By default, rl_loop.py will use directories specified by the environment variable BUCKET_NAME, set at the top of rl_loop.py.

gs://$BUCKET_NAME/data/training_chunks/$MODEL_NAME-{chunk_number}.tfrecord.zz

The file gs://$BUCKET_NAME/data/training_chunks/meta.txtis used to keep track of which games have been processed so far. (more about this needed)

python3 main.py gather \
  --input-directory=gs://$BUCKET_NAME/data/selfplay \
  --output-directory=gs://$BUCKET_NAME/data/training_chunks

Training

This command finds the most recent 50 models' training chunks and trains a new model, starting from the latest model weights.

Run the training job:

python3 main.py train gs://$BUCKET_NAME/data/training_chunks \
    gs://$BUCKET_NAME/models/000001-somename \
    --load-file=gs://$BUCKET_NAME/models/000000-bootstrap \
    --generation-num=1 \
    --logdir=path/to/tensorboard/logs \

The updated model weights will be saved at the end. (TODO: implement some sort of local checkpointing based on global_step that will resume appropriately.)

Additionally, you can follow along with the training progress with TensorBoard - if you give each run a different name (logs/my_training_run, logs/my_training_run2), you can overlay the runs on top of each other.

tensorboard --logdir=path/to/tensorboard/logs/

Validation

It can be useful to set aside some games to use as a 'validation set' for tracking the model overfitting. One way to do this is with the validate command.

Validating on holdout data

By default, Minigo will hold out 5% of selfplay games for validation, and write them to gs://$BUCKET_NAME/data/holdout/<model_name>. This can be changed by adjusting the holdout-pct flag on the selfplay command.

With this setup, python rl_loop.py validate --logdir=<logdir> -- will figure out the most recent model, grab the holdout data from the fifty models prior to that one, and calculate the validation error, writing the tensorboard logs to logdir.

Validating on a different set of data

This might be useful if you have some known set of 'good data' to test your network against, e.g., a set of pro games. Assuming you've got a set of .sgfs with the proper komi & boardsizes, you'll want to preprocess them into the .tfrecord files, by running something similar to

import preprocessing
filenames = [generate a list of filenames here]
for f in filenames:
     try:
         preprocessing.make_dataset_from_sgf(f, f.replace(".sgf", ".tfrecord.zz"))
     except:
         print(f)

Once you've collected all the files in a directory, producing validation is as easy as

BOARD_SIZE=19 python main.py validate path/to/validation/files/ --load-file=/path/to/model
--logdir=path/to/tb/logs --num-steps=<number of positions to run validation on>

the main.py validate command will glob all the .tfrecord.zz files under the directories given as positional arguments and compute the validation error for num_steps * TRAINING_BATCH_SIZE positions from those files.

Running Minigo on a Kubernetes Cluster

See more at cluster/README.md