Repository navigation
Expand file tree
/
Copy pathrl_loop.py
More file actions
111 lines (96 loc) · 3.9 KB
/
Copy pathrl_loop.py
File metadata and controls
111 lines (96 loc) · 3.9 KB
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
<
8000
div data-line-number="34" class="react-line-number react-code-text" style="padding-right:16px">3435
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
# 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 os
import main
import shipname
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, 'games')
SGF_DIR = os.path.join(BASE_DIR, 'sgf')
TRAINING_CHUNK_DIR = os.path.join(BASE_DIR, 'data', 'training_chunks')
def print_flags():
flags = {
'BUCKET_NAME': BUCKET_NAME,
'BASE_DIR': BASE_DIR,
'MODELS_DIR': MODELS_DIR,
'SELFPLAY_DIR': SELFPLAY_DIR,
'SGF_DIR': SGF_DIR,
'TRAINING_CHUNK_DIR': TRAINING_CHUNK_DIR,
}
print("Computed variables are:")
print('\n'.join('--{}={}'.format(flag, value) for flag, value in flags.items()))
def get_latest_model():
'''Finds the latest model, returning its model number and name
Returns: (17, 000017-modelname)
'''
all_models = gfile.Glob(os.path.join(MODELS_DIR, '*.meta'))
model_filenames = [os.path.basename(m) for m in all_models]
model_numbers_names = [
(shipname.detect_model_num(m), shipname.detect_model_name(m))
for m in model_filenames]
latest_model = sorted(model_numbers_names, reverse=True)[0]
return latest_model
def convert_all():
all_models = gfile.Glob(os.path.join(MODELS_DIR, '*.meta'))
model_filenames = [os.path.basename(m).split('.')[0] for m in all_models]
print(model_filenames)
from tqdm import tqdm
import multiprocessing
pool = multiprocessing.Pool(10)
for m in tqdm(model_filenames[10:]):
p = os.path.join('gs://mugozero-v2/games/', m) + "/**/*.gz"
games = gfile.Glob(p)
pool.map(main.convert, games)
def bootstrap():
bootstrap_name = shipname.generate(0)
bootstrap_model_path = os.path.join(MODELS_DIR, bootstrap_name)
print("Bootstrapping model at {}".format(bootstrap_model_path))
main.bootstrap(bootstrap_model_path)
def selfplay(readouts=1600, verbose=2, resign_threshold=0.99):
_, model_name = get_latest_model()
print("Playing a game with model {}".format(model_name))
model_save_file = os.path.join(MODELS_DIR, model_name)
main.selfplay(
load_file=model_save_file,
output_dir=os.path.join(SELFPLAY_DIR, model_name),
output_sgf=SGF_DIR,
readouts=readouts,
verbose=verbose,
)
def gather():
print("Gathering game output...")
main.gather(input_directory=SELFPLAY_DIR, output_directory=TRAINING_CHUNK_DIR)
def train(logdir=None):
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)
main.train(TRAINING_CHUNK_DIR, save_file=save_file, load_file=load_file,
generation_num=model_num, logdir=logdir)
parser = argparse.ArgumentParser()
argh.add_commands(parser, [train, selfplay, gather, bootstrap, convert_all])
if __name__ == '__main__':
print_flags()
cloud_logging.configure()
argh.dispatch(parser)