-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
210 lines (170 loc) · 7.32 KB
/
training.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import argparse
from subprocess import PIPE, DEVNULL
from tqdm import tqdm
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.regularizers import l2
from tensorflow.keras import optimizers
import tensorflow_addons as tfa
import numpy as np
import pickle
from threading import Thread, RLock
import os
import subprocess
from datetime import datetime
from munch import Munch
import toml
from settings import *
from networks import *
from replay_buffer import ReplayBuffer, BufferThread, AlphaZeroGenerator, MuGenerator
# Allow dynamic memory growth in order not to take all the GPU resource
physical_devices = tf.config.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("--new", help="create new network", action="store_true")
parser.add_argument("--config", type=str, help="config file name")
parser.add_argument("--method", type=str,
help="learning method", choices=["alpha", "mu"])
args = parser.parse_args()
# Load config file
config_file = "config/{}.toml".format(args.config)
method = args.method
config = toml.load(config_file)
# Setup default parameters.
if "mu" in config:
if "reward_support" not in config["mu"]:
config["mu"]["reward_support"] = 0
if "value_support" not in config["mu"]["puct"]:
config["mu"]["puct"]["value_support"] = 0
if "alpha" in config:
if "value_support" not in config["alpha"]["puct"]:
config["alpha"]["puct"]["value_support"] = 0
if "game" in config:
if "history" not in config["game"]:
config["game"]["history"] = 1
config = Munch.fromDict(config)
base_dir = dir_name(config, method)
print("Loaded config: {}".format(config))
# REPLAY BUFFER
print("Creating replay buffer.")
training_data_path = "./data/{}/training_data/".format(base_dir)
if os.path.exists(training_data_path+"replay_buffer.pkl"):
print("| Loaded replay buffer")
f = open(training_data_path+"replay_buffer.pkl", "rb")
replay_buffer = pickle.load(f)
f.close()
print("Status: {} / {}".format(replay_buffer.states_count, replay_buffer.max_index))
else:
print("| Starting replay buffer from scratch")
os.makedirs(training_data_path, exist_ok=True)
replay_buffer = ReplayBuffer(0, 0, 0, [None]*config.training.replay_buffer)
buffer_thr = BufferThread(config, replay_buffer, training_data_path,
fifo_path="./data/{}/fifo".format(base_dir))
# Create models and data generators.
if method == "alpha":
if not hasattr(config, "alpha"):
print("Alpha is not supported for this game.")
exit(-1)
model_path = "./data/{}/model/".format(base_dir)
if os.path.exists(model_path) and not(args.new):
print("| Loaded previous instance of the model.")
network = models.load_model(model_path)
print(network.summary())
try:
start_epoch = np.load(model_path+"epoch.npy")
except FileNotFoundError:
start_epoch = 0
print("Epoch: {}".format(start_epoch))
else:
print("| Starting model from scratch.")
network = policy_value_network_alpha(config)
start_epoch = 0
network.compile(optimizer="adam", loss={
"policy": "categorical_crossentropy", "value": "binary_crossentropy"})
models.save_model(network, model_path, save_format="tf")
trainGenerator = AlphaZeroGenerator(replay_buffer, config)
elif method == "mu":
if not hasattr(config, "mu"):
print("Mu is not supported for this game.")
exit(-1)
models_path = "./data/{}/models/".format(base_dir)
if os.path.exists(models_path+"pv") and not(args.new):
print("| Loaded previous instance of the models.")
pv = models.load_model(models_path+"pv", compile=False)
state = models.load_model(models_path+"state", compile=False)
dynamics = models.load_model(models_path+"dyn", compile=False)
try:
start_epoch = np.load(models_path+"epoch.npy")
except:
start_epoch = 0
print("Epoch: {}".format(start_epoch))
else:
print("| Starting model from scratch.")
state = representation_network(config)
print("state")
pv = prediction_network_mu(config)
print("policy")
dynamics = dynamics_network(config)
print("dynamics")
start_epoch = 0
models.save_model(pv, models_path+"pv", save_format="tf")
models.save_model(dynamics, models_path+"dyn", save_format="tf")
models.save_model(state, models_path+"state", save_format="tf")
network = unroll_networks(config, state, pv, dynamics)
network.compile(optimizer="adam", loss={
"policy": mu_loss_unrolled_cce(config), "value": mu_loss_unrolled_cce(config), "reward": mu_loss_unrolled_cce(config)})
trainGenerator = MuGenerator(replay_buffer, config)
else:
print("Unknown method..")
exit(-1)
trainDataset = trainGenerator.dataset()
trainDataset = tf.data.Dataset.range(4).interleave(lambda x: trainDataset, num_parallel_calls=4)\
.prefetch(tf.data.experimental.AUTOTUNE)
# waiting until several bootstrap games have been created.
buffer_thr.preload(config.training.batch)
buffer_thr.start()
# Logs for tensorboard
logdir = "./data/{}/logs/{}/".format(base_dir,
datetime.now().strftime("%Y%m%d-%H%M%S"))
file_writer = tf.summary.create_file_writer(logdir)
file_writer.set_as_default()
class StatsLogger(tf.keras.callbacks.Callback):
def __init__(self):
self.n = 0
def on_epoch_end(self, epoch, logs=None):
global replay_buffer
self.n += config.training.epoch
tf.summary.scalar('generated_states',
data=replay_buffer.states_count, step=epoch)
tf.summary.scalar('Games length', data=sum(
[len(g.turn) for g in replay_buffer.games[:replay_buffer.max_index]])/replay_buffer.max_index, step=epoch)
if self.n > config.training.checkpoint_freq and method == "mu":
self.n = 0
print("Saving models..")
np.save(models_path+"epoch.npy", epoch)
models.save_model(pv, models_path+"pv",
save_format="tf", include_optimizer=False)
models.save_model(dynamics, models_path+"dyn",
save_format="tf", include_optimizer=False)
models.save_model(state, models_path+"state",
save_format="tf", include_optimizer=False)
# Callbacks
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
stats_callback = StatsLogger()
tqdm_callback = tfa.callbacks.TQDMProgressBar()
callbacks = [tqdm_callback, tensorboard_callback, stats_callback]
if method == "alpha":
# CHECKPOINT
checkpoint_callback = ModelCheckpoint(
model_path, verbose=1, save_weights_only=False, save_freq=config.training.checkpoint_freq)
callbacks.append(checkpoint_callback)
# Main loop.
network.fit(trainDataset, epochs=config.training.n_epoch, steps_per_epoch=config.training.epoch//config.training.batch,
verbose=0, callbacks=callbacks, initial_epoch=start_epoch)
buffer_thr.stop()
buffer_thr.join()