-
Notifications
You must be signed in to change notification settings - Fork 18
/
plot_gomoku.py
345 lines (275 loc) · 10.7 KB
/
plot_gomoku.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
# 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/gomoku/13x13', '')
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_15_step_count'] = df.apply(lambda row: 1 if row['game_length'] < 15 else 0, axis=1)
df['game_15_to_30_step_count'] = df.apply(
lambda row: 1 if row['game_length'] >= 15 and row['game_length'] <= 30 else 0,
axis=1,
)
df['game_gt_30_step_count'] = df.apply(lambda row: 1 if row['game_length'] > 30 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,
)
# 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()
# Compute accumulative sum
grouped_df['total_games'] = grouped_df['game_count'].cumsum()
grouped_df['games_lt_15_steps'] = grouped_df['game_lt_15_step_count'].cumsum()
grouped_df['games_15_to_30_steps'] = grouped_df['game_15_to_30_step_count'].cumsum()
grouped_df['games_gt_30_steps'] = grouped_df['game_gt_30_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()
# Other ratio
grouped_df['steps_lt_15_rate'] = grouped_df['games_lt_15_steps'] / grouped_df['total_games']
grouped_df['steps_15_to_30_rate'] = grouped_df['games_15_to_30_steps'] / grouped_df['total_games']
grouped_df['steps_gt_30_rate'] = grouped_df['games_gt_30_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']
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(4, 1, sharex=True)
axs_train = subfigs[1].subplots(5, 1, sharex=True)
axs_eval = subfigs[2].subplots(2, 1, sharex=True)
# Self-play statistics
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_games_winrate(selfplay_df, ax)
elif i == 3:
if selfplay_df is not None:
plot_selfplay_games_precentage(selfplay_df, ax)
ax.set_xlabel('Training steps', fontsize='large')
# Training statistics
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')
# Evaluation statistics
for i, ax in enumerate(axs_eval):
ax.xaxis.set_major_formatter(FuncFormatter(shorten))
if i == 0:
if eval_df is not None:
plot_eval_game_length(eval_df, ax)
elif i == 1:
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_15_rate * 100,
color='steelblue',
linewidth=FLAGS.line_width,
label='< 15 steps',
)
ax.plot(
df.training_steps,
df.steps_15_to_30_rate * 100,
color='purple',
linewidth=FLAGS.line_width,
label='15 - 30 steps',
)
ax.plot(
df.training_steps,
df.steps_gt_30_rate * 100,
color='orange',
linewidth=FLAGS.line_width,
label='> 30 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.legend()
ax.set_ylabel('Win rate (%)', 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,
)
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,
color='steelblue',
linewidth=FLAGS.line_width,
)
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,
color='steelblue',
linewidth=FLAGS.line_width,
)
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,
color='steelblue',
linewidth=FLAGS.line_width,
)
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,
color='steelblue',
linewidth=FLAGS.line_width,
)
ax.set_ylabel('Cross-entropy loss', 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,
)
ax.set_ylabel('Evaluation \n game steps', fontsize='large')
if __name__ == '__main__':
app.run(main)