Skip to content

Commit

Permalink
Merge pull request #28 from mokemokechicken/feature/enable_tensorboard
Browse files Browse the repository at this point in the history
add simple tensorboard callback #27
  • Loading branch information
mokemokechicken authored Jan 4, 2018
2 parents 3f2a9fe + 01e99d7 commit 410802e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ After BestModel moves, numbers are displayed on the board.
* Top left numbers(1) mean 'Visit Count (=N(s,a))' of the last search.
* Bottom left numbers(2) mean 'Q Value (=Q(s,a)) on AI side' of the last state and move. The Q values are multiplied by 100.

View Training Log in TensorBoard
----------------

### 1. install tensorboard

```bash
pip install tensorboard
```

### 2. launch tensorboard and access by web browser

```bash
tensorboard --logdir logs/tensorboard/
```

And access `http://<The Machine IP>:6006/`.

### Trouble Shooting

If you can not launch tensorboard by error,
try to create another new plain project which includes only `tensorflow` and `tensorboard`.

And

```bash
tensorboard --logdir <PATH TO REVERSI DIR>/logs/tensorboard/
```


Tips and Memo
====
Expand Down
1 change: 1 addition & 0 deletions src/reversi_zero/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self):

self.log_dir = os.path.join(self.project_dir, "logs")
self.main_log_path = os.path.join(self.log_dir, "main.log")
self.tensorboard_log_dir = os.path.join(self.log_dir, 'tensorboard')

def create_directories(self):
dirs = [self.project_dir, self.data_dir, self.model_dir, self.play_data_dir, self.log_dir,
Expand Down
1 change: 1 addition & 0 deletions src/reversi_zero/configs/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self):
self.epoch_to_checkpoint = 1
self.start_total_steps = 0
self.save_model_steps = 200
self.use_tensorboard = True


class ModelConfig:
Expand Down
29 changes: 29 additions & 0 deletions src/reversi_zero/lib/tensorboard_step_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from keras.callbacks import Callback
import tensorflow as tf


class TensorBoardStepCallback(Callback):
"""Tensorboard basic visualizations by step.
"""

def __init__(self, log_dir):
super().__init__()
self.step = 0
self.writer = tf.summary.FileWriter(log_dir)

def on_batch_end(self, batch, logs=None):
self.step += 1

for name, value in logs.items():
if name in ['batch', 'size']:
continue
summary = tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = value.item()
summary_value.tag = name
self.writer.add_summary(summary, self.step)
self.writer.flush()

def on_train_end(self, logs=None):
self.writer.close()
8 changes: 7 additions & 1 deletion src/reversi_zero/worker/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import keras.backend as K
import numpy as np
from keras.callbacks import LambdaCallback, Callback
from keras.callbacks import Callback
from keras.optimizers import SGD

from reversi_zero.agent.model import ReversiModel, objective_function_for_policy, \
Expand All @@ -16,6 +16,7 @@
from reversi_zero.lib.data_helper import get_game_data_filenames, read_game_data_from_file, \
get_next_generation_model_dirs
from reversi_zero.lib.model_helpler import load_best_model_weight
from reversi_zero.lib.tensorboard_step_callback import TensorBoardStepCallback

logger = getLogger(__name__)

Expand Down Expand Up @@ -44,6 +45,11 @@ def training(self):
save_model_callback = PerStepCallback(self.config.trainer.save_model_steps, self.save_current_model)
callbacks = [save_model_callback]

if self.config.trainer.use_tensorboard:
callbacks += [TensorBoardStepCallback(
log_dir=self.config.resource.tensorboard_log_dir,
)]

while True:
self.load_play_data()
if self.dataset_size < self.config.trainer.min_data_size_to_learn:
Expand Down

0 comments on commit 410802e

Please sign in to comment.