-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·186 lines (158 loc) · 6.77 KB
/
train.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
#!/usr/bin/env python3
#
# This file is part of Leela Zero.
# Copyright (C) 2017 Gian-Carlo Pascutto
#
# Leela Zero is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Leela Zero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
import yaml
import sys
import glob
import gzip
import random
import multiprocessing as mp
import tensorflow as tf
from tfprocess import TFProcess
from chunkparser import ChunkParser
SKIP = 32
def get_chunks(data_prefix):
return glob.glob(data_prefix + "*.gz")
def get_latest_chunks(path, num_chunks):
chunks = []
for d in glob.glob(path):
chunks += get_chunks(d)
if len(chunks) < num_chunks:
print("Not enough chunks {}".format(len(chunks)))
sys.exit(1)
print("sorting {} chunks...".format(len(chunks)), end='')
chunks.sort(key=os.path.getmtime, reverse=True)
print("[done]")
chunks = chunks[:num_chunks]
print("{} - {}".format(os.path.basename(chunks[-1]), os.path.basename(chunks[0])))
random.shuffle(chunks)
return chunks
class FileDataSrc:
"""
data source yielding chunkdata from chunk files.
"""
def __init__(self, chunks):
self.chunks = []
self.done = chunks
def next(self):
if not self.chunks:
self.chunks, self.done = self.done, self.chunks
random.shuffle(self.chunks)
if not self.chunks:
return None
while len(self.chunks):
filename = self.chunks.pop()
try:
with gzip.open(filename, 'rb') as chunk_file:
self.done.append(filename)
return chunk_file.read()
except:
print("failed to parse {}".format(filename))
def main(cmd):
cfg = yaml.safe_load(cmd.cfg.read())
print(yaml.dump(cfg, default_flow_style=False))
num_chunks = cfg['dataset']['num_chunks']
train_ratio = cfg['dataset']['train_ratio']
num_train = int(num_chunks*train_ratio)
num_test = num_chunks - num_train
if 'input_test' in cfg['dataset']:
train_chunks = get_latest_chunks(cfg['dataset']['input_train'], num_train)
test_chunks = get_latest_chunks(cfg['dataset']['input_test'], num_test)
else:
chunks = get_latest_chunks(cfg['dataset']['input'], num_chunks)
train_chunks = chunks[:num_train]
test_chunks = chunks[num_train:]
shuffle_size = cfg['training']['shuffle_size']
total_batch_size = cfg['training']['batch_size']
batch_splits = cfg['training'].get('num_batch_splits', 1)
if total_batch_size % batch_splits != 0:
raise ValueError('num_batch_splits must divide batch_size evenly')
split_batch_size = total_batch_size // batch_splits
# Load data with split batch size, which will be combined to the total batch size in tfprocess.
ChunkParser.BATCH_SIZE = split_batch_size
root_dir = os.path.join(cfg['training']['path'], cfg['name'])
if not os.path.exists(root_dir):
os.makedirs(root_dir)
'''train_parser = ChunkParser(FileDataSrc(train_chunks),
shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
dataset = tf.data.Dataset.from_generator(
train_parser.parse, output_types=(tf.string, tf.string, tf.string))
dataset = dataset.map(ChunkParser.parse_function)
dataset = dataset.prefetch(2)
train_iterator = dataset.make_one_shot_iterator()
shuffle_size = int(shuffle_size*(1.0-train_ratio))
test_parser = ChunkParser(FileDataSrc(test_chunks),
shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
dataset = tf.data.Dataset.from_generator(
test_parser.parse, output_types=(tf.string, tf.string, tf.string))
dataset = dataset.map(ChunkParser.parse_function)
dataset = dataset.prefetch(2)
test_iterator = dataset.make_one_shot_iterator()'''
filenames = {'train': 'test_bytes', 'test': 'test_bytes'}
def extract(example):
features = {
'x': tf.FixedLenFeature((), tf.string),
'_y': tf.FixedLenFeature((), tf.string),
'_z': tf.FixedLenFeature((), tf.string)
}
parsed_example = tf.parse_single_example(example, features)
x = tf.decode_raw(parsed_example['x'], tf.float32)
_y = tf.decode_raw(parsed_example['_y'], tf.float32)
_z = tf.decode_raw(parsed_example['_z'], tf.float32)
x.set_shape([112 * 64])
_y.set_shape([1858])
_z.set_shape([1])
x = tf.reshape(x, [112, 64])
return x, _y, _z
dataset = tf.data.TFRecordDataset(filenames=[filenames['train']],
compression_type='GZIP')
dataset = dataset.map(extract)
dataset = dataset.batch(total_batch_size)
dataset = dataset.prefetch(4)
train_iterator = dataset.make_one_shot_iterator()
dataset = tf.data.TFRecordDataset(filenames=[filenames['test']],
compression_type='GZIP')
dataset = dataset.map(extract)
dataset = dataset.batch(total_batch_size)
dataset = dataset.prefetch(4)
test_iterator = dataset.make_one_shot_iterator()
tfprocess = TFProcess(cfg)
tfprocess.init(dataset, train_iterator, test_iterator)
if os.path.exists(os.path.join(root_dir, 'checkpoint')):
cp = tf.train.latest_checkpoint(root_dir)
# Sweeps through all test chunks statistically
# Assumes average of 10 samples per test game.
# For simplicity, testing can use the split batch size instead of total batch size.
# This does not affect results, because test results are simple averages that are independent of batch size.
num_evals = num_test*10 // ChunkParser.BATCH_SIZE
print("Using {} evaluation batches".format(num_evals))
tfprocess.process_loop(total_batch_size, num_evals)
tfprocess.session.close()
train_parser.shutdown()
test_parser.shutdown()
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description=\
'Tensorflow pipeline for training Leela Chess.')
argparser.add_argument('--cfg', type=argparse.FileType('r'),
help='yaml configuration with training parameters')
argparser.add_argument('--output', type=str,
help='file to store weights in')
mp.set_start_method('spawn')
main(argparser.parse_args())
mp.freeze_support()