In chapter 7 of my (+ Max’s) book, Deep Learning and the Game of Go, there is an example that collects a sample of human go game records, encodes them, and trains a small neural network. This is followed by a sample output showing it achieving over 90% training set accuracy.

Several readers have reported that they could not reproduce this. You’re not crazy! There is a mismatch between the code listing in the book and the results listing. Unfortunately, we haven’t been able to recover the exact settings that produced the result. However, I have found some settings that give you high accuracy on a small training set.

tl;dr

In train_generator.py, set num_games = 1000 and num_epochs = 50. This should get you training set accuracy over 80%.

Detailed steps to reproduce

Check out the book’s code from GitHub, and create a clean virtualenv. Next install the dependencies:

# If you have CUDA drivers configured already,
# install tensorflow-gpu instead of tensorflow
pip install tensorflow==1.14.0 keras==2.2.5

Now check out the chapter_7 branch:

git checkout chapter_7

Then run the (updated) example:

cd code/examples
PYTHONPATH=.. python train_generator_better.py

You should get something similar to these results:

1480/1480 [==============================] - 27s 18ms/step -
 loss: 5.8746 - acc: 0.0037 - val_loss: 5.8506 - val_acc: 0.0036
Epoch 2/50
1480/1480 [==============================] - 25s 17ms/step -
 loss: 5.7754 - acc: 0.0048 - val_loss: 5.6919 - val_acc: 0.0066
Epoch 3/50
1480/1480 [==============================] - 26s 17ms/step -
 loss: 5.6483 - acc: 0.0072 - val_loss: 5.5923 - val_acc: 0.0093

[snip snip]

Epoch 48/50
1480/1480 [==============================] - 26s 18ms/step -
 loss: 0.6825 - acc: 0.8170 - val_loss: 1.3805 - val_acc: 0.6379
Epoch 49/50
1480/1480 [==============================] - 25s 17ms/step -
 loss: 0.6576 - acc: 0.8240 - val_loss: 1.3257 - val_acc: 0.6533
Epoch 50/50
1480/1480 [==============================] - 25s 17ms/step -
 loss: 0.6196 - acc: 0.8360 - val_loss: 1.2520 - val_acc: 0.6686

OK, what is going on? Here are the main problems we’ve seen:

More games and epochs

The listing in the book specifies a 100 game sample and 5 epochs. That’s just not enough! The modified example picks a 1000 game sample and runs for 50 epochs.

Clear the .npy files between runs

The game collection script caches the training data on disk to speed up future runs. However, if you change settings such as num_games, the cached data will be out of date; you can get confusing results or even a data leak into the validation set.

To be safe, you can just clear out the cached files between runs:

find ./data/ -name \*.npy -delete

Breaking changes in TensorFlow / Keras

The code is the book was written against TensorFlow 1.8.x / Keras 2.2.x. There have been many many new releases since them, including some breaking changes to the API. I believe anything earlier than TF 2.0 will work OK with the book examples, but if you want to be really safe, you can downgrade all the way to TF 1.8.0.

A more practical example

If you actually want to use this network to play go, you’ll want a much larger sample. Probably 100000 games minimum, but more is better. As the number of games grows larger, you can reduce the number of epochs.

In my experience, 2 or 3 epochs over the full game set gives you a network that plays pretty reasonable moves. That should take a few days if you have access to a reasonably modern GPU, and a few weeks if you don’t.

Sorry about that

Sorry about the mistake in the listing. I underestimated how difficult it would be to keep all the code and output listings throughout the whole book in sync. Unfortunately, a few mistakes like this slipped through. We regret the error.