-
Notifications
You must be signed in to change notification settings - Fork 18
/
plot_go.py
491 lines (401 loc) · 16.1 KB
/
plot_go.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Copyright (c) 2023 Michael Hu.
# This code is part of the book "The Art of Reinforcement Learning: Fundamentals, Mathematics, and Implementation with Python.".
# This project is released under the MIT License.
# See the accompanying LICENSE file for details.
"""Functions to plot statistics from csv log files."""
from absl import app, flags
import logging
import os
import math
import re
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter
FLAGS = flags.FLAGS
flags.DEFINE_string('logs_dir', './logs/go/9x9', '')
flags.DEFINE_float('line_width', 1, '')
def shorten(value, tick_number=None):
num_thousands = 0 if abs(value) < 1000 else math.floor(math.log10(abs(value)) / 3)
value = round(value / 1000**num_thousands, 2)
return f'{value:g}' + ' KMGTPEZY'[num_thousands]
def get_selfplay_dataframe(logs_dir):
actor_csv_logs = []
if os.path.exists(logs_dir):
for root, dirnames, filenames in os.walk(logs_dir):
for f in filenames:
if f.startswith('actor') and f.endswith('.csv'):
actor_csv_logs.append(os.path.join(root, f))
# Load actor statistics files
if len(actor_csv_logs) == 0:
logging.warning(f'No log files have been found at "{logs_dir}"')
return
df = pd.concat([pd.read_csv(f) for f in actor_csv_logs], sort=True)
# Derive new columns from existing data
df['game_count'] = 1 # every single row is a game
df['game_lt_60_step_count'] = df.apply(lambda row: 1 if row['game_length'] < 60 else 0, axis=1)
df['game_60_to_100_step_count'] = df.apply(
lambda row: 1 if row['game_length'] >= 60 and row['game_length'] <= 100 else 0,
axis=1,
)
df['game_gt_100_step_count'] = df.apply(lambda row: 1 if row['game_length'] > 100 else 0, axis=1)
df['black_won_count'] = df.apply(
lambda row: 1 if re.match(r'B\+', row['game_result'], re.IGNORECASE) else 0,
axis=1,
)
df['white_won_count'] = df.apply(
lambda row: 1 if re.match(r'W\+', row['game_result'], re.IGNORECASE) else 0,
axis=1,
)
df['black_resign_count'] = df.apply(lambda row: 1 if row['game_result'] == 'W+R' else 0, axis=1)
df['white_resign_count'] = df.apply(lambda row: 1 if row['game_result'] == 'B+R' else 0, axis=1)
df['resign_disabled_count'] = df.apply(lambda row: 1 if row['is_resign_disabled'] else 0, axis=1)
# Games marked for resign by a player, where resign move is never played since resignation is disabled
df['marked_resign_count'] = df.apply(
lambda row: 1 if row['is_resign_disabled'] and row['is_marked_for_resign'] else 0,
axis=1,
)
# Games marked for resign but ended the marked resign player won, where resign move is never played since resignation is disabled
df['marked_resign_could_won_count'] = df.apply(
lambda row: 1 if row['is_resign_disabled'] and row['is_marked_for_resign'] and row['is_could_won'] else 0,
axis=1,
)
# Group data by hours
# df['datetime'] = pd.to_datetime(df['datetime']) # if not already as datetime object
# grouped_df = df.groupby(pd.Grouper(key='datetime', axis=0, freq='H')).sum(numeric_only=True)
# grouped_df = grouped_df.reset_index()
# grouped_df['hours'] = grouped_df.index
# Group data by training steps
grouped_df = df.groupby(['training_steps']).sum(numeric_only=True)
grouped_df = grouped_df.reset_index()
grouped_df['avg_steps_per_game'] = grouped_df['game_length'].cumsum() / grouped_df['game_count'].cumsum()
grouped_df['avg_passes_per_game'] = grouped_df['num_passes'].cumsum() / grouped_df['game_count'].cumsum()
# Compute accumulative sum
grouped_df['total_games'] = grouped_df['game_count'].cumsum()
grouped_df['games_lt_60_steps'] = grouped_df['game_lt_60_step_count'].cumsum()
grouped_df['games_60_to_100_steps'] = grouped_df['game_60_to_100_step_count'].cumsum()
grouped_df['games_gt_100_steps'] = grouped_df['game_gt_100_step_count'].cumsum()
grouped_df['black_won_games'] = grouped_df['black_won_count'].cumsum()
grouped_df['white_won_games'] = grouped_df['white_won_count'].cumsum()
grouped_df['black_resign_games'] = grouped_df['black_resign_count'].cumsum()
grouped_df['white_resign_games'] = grouped_df['white_resign_count'].cumsum()
grouped_df['resign_disabled_games'] = grouped_df['resign_disabled_count'].cumsum()
grouped_df['marked_resign_games'] = grouped_df['marked_resign_count'] # .cumsum()
grouped_df['marked_resign_could_won_games'] = grouped_df['marked_resign_could_won_count'] # .cumsum()
# Compute estimated resignation false positive ratio using marked resign games from those 10% where resign is disabled
grouped_df['resign_false_positive_rate'] = grouped_df['marked_resign_could_won_games'] / grouped_df['marked_resign_games']
# Other ratio
grouped_df['steps_lt_60_rate'] = grouped_df['games_lt_60_steps'] / grouped_df['total_games']
grouped_df['steps_60_to_100_rate'] = grouped_df['games_60_to_100_steps'] / grouped_df['total_games']
grouped_df['steps_gt_100_rate'] = grouped_df['games_gt_100_steps'] / grouped_df['total_games']
grouped_df['black_won_rate'] = grouped_df['black_won_games'] / grouped_df['total_games']
grouped_df['white_won_rate'] = grouped_df['white_won_games'] / grouped_df['total_games']
grouped_df['black_resign_rate'] = grouped_df['black_resign_games'] / grouped_df['total_games']
grouped_df['white_resign_rate'] = grouped_df['white_resign_games'] / grouped_df['total_games']
grouped_df['resign_disabled_rate'] = grouped_df['resign_disabled_games'] / grouped_df['total_games']
return grouped_df
def get_dataframe(csv_file):
if not os.path.exists(csv_file) or not os.path.isfile(csv_file):
logging.warning(f'No log files have been found at "{csv_file}"')
return
df = pd.read_csv(csv_file)
# Derive hours from datetime
df['datetime'] = pd.to_datetime(df['datetime'])
start_time = df['datetime'].iloc[0]
df['hours'] = (df['datetime'] - start_time).dt.total_seconds() / 3600
return df
def main(argv): # noqa: C901
logging.info('Loading log files, this may take few minutes...')
train_df = get_dataframe(os.path.join(FLAGS.logs_dir, 'training.csv'))
eval_df = get_dataframe(os.path.join(FLAGS.logs_dir, 'evaluation.csv'))
selfplay_df = get_selfplay_dataframe(FLAGS.logs_dir)
fig = plt.figure(layout='constrained', figsize=(16, 9))
# Three columns
subfigs = fig.subfigures(1, 3, wspace=0.04)
for subfig in subfigs:
subfig.set_facecolor('0.95')
subfigs[0].suptitle('Self-play', fontsize='x-large')
subfigs[1].suptitle('Training', fontsize='x-large')
subfigs[2].suptitle('Evaluation', fontsize='x-large')
axs_selfplay = subfigs[0].subplots(5, 1, sharex=True)
axs_train = subfigs[1].subplots(5, 1, sharex=True)
axs_eval = subfigs[2].subplots(4, 1, sharex=True)
for i, ax in enumerate(axs_selfplay):
# ax.xaxis.set_major_locator(MaxNLocator(integer=True)) # if group by hours
ax.xaxis.set_major_formatter(FuncFormatter(shorten))
if i == 0:
if selfplay_df is not None:
plot_selfplay_num_games(selfplay_df, ax)
elif i == 1:
if selfplay_df is not None:
plot_selfplay_game_length(selfplay_df, ax)
elif i == 2:
if selfplay_df is not None:
plot_selfplay_resign_fp_ratio(selfplay_df, ax)
elif i == 3:
if selfplay_df is not None:
plot_selfplay_games_winrate(selfplay_df, ax)
elif i == 4:
if selfplay_df is not None:
plot_selfplay_games_precentage(selfplay_df, ax)
ax.set_xlabel('Training steps', fontsize='large')
for i, ax in enumerate(axs_train):
ax.xaxis.set_major_formatter(FuncFormatter(shorten))
if i == 0:
if train_df is not None:
plot_training_policy_loss(train_df, ax)
elif i == 1:
if train_df is not None:
plot_training_value_loss(train_df, ax)
elif i == 2:
if train_df is not None:
plot_training_lr(train_df, ax)
elif i == 3:
if train_df is not None:
plot_training_samples(train_df, ax)
elif i == 4:
if train_df is not None:
plot_training_time(train_df, ax)
ax.set_xlabel('Training steps', fontsize='large')
for i, ax in enumerate(axs_eval):
ax.xaxis.set_major_formatter(FuncFormatter(shorten))
if i == 0:
if eval_df is not None and 'policy_top_1_accuracy' in eval_df.columns:
plot_eval_policy_accuracy(eval_df, ax)
elif i == 1:
if eval_df is not None and 'value_mse_error' in eval_df.columns:
plot_eval_mse_error(eval_df, ax)
elif i == 2:
if eval_df is not None:
if 'policy_entropy' in eval_df.columns:
plot_eval_policy_entropy(eval_df, ax)
else:
plot_eval_game_length(eval_df, ax)
elif i == 3:
if eval_df is not None:
plot_eval_elo_rating(eval_df, ax)
ax.set_xlabel('Training steps', fontsize='large')
plt.show()
def plot_selfplay_games_precentage(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.steps_lt_60_rate * 100,
color='steelblue',
linewidth=FLAGS.line_width,
label='< 60 steps',
)
ax.plot(
df.training_steps,
df.steps_60_to_100_rate * 100,
color='purple',
linewidth=FLAGS.line_width,
label='60 - 100 steps',
)
ax.plot(
df.training_steps,
df.steps_gt_100_rate * 100,
color='orange',
linewidth=FLAGS.line_width,
label='> 100 steps',
)
ax.legend()
ax.set_ylabel('Game lengths \n (%)', fontsize='large')
def plot_selfplay_games_winrate(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.black_won_rate * 100,
color='black',
linewidth=FLAGS.line_width,
label='Black won',
)
ax.plot(
df.training_steps,
df.white_won_rate * 100,
color='gray',
linewidth=FLAGS.line_width,
label='White won',
)
ax.plot(
df.training_steps,
df.black_resign_rate * 100,
'--',
color='black',
linewidth=FLAGS.line_width,
label='Black resigned',
)
ax.plot(
df.training_steps,
df.white_resign_rate * 100,
'--',
color='gray',
linewidth=FLAGS.line_width,
label='White resigned',
)
ax.legend()
ax.set_ylabel('Win rate (%)', fontsize='large')
def plot_selfplay_resign_fp_ratio(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.resign_false_positive_rate * 100,
color='red',
linewidth=FLAGS.line_width,
label='False positive (estimated)',
)
ax.hlines(
y=0.05 * 100,
xmin=0,
xmax=max(df.training_steps),
color='black',
linewidth=FLAGS.line_width,
linestyle='--',
)
ax.set_ylabel('Resignation \n false positive (%)', fontsize='large')
def plot_selfplay_num_games(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.total_games,
color='steelblue',
linewidth=FLAGS.line_width,
label='Total',
)
ax.set_ylabel('Number of \n games', fontsize='large')
ax.yaxis.set_major_formatter(FuncFormatter(shorten))
def plot_selfplay_game_length(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.avg_steps_per_game,
color='steelblue',
linewidth=FLAGS.line_width,
label='Game length',
)
ax.plot(
df.training_steps,
df.avg_passes_per_game,
color='orange',
linewidth=FLAGS.line_width,
label='Number of passes',
)
ax.legend()
ax.set_ylabel('Avg steps \n per game', fontsize='large')
def plot_training_time(df, ax):
if df is not None:
ax.plot(df.training_steps, df.hours, linewidth=FLAGS.line_width, color='steelblue')
ax.set_ylabel('Training time (h)', fontsize='large')
def plot_training_samples(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.total_samples,
color='steelblue',
linewidth=FLAGS.line_width,
label='Total samples',
)
ax.set_ylabel('Training samples \n (total)', fontsize='large')
ax.yaxis.set_major_formatter(FuncFormatter(shorten))
def plot_training_lr(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.learning_rate,
linewidth=FLAGS.line_width,
color='steelblue',
)
ax.set_ylabel('Learning rate', fontsize='large')
def plot_training_value_loss(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.value_loss,
linewidth=FLAGS.line_width,
color='steelblue',
)
ax.set_ylabel('MSE loss', fontsize='large')
def plot_training_policy_loss(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.policy_loss,
linewidth=FLAGS.line_width,
color='steelblue',
)
ax.set_ylabel('Cross-entropy loss', fontsize='large')
def plot_eval_policy_entropy(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.policy_entropy,
linewidth=FLAGS.line_width,
color='steelblue',
)
ax.set_ylabel('Policy entropy', fontsize='large')
def plot_eval_mse_error(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.value_mse_error,
linewidth=FLAGS.line_width,
color='steelblue',
)
# scale by 1/4 to the range of 0-1
# ax.plot(df.training_steps, df.value_mse_error * 0.25, linewidth=FLAGS.line_width, color='steelblue')
ax.set_ylabel('MSE of professional \n game outcomes', fontsize='large')
def plot_eval_policy_accuracy(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.policy_top_1_accuracy * 100,
color='steelblue',
linewidth=FLAGS.line_width,
label='Top 1 accuracy',
)
ax.plot(
df.training_steps,
df.policy_top_3_accuracy * 100,
color='orange',
linewidth=FLAGS.line_width,
label='Top 3 accuracy',
)
ax.plot(
df.training_steps,
df.policy_top_5_accuracy * 100,
color='green',
linewidth=FLAGS.line_width,
label='Top 5 accuracy',
)
ax.legend()
ax.set_ylabel('Prediction accuracy \n on professional moves (%)', fontsize='large')
def plot_eval_elo_rating(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.black_elo_rating,
color='steelblue',
linewidth=FLAGS.line_width,
label='Elo rating',
)
ax.set_ylabel('Elo ratings', fontsize='large')
def plot_eval_game_length(df, ax):
if df is not None:
ax.plot(
df.training_steps,
df.game_length,
color='steelblue',
linewidth=FLAGS.line_width,
label='Game length',
)
ax.plot(
df.training_steps,
df.num_passes,
'--',
color='orange',
linewidth=FLAGS.line_width,
label='Number of passes',
)
ax.legend()
ax.set_ylabel('Evaluation \n game steps', fontsize='large')
if __name__ == '__main__':
app.run(main)