forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl_loop.py
188 lines (154 loc) · 6.4 KB
/
rl_loop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Wrapper scripts to ensure that main.py commands are called correctly."""
import argh
import argparse
import cloud_logging
import logging
import os
import main
import shipname
import sys
import time
from utils import timer
from tensorflow import gfile
# Pull in environment variables. Run `source ./cluster/common` to set these.
BUCKET_NAME = os.environ['BUCKET_NAME']
BASE_DIR = "gs://{}".format(BUCKET_NAME)
MODELS_DIR = os.path.join(BASE_DIR, 'models')
SELFPLAY_DIR = os.path.join(BASE_DIR, 'data/selfplay')
HOLDOUT_DIR = os.path.join(BASE_DIR, 'data/holdout')
SGF_DIR = os.path.join(BASE_DIR, 'sgf')
TRAINING_CHUNK_DIR = os.path.join(BASE_DIR, 'data', 'training_chunks')
ESTIMATOR_WORKING_DIR = 'estimator_working_dir'
# How many games before the selfplay workers will stop trying to play more.
MAX_GAMES_PER_GENERATION = 10000
# What percent of games to holdout from training per generation
HOLDOUT_PCT = 0.05
def print_flags():
flags = {
'BUCKET_NAME': BUCKET_NAME,
'BASE_DIR': BASE_DIR,
'MODELS_DIR': MODELS_DIR,
'SELFPLAY_DIR': SELFPLAY_DIR,
'HOLDOUT_DIR': HOLDOUT_DIR,
'SGF_DIR': SGF_DIR,
'TRAINING_CHUNK_DIR': TRAINING_CHUNK_DIR,
'ESTIMATOR_WORKING_DIR': ESTIMATOR_WORKING_DIR,
}
print("Computed variables are:")
print('\n'.join('--{}={}'.format(flag, value)
for flag, value in flags.items()))
def get_models():
"""Finds all models, returning a list of model number and names
sorted increasing.
Returns: [(13, 000013-modelname), (17, 000017-modelname), ...etc]
"""
all_models = gfile.Glob(os.path.join(MODELS_DIR, '*.meta'))
model_filenames = [os.path.basename(m) for m in all_models]
model_numbers_names = sorted([
(shipname.detect_model_num(m), shipname.detect_model_name(m))
for m in model_filenames])
return model_numbers_names
def get_latest_model():
"""Finds the latest model, returning its model number and name
Returns: (17, 000017-modelname)
"""
return get_models()[-1]
def get_model(model_num):
models = {k: v for k, v in get_models()}
if not model_num in models:
raise ValueError("Model {} not found!".format(model_num))
return models[model_num]
def game_counts(n_back=20):
"""Prints statistics for the most recent n_back models"""
all_models = gfile.Glob(os.path.join(MODELS_DIR, '*.meta'))
model_filenames = sorted([os.path.basename(m).split('.')[0]
for m in all_models], reverse=True)
for m in model_filenames[:n_back]:
games = gfile.Glob(os.path.join(SELFPLAY_DIR, m, '*.zz'))
print(m, len(games))
def bootstrap():
bootstrap_name = shipname.generate(0)
bootstrap_model_path = os.path.join(MODELS_DIR, bootstrap_name)
print("Bootstrapping with working dir {}\n Model 0 exported to {}".format(
ESTIMATOR_WORKING_DIR, bootstrap_model_path))
main.bootstrap(ESTIMATOR_WORKING_DIR, bootstrap_model_path)
def selfplay(readouts=1600, verbose=2, resign_threshold=0.99):
_, model_name = get_latest_model()
games = gfile.Glob(os.path.join(SELFPLAY_DIR, model_name, '*.zz'))
if len(games) > MAX_GAMES_PER_GENERATION:
print("{} has enough games ({})".format(model_name, len(games)))
time.sleep(10*60)
sys.exit(1)
print("Playing a game with model {}".format(model_name))
model_save_path = os.path.join(MODELS_DIR, model_name)
game_output_dir = os.path.join(SELFPLAY_DIR, model_name)
game_holdout_dir = os.path.join(HOLDOUT_DIR, model_name)
sgf_dir = os.path.join(SGF_DIR, model_name)
main.selfplay(
load_file=model_save_path,
output_dir=game_output_dir,
holdout_dir=game_holdout_dir,
output_sgf=sgf_dir,
readouts=readouts,
holdout_pct=HOLDOUT_PCT,
resign_threshold=resign_threshold,
verbose=verbose,
)
def gather():
print("Gathering game output...")
main.gather(input_directory=SELFPLAY_DIR,
output_directory=TRAINING_CHUNK_DIR)
def train():
model_num, model_name = get_latest_model()
print("Training on gathered game data, initializing from {}".format(model_name))
new_model_name = shipname.generate(model_num + 1)
print("New model will be {}".format(new_model_name))
load_file = os.path.join(MODELS_DIR, model_name)
save_file = os.path.join(MODELS_DIR, new_model_name)
try:
main.train(ESTIMATOR_WORKING_DIR, TRAINING_CHUNK_DIR, save_file,
generation_num=model_num + 1)
except:
print("Got an error training, muddling on...")
logging.exception("Train error")
def validate(model_num=None, validate_name=None):
""" Runs validate on the directories up to the most recent model, or up to
(but not including) the model specified by `model_num`
"""
if model_num is None:
model_num, model_name = get_latest_model()
else:
model_num = int(model_num)
model_name = get_model(model_num)
# Model N was trained on games up through model N-2, so the validation set
# should only be for models through N-2 as well, thus the (model_num - 1)
# term.
models = list(
filter(lambda num_name: num_name[0] < (model_num - 1), get_models()))
# Run on the most recent 50 generations,
# TODO(brianklee): make this hyperparameter dependency explicit/not hardcoded
holdout_dirs = [os.path.join(HOLDOUT_DIR, pair[1])
for pair in models[-50:]]
main.validate(ESTIMATOR_WORKING_DIR, *holdout_dirs,
checkpoint_name=os.path.join(MODELS_DIR, model_name),
validate_name=validate_name)
parser = argparse.ArgumentParser()
argh.add_commands(parser, [train, selfplay, gather,
bootstrap, game_counts, validate])
if __name__ == '__main__':
print_flags()
cloud_logging.configure()
argh.dispatch(parser)