-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
383 lines (343 loc) · 12.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""Main training script."""
import argparse
import asyncio
import math
import os
from collections import defaultdict
from contextlib import closing
from dataclasses import dataclass
from functools import reduce
from itertools import chain
from pathlib import Path
from typing import Optional, TextIO, Union
if (
__name__ == "__main__"
and os.environ.get("TF_CPP_MIN_LOG_LEVEL", None) is None
):
# Prevent log spam when compiling graphs.
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
# pylint: disable=wrong-import-position
import tensorflow as tf
import yaml
from tqdm import tqdm
from .agents.dqn_agent import DQNAgent, DQNAgentConfig
from .agents.drqn_agent import DRQNAgent, DRQNAgentConfig
from .config import TrainConfig
from .environments.battle_env import BattleEnv, EvalOpponentConfig
from .utils.paths import DEFAULT_CONFIG_PATH, PROJECT_DIR
from .utils.random import randstr
from .utils.tqdm_redirect import std_out_err_redirect_tqdm
@dataclass
class Wlt:
"""Win-loss-tie container for evaluation step."""
wins: int = 0
losses: int = 0
ties: int = 0
total: int = 0
@property
def win_rate(self):
"""Ratio of wins to total battles."""
return float(self.wins) / self.total
@property
def loss_rate(self):
"""Ratio of losses to total battles."""
return float(self.losses) / self.total
@property
def tie_rate(self):
"""Ratio of ties to total battles."""
return float(self.ties) / self.total
def win(self):
"""Add win."""
self.wins += 1
self.total += 1
def lose(self):
"""Add loss."""
self.losses += 1
self.total += 1
def tie(self):
"""Add tie."""
self.ties += 1
self.total += 1
def __repr__(self):
return f"{self.wins}-{self.losses}-{self.ties}"
async def run_eval(
prefix: str,
episode: tf.Variable,
agent: Union[DQNAgent, DRQNAgent],
env: BattleEnv,
opponents: tuple[EvalOpponentConfig, ...],
progbar_file: TextIO,
writer: Optional[tf.summary.SummaryWriter] = None,
):
"""Runs evaluation games for the current model against baseline agents."""
wlts = defaultdict[str, Wlt](Wlt)
max_battles = dict((opp.name, opp.battles) for opp in opponents)
with tqdm(
desc=f"{prefix}: Eval",
total=reduce(lambda a, x: a + x, max_battles.values()),
leave=True,
file=progbar_file,
unit="battles",
unit_scale=True,
dynamic_ncols=True,
position=1,
) as pbar:
state, info = env.reset(eval_opponents=opponents)
done = False
while not done:
action = agent.select_action(
state, info, episode=episode, can_explore=False
)
state, _, terminated, truncated, info, done = await env.step(action)
for key, ended in chain(terminated.items(), truncated.items()):
if ended:
state.pop(key)
info.pop(key)
if isinstance(agent, DRQNAgent):
# Note: This is usually handled by update_model() but we
# have to do this manually during evaluation to prevent
# memory leaks.
agent.reset(key)
for key, env_info in info.items():
if key.player != "__env__":
continue
battle_result = env_info.get("battle_result", None)
if battle_result is None:
continue
# Finished an evaluation battle.
opp_name = battle_result["agents"]["p2"]
wlt = wlts[opp_name]
winner = battle_result.get("winner", None)
if winner is None:
wlt.tie()
elif winner == "p1":
wlt.win()
else:
wlt.lose()
if wlt.total >= max_battles[opp_name]:
# Finished all evaluation battles against an opponent.
tqdm.write(f"{prefix}: Eval vs {opp_name}: {wlt!r}")
if writer is not None:
with writer.as_default(step=episode):
tf.summary.scalar(
f"eval/{opp_name}/win_rate", wlt.win_rate
)
tf.summary.scalar(
f"eval/{opp_name}/loss_rate", wlt.loss_rate
)
tf.summary.scalar(
f"eval/{opp_name}/tie_rate", wlt.tie_rate
)
pbar.update()
# pylint: disable-next=too-many-branches
async def train(config: TrainConfig):
"""Main training script."""
save_path: Optional[Path] = None
writer: Optional[tf.summary.SummaryWriter] = None
if config.save_path is not None:
save_path = Path(PROJECT_DIR, config.save_path, config.name).resolve()
print(f"Configured to write training logs to {save_path}")
metrics_path = Path(save_path, "metrics").resolve()
writer = tf.summary.create_file_writer(os.fspath(metrics_path))
writer.init()
writer.set_as_default()
print(f"Configured to write TensorBoard metrics to {metrics_path}")
if config.seed is not None:
tf.keras.utils.set_random_seed(config.seed)
rng = (
tf.random.Generator.from_seed(
tf.random.uniform(shape=(), maxval=tf.int64.max, dtype=tf.int64)
)
if config.seed is not None
else tf.random.get_global_generator()
)
agent: Union[DQNAgent, DRQNAgent]
if config.agent.type == "dqn":
assert isinstance(config.agent.config, DQNAgentConfig)
agent = DQNAgent(config=config.agent.config, rng=rng, writer=writer)
elif config.agent.type == "drqn":
assert isinstance(config.agent.config, DRQNAgentConfig)
agent = DRQNAgent(config=config.agent.config, rng=rng, writer=writer)
else:
raise ValueError(f"Unknown agent type '{config.agent.type}'")
env_id = randstr(rng, 6)
env = BattleEnv(
config=config.rollout.env,
rng=rng,
sock_id=env_id,
log_path=Path(save_path, "battles", "rollout").resolve()
if save_path is not None
else None,
)
await env.ready()
while (eval_id := randstr(rng, 6)) == env_id:
pass
eval_env = BattleEnv(
config=config.eval.env,
rng=rng,
sock_id=eval_id,
log_path=Path(save_path, "battles", "eval").resolve()
if save_path is not None
else None,
)
await eval_env.ready()
# Current episode number.
episode = tf.Variable(0, name="episode", dtype=tf.int64)
num_ties = tf.Variable(0, name="num_ties", dtype=tf.int64)
ckpt: Optional[tf.train.Checkpoint] = None
ckpt_manager: Optional[tf.train.CheckpointManager] = None
ckpt_options: Optional[tf.train.CheckpointOptions] = None
restored = False
if save_path is not None:
ckpt_dir = Path(save_path, "checkpoints").resolve()
ckpt = tf.train.Checkpoint(
episode=episode,
num_ties=num_ties,
rng=rng,
model=agent.model,
previous=agent.previous,
target=agent.target,
optimizer=agent.optimizer,
step=agent.step,
)
ckpt_manager = tf.train.CheckpointManager(ckpt, ckpt_dir, max_to_keep=5)
print(f"Configured to write checkpoints to {ckpt_dir}")
ckpt_options = tf.train.CheckpointOptions(enable_async=True)
if (restore_path := ckpt_manager.restore_or_initialize()) is not None:
print(f"Restored from {restore_path}")
restored = True
print(f"Last completed episode: {int(episode)}")
episode.assign_add(1, read_value=False)
eps_padding = 1 + math.floor(math.log10(config.rollout.num_episodes))
eps_fmt = "{: >" + str(eps_padding) + "d}"
with closing(env), closing(
eval_env
), std_out_err_redirect_tqdm() as original_stdout, tqdm(
desc="Episode",
total=config.rollout.num_episodes,
leave=True,
file=original_stdout,
unit="eps",
unit_scale=True,
dynamic_ncols=True,
smoothing=0.1,
initial=min(int(episode), config.rollout.num_episodes),
position=0,
) as pbar:
if config.rollout.eps_per_eval > 0 and not restored and episode == 0:
# Pre-evaluation for comparison against the later trained model.
await run_eval(
prefix=eps_fmt.format(int(episode)),
episode=episode,
agent=agent,
env=eval_env,
opponents=config.eval.opponents,
progbar_file=original_stdout,
writer=writer,
)
rollout_battles = max(0, config.rollout.num_episodes - int(episode))
state, info = env.reset(
rollout_battles=rollout_battles,
rollout_opponents=config.rollout.opponents,
)
done = False
while not done:
if rollout_battles <= 0:
break
action = agent.select_action(
state, info, episode=episode, can_explore=True
)
(
next_state,
reward,
terminated,
truncated,
info,
done,
) = await env.step(action)
agent.update_model(
state,
reward,
next_state,
terminated,
truncated,
info,
)
state = next_state
for key, ended in chain(terminated.items(), truncated.items()):
if ended:
state.pop(key)
info.pop(key)
for key, env_info in info.items():
if key.player != "__env__":
continue
battle_result = env_info.get("battle_result", None)
if battle_result is None:
continue
# Finished a rollout episode.
episode.assign_add(1, read_value=False)
pbar.update()
if battle_result.get("winner", None) is None:
num_ties.assign_add(1, read_value=False)
if writer is not None:
with writer.as_default(step=episode):
tf.summary.scalar(
"rollout/tie_rate", num_ties / episode
)
tf.summary.scalar("rollout/num_ties", num_ties)
if config.agent.config.exploration is not None:
tf.summary.scalar(
"rollout/exploration",
agent.epsilon_greedy.get_epsilon(episode),
)
if (
config.rollout.eps_per_eval > 0
and episode > 0
and episode % config.rollout.eps_per_eval == 0
):
await run_eval(
prefix=eps_fmt.format(int(episode)),
episode=episode,
agent=agent,
env=eval_env,
opponents=config.eval.opponents,
progbar_file=original_stdout,
writer=writer,
)
if (
config.rollout.eps_per_prev_update is not None
and episode > 0
and episode % config.rollout.eps_per_prev_update == 0
):
agent.update_prev()
if (
ckpt_manager is not None
and config.rollout.eps_per_ckpt is not None
and episode > 0
and episode % config.rollout.eps_per_ckpt == 0
):
ckpt_path = ckpt_manager.save(options=ckpt_options)
tqdm.write(
f"{eps_fmt.format(int(episode))}: "
f"Saved checkpoint: {ckpt_path}"
)
if writer is not None:
writer.flush()
if save_path is not None:
save_path = Path(save_path, "model").resolve()
agent.model.save(save_path, save_traces=False)
print(f"Saved model to {save_path}")
def main():
"""Main entry point for training script."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--config-file",
default=DEFAULT_CONFIG_PATH,
type=Path,
)
args = parser.parse_args()
with args.config_file.open("r") as file:
config = TrainConfig.from_dict(yaml.safe_load(file))
asyncio.run(train(config=config))
if __name__ == "__main__":
main()