Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose instead of setup_env.sh #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

A simplified, highly flexible, commented and (hopefully) easy to understand implementation of self-play based reinforcement learning based on the AlphaGo Zero paper (Silver et al). It is designed to be easy to adopt for any two-player turn-based adversarial game and any deep learning framework of your choice. A sample implementation has been provided for the game of Othello in PyTorch, Keras, TensorFlow and Chainer. An accompanying tutorial can be found [here](http://web.stanford.edu/~surag/posts/alphazero.html). We also have implementations for GoBang and TicTacToe.

To use a game of your choice, subclass the classes in ```Game.py``` and ```NeuralNet.py``` and implement their functions. Example implementations for Othello can be found in ```othello/OthelloGame.py``` and ```othello/{pytorch,keras,tensorflow,chainer}/NNet.py```.
To use a game of your choice, subclass the classes in ```Game.py``` and ```NeuralNet.py``` and implement their functions. Example implementations for Othello can be found in ```othello/OthelloGame.py``` and ```othello/{pytorch,keras,tensorflow,chainer}/NNet.py```.

```Coach.py``` contains the core training loop and ```MCTS.py``` performs the Monte Carlo Tree Search. The parameters for the self-play can be specified in ```main.py```. Additional neural network parameters are in ```othello/{pytorch,keras,tensorflow,chainer}/NNet.py``` (cuda flag, batch size, epochs, learning rate etc.).
```Coach.py``` contains the core training loop and ```MCTS.py``` performs the Monte Carlo Tree Search. The parameters for the self-play can be specified in ```main.py```. Additional neural network parameters are in ```othello/{pytorch,keras,tensorflow,chainer}/NNet.py``` (cuda flag, batch size, epochs, learning rate etc.).

To start training a model for Othello:
```bash
Expand All @@ -13,14 +13,19 @@ python main.py
Choose your framework and game in ```main.py```.

### Docker Installation
For easy environment setup, we can use [nvidia-docker](https://github.com/NVIDIA/nvidia-docker). Once you have nvidia-docker set up, we can then simply run:
For easy environment setup, we can use [nvidia-docker](https://github.com/NVIDIA/nvidia-docker)
and [docker-compose](https://docs.docker.com/compose/compose-file/compose-file-v2/).
Once you have them set up, simply run:
```
./setup_env.sh
docker-compose up
```
to set up a (default: pyTorch) Jupyter docker container. We can now open a new terminal and enter:
to start a (default: pyTorch) Jupyter docker container
available at http://localhost:8888.
We can also open a new terminal and enter:
```
docker exec -ti pytorch_notebook python main.py
docker-compose exec pytorch_notebook bash
```
to get a Bash prompt inside the container.

### Experiments
We trained a PyTorch model for 6x6 Othello (~80 iterations, 100 episodes per iteration and 25 MCTS simulations per turn). This took about 3 days on an NVIDIA Tesla K80. The pretrained model (PyTorch) can be found in ```pretrained_models/othello/pytorch/```. You can play a game against it using ```pit.py```. Below is the performance of the model against a random and a greedy baseline with the number of iterations.
Expand All @@ -33,7 +38,7 @@ While the current code is fairly functional, we could benefit from the following
* Game logic files for more games that follow the specifications in ```Game.py```, along with their neural networks
* Neural networks in other frameworks
* Pre-trained models for different game configurations
* An asynchronous version of the code- parallel processes for self-play, neural net training and model comparison.
* An asynchronous version of the code- parallel processes for self-play, neural net training and model comparison.
* Asynchronous MCTS as described in the paper

### Contributors and Credits
Expand All @@ -44,4 +49,3 @@ While the current code is fairly functional, we could benefit from the following
* [Jernej Habjan](https://github.com/JernejHabjan) contributed RTS game.

Thanks to [pytorch-classification](https://github.com/bearpaw/pytorch-classification) and [progress](https://github.com/verigak/progress).

19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '2.3'
services:
pytorch_notebook:
image: pytorch:0.4.0-py3-gpu
runtime: nvidia
build:
context: docker
dockerfile: Dockerfile.pytorch
shm_size: '8G'
volumes:
- .:/workspace
- ./docker/jupyter_notebook_config.py:/root/.jupyter/jupyter_notebook_config.py
ports:
- ${NOTEBOOK_PORT:-8888}:8888
- ${VISDOM_PORT:-8097}:8097
environment:
- JUPYTER_ENABLE_LAB=1
- NOTEBOOK_PORT=
- VISDOM_PORT=${VISDOM_PORT:-8097}
19 changes: 10 additions & 9 deletions docker/Dockerfile.pytorch
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
RUN curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda install numpy pyyaml scipy cython jupyter ipython mkl mkl-include && \
/opt/conda/bin/conda install -c soumith magma-cuda90 && \
/opt/conda/bin/conda install pytorch=0.4.0 -c pytorch && \
/opt/conda/bin/conda clean -ya

rm ~/miniconda.sh
ENV PATH /opt/conda/bin:$PATH
RUN conda install numpy pyyaml scipy cython jupyter ipython mkl mkl-include && \
conda install -c soumith magma-cuda90 && \
conda install pytorch=0.4.0 cuda90 -c pytorch && \
conda clean -ya
# conda install pytorch torchvision cuda90 -c pytorch &&\


# This must be done before pip so that requirements.txt is available.
WORKDIR /opt/pytorch
# pip install custom module listed in requirements.txt
COPY ./docker/requirements.txt .
COPY requirements.txt .
RUN pip install -U pip && pip install -r requirements.txt

WORKDIR /workspace
Expand All @@ -47,12 +48,12 @@ EXPOSE 8888
EXPOSE 8097

# Set up our notebook config.
COPY ./docker/jupyter_notebook_config.py /root/.jupyter/
COPY jupyter_notebook_config.py /root/.jupyter/

# Jupyter has issues with being run directly:
# https://github.com/ipython/ipython/issues/7062
# We just add a little wrapper script.
COPY ./docker/run_jupyter.sh /
COPY run_jupyter.sh /
RUN chmod +x /run_jupyter.sh

CMD ["/run_jupyter.sh", "--allow-root"]
13 changes: 2 additions & 11 deletions docker/jupyter_notebook_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@
from IPython.lib import passwd

c = c # pylint:disable=undefined-variable
c.NotebookApp.ip = '*'
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.port = int(os.getenv('PORT', 8888))
c.NotebookApp.open_browser = False

# sets a password if PASSWORD is set in the environment
if 'PASSWORD' in os.environ:
password = os.environ['PASSWORD']
if password:
c.NotebookApp.password = passwd(password)
else:
c.NotebookApp.password = ''
c.NotebookApp.token = ''
del os.environ['PASSWORD']
c.NotebookApp.token = os.getenv('TOKEN', u'')
15 changes: 0 additions & 15 deletions setup_env.sh

This file was deleted.