forked from pytorch/ELF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_lib.py
372 lines (311 loc) · 11.3 KB
/
console_lib.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
# Copyright (c) 2018-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import inspect
import traceback
from collections import Counter
def move2xy(v):
if v.lower() == "pass":
return -1, -1
x = ord(v[0].lower()) - ord('a')
# Skip 'i'
if x >= 9:
x -= 1
y = int(v[1:]) - 1
return x, y
def xy2move(x, y):
if x == -1 and y == -1:
return "pass"
if x >= 8:
x += 1
return chr(x + 65) + str(y + 1)
def plot_plane(v):
s = ""
for j in range(v.size(1)):
for i in range(v.size(0)):
if v[i, v.size(1) - 1 - j] != 0:
s += "o "
else:
s += ". "
s += "\n"
print(s)
def topk_accuracy2(batch, state_curr, topk=(1,)):
pi = state_curr["pi"]
import torch
if isinstance(pi, torch.autograd.Variable):
pi = pi.data
score, indices = pi.sort(dim=1, descending=True)
maxk = max(topk)
topn_count = [0] * maxk
for ind, gt in zip(indices, batch["offline_a"][0]):
for i in range(maxk):
if ind[i] == gt[0]:
topn_count[i] += 1
for i in range(maxk):
topn_count[i] /= indices.size(0)
return [topn_count[i - 1] for i in topk]
class GoConsole:
def __init__(self, GC, evaluator):
self.exit = False
self.GC = GC
self.board_size = GC.params["board_size"]
self.evaluator = evaluator
self.last_move_idx = None
def move2action(self, v):
if v.lower() == "pass":
return self.board_size ** 2
x, y = move2xy(v)
return x * self.board_size + y
def action2move(self, a):
if a == self.board_size ** 2:
return "pass"
x = a // self.board_size
y = a % self.board_size
return xy2move(x, y)
def check(self, batch):
reply = self.evaluator.actor(batch)
topk = topk_accuracy2(batch, reply, topk=(1, 2, 3, 4, 5))
for i, v in enumerate(topk):
self.check_stats[i] += v
if sum(topk) == 0:
self.check_stats[-1] += 1
def actor(self, batch):
reply = self.evaluator.actor(batch)
return reply
def showboard(self, batch):
print(batch.GC.getGame(0).showBoard())
def prompt(self, prompt_str, batch):
if self.last_move_idx is not None:
curr_move_idx = batch["move_idx"][0][0]
if curr_move_idx - self.last_move_idx == 1:
self.check(batch)
self.last_move_idx = curr_move_idx
return
else:
n = sum(self.check_stats.values())
print("#Move: " + str(n))
accu = 0
for i in range(5):
accu += self.check_stats[i]
print("Top %d: %.3f" % (i, accu / n))
self.last_move_idx = None
self.showboard(batch)
# Ask user to choose
while True:
if getattr(self, "repeat", 0) > 0:
self.repeat -= 1
cmd = self.repeat_cmd
else:
cmd = input(prompt_str)
items = cmd.split()
if len(items) < 1:
print("Invalid input")
c = items[0]
reply = dict(pi=None, a=None, V=0)
try:
if c == 'p':
reply["a"] = self.move2action(items[1])
return reply
elif c == 'c':
reply = self.evaluator.actor(batch)
return reply
elif c == "s":
channel_id = int(items[1])
plot_plane(batch["s"][0][0][channel_id])
elif c == "a":
reply = self.evaluator.actor(batch)
if "pi" in reply:
score, indices = reply["pi"].squeeze().sort(
dim=0, descending=True)
first_n = int(items[1])
for i in range(first_n):
print("%s: %.3f" %
(self.action2move(indices[i]), score[i]))
else:
print("No key \"pi\"")
elif c == "check":
print("Top %d" % self.check(batch))
elif c == 'check2end':
self.check_stats = Counter()
self.check(batch)
self.last_move_idx = batch["move_idx"][0][0]
if len(items) == 2:
self.repeat = int(items[1])
self.repeat_cmd = "check2end_cont"
return
elif c == "check2end_cont":
if not hasattr(self, "check_stats"):
self.check_stats = Counter()
self.check(batch)
self.last_move_idx = batch["move_idx"][0][0]
return
elif c == "aug":
print(batch["aug_code"][0][0])
elif c == "show":
self.showboard(batch)
elif c == "dbg":
import pdb
pdb.set_trace()
elif c == 'offline_a':
if "offline_a" in batch:
for i, offline_a in \
enumerate(batch["offline_a"][0][0]):
print(
"[%d]: %s" %
(i, self.action2move(offline_a)))
else:
print("No offline_a available!")
elif c == "exit":
self.exit = True
return reply
else:
print("Invalid input: " + cmd + ". Please try again")
except Exception as e:
print("Something wrong! " + str(e))
'''
elif c == "u":
batch.GC.undoMove(0)
self.showboard(batch)
elif c == "h":
handicap = int(items[1])
batch.GC.applyHandicap(0, handicap)
self.showboard(batch)
'''
class GoConsoleGTP:
def on_protocol_version(self, batch, items, reply):
return True, "2"
def on_clear_board(self, batch, items, reply):
reply["a"] = self.actions["clear"]
return True, reply
def on_name(self, batch, items, reply):
return True, "DF2"
def on_komi(self, batch, items, reply):
# For now we just fix komi number.
if items[1] != "7.5":
return False, "We only support 7.5 komi for now"
return True, None
def on_boardsize(self, batch, items, reply):
if items[1] != str(self.board_size):
return (
False,
"We only support %dx%d board for now" % (
self.board_size, self.board_size)
)
return True, None
def on_genmove(self, batch, items, reply):
ret, msg = self.check_player(batch, items[1][0])
if ret:
reply["a"] = self.actions["skip"]
return True, reply
else:
return False, msg
def on_play(self, batch, items, reply):
ret, msg = self.check_player(batch, items[1][0])
if ret:
reply["a"] = self.move2action(items[2])
return True, reply
else:
return False, msg
def on_showboard(self, batch, items, reply):
self.showboard(batch)
return True, None
def on_final_score(self, batch, items, reply):
final_score = self.get_final_score(batch)
if final_score > 0:
return True, "B+%.1f" % final_score
else:
return True, "W+%.1f" % (-final_score)
def on_version(self, batch, items, reply):
return True, "1.0"
def on_exit(self, batch, items, reply):
self.exit = True
return True, reply
def on_quit(self, batch, items, reply):
return self.on_exit(batch, items, reply)
def on_list_commands(self, batch, items, reply):
msg = "\n".join(self.commands.keys())
return True, msg
def __init__(self, GC, evaluator):
self.exit = False
self.GC = GC
self.board_size = GC.params["board_size"]
self.evaluator = evaluator
self.actions = {
"skip": GC.params["ACTION_SKIP"],
"pass": GC.params["ACTION_PASS"],
"resign": GC.params["ACTION_RESIGN"],
"clear": GC.params["ACTION_CLEAR"]
}
self.last_cmd = ""
self.commands = {
key[3:]: func
for key, func in inspect.getmembers(
self, predicate=inspect.ismethod)
if key.startswith("on_")
}
def move2action(self, v):
if v.lower() in self.actions:
return self.actions[v.lower()]
x, y = move2xy(v)
return x * self.board_size + y
def actor(self, batch):
reply = self.evaluator.actor(batch)
return reply
def action2move(self, a):
x = a // self.board_size
y = a % self.board_size
return xy2move(x, y)
def showboard(self, batch):
print(batch.GC.getGame(0).showBoard())
def get_next_player(self, batch):
return batch.GC.getGame(0).getNextPlayer()
def get_last_move(self, batch):
return batch.GC.getGame(0).getLastMove()
def get_final_score(self, batch):
return batch.GC.getGame(0).getLastScore()
def check_player(self, batch, player):
board_next_player = self.get_next_player(batch)
if player.lower() != board_next_player.lower():
return (
False,
("Specified next player %s is not the same as the "
"next player %s on the board") % (
player, board_next_player
)
)
else:
return True, None
def print_msg(self, ret, msg):
print("\n%s %s\n\n" % (("=" if ret else "?"), msg))
def prompt(self, prompt_str, batch):
# Show last command results.
if self.last_cmd == "play" or self.last_cmd == "clear_board":
self.print_msg(True, "")
elif self.last_cmd == "genmove":
self.print_msg(True, self.get_last_move(batch))
self.last_cmd = ""
while True:
cmd = input(prompt_str)
items = cmd.split()
if len(items) < 1:
self.print_msg(False, "Invalid input")
continue
c = items[0]
reply = dict(pi=None, a=None, V=0)
try:
ret, msg = self.commands[c](batch, items, reply)
self.last_cmd = c
if not ret:
self.print_msg(False, msg)
else:
if isinstance(msg, dict):
return msg
elif isinstance(msg, str):
self.print_msg(True, msg)
else:
self.print_msg(True, "")
except Exception:
print(traceback.format_exc())
self.print_msg(False, "Invalid command")