forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
268 lines (213 loc) · 8.79 KB
/
Copy pathfeatures.py
File metadata and controls
268 lines (213 loc) · 8.79 KB
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
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Features used by AlphaGo Zero, in approximate order of importance.
Feature # Notes
Stone History 16 The stones of each color during the last 8 moves.
Ones 1 Constant plane of 1s
All features with 8 planes are 1-hot encoded, with plane i marked with 1
only if the feature was equal to i. Any features >= 8 would be marked as 8.
This file includes the features from the first paper as DEFAULT_FEATURES
and the features from AGZ as AGZ_FEATURES.
"""
import numpy as np
import go
import utils
# Resolution/truncation limit for one-hot features
P = 8
def make_onehot(feature, planes: int):
""" onehot_features[:,:,i] = 1 iff a point has i+1 liberty; the last plane = 1 if a point has liberty >=i+1
"""
onehot_features = np.zeros(feature.shape + (planes,), dtype=np.uint8)
capped = np.minimum(feature, planes)
onehot_index_offsets = np.arange(0, utils.product(
onehot_features.shape), planes) + capped.ravel()
# A 0 is encoded as [0,0,0,0], not [1,0,0,0], so we'll
# filter out any offsets that are a multiple of $planes
# A 1 is encoded as [1,0,0,0], not [0,1,0,0], so subtract 1 from offsets
nonzero_elements = (capped != 0).ravel()
nonzero_index_offsets = onehot_index_offsets[nonzero_elements] - 1
onehot_features.ravel()[nonzero_index_offsets] = 1
return onehot_features
def planes(num_planes):
def deco(f):
f.planes = num_planes
return f
return deco
# TODO(tommadams): add a generic stone_features for all N <= 8
@planes(16)
def stone_features(position: go.Position):
""" AGZ: 8 binary feature planes each for black / white: latest 8 board positions """
# a bit easier to calculate it with axis 0 being the 16 board states,
# and then roll axis 0 to the end.
features = np.zeros([16, go.N, go.N], dtype=np.uint8)
num_deltas_avail = position.board_deltas.shape[0]
cumulative_deltas = np.cumsum(position.board_deltas, axis=0)
last_eight = np.tile(position.board, [8, 1, 1])
# apply deltas to compute previous board states
last_eight[1:num_deltas_avail + 1] -= cumulative_deltas
# if no more deltas are available, just repeat oldest board.
last_eight[num_deltas_avail + 1:] = last_eight[num_deltas_avail].reshape(1, go.N, go.N)
features[::2] = last_eight == position.to_play
features[1::2] = last_eight == -position.to_play
return np.rollaxis(features, 0, 3)
@planes(8)
def recent_board_features(position: go.Position):
""" recent 8 boards (each with 1/0/-1), latest board last """
num_deltas_avail = position.board_deltas.shape[0]
cumulative_deltas = np.cumsum(position.board_deltas, axis=0)
last_eight = np.tile(position.board, [8, 1, 1])
# apply deltas to compute previous board states
last_eight[1:num_deltas_avail + 1] -= cumulative_deltas
# if no more deltas are available, just repeat oldest board.
last_eight[num_deltas_avail + 1:] = last_eight[num_deltas_avail].reshape(1, go.N, go.N)
return np.moveaxis(last_eight[::-1], 0, -1)
# TODO(tommadams): add a generic stone_features for all N <= 8
@planes(8)
def stone_features_4(position):
# a bit easier to calculate it with axis 0 being the 16 board states,
# and then roll axis 0 to the end.
features = np.zeros([8, go.N, go.N], dtype=np.uint8)
num_deltas_avail = position.board_deltas.shape[0]
cumulative_deltas = np.cumsum(position.board_deltas, axis=0)
last = np.tile(position.board, [4, 1, 1])
# apply deltas to compute previous board states
last[1:num_deltas_avail + 1] -= cumulative_deltas
# if no more deltas are available, just repeat oldest board.
last[num_deltas_avail + 1:] = last[num_deltas_avail].reshape(1, go.N, go.N)
features[::2] = last == position.to_play
features[1::2] = last == -position.to_play
return np.rollaxis(features, 0, 3)
@planes(1)
def color_to_play_feature(position):
if position.to_play == go.BLACK:
return np.ones([go.N, go.N, 1], dtype=np.uint8)
else:
return np.zeros([go.N, go.N, 1], dtype=np.uint8)
@planes(3)
def stone_color_feature(position):
""" this feature is from the pov of current player """
board = position.board
features = np.zeros([go.N, go.N, 3], dtype=np.uint8)
if position.to_play == go.BLACK:
features[board == go.BLACK, 0] = 1
features[board == go.WHITE, 1] = 1
else:
features[board == go.WHITE, 0] = 1
features[board == go.BLACK, 1] = 1
features[board == go.EMPTY, 2] = 1
return features
@planes(1)
def ones_feature(position):
return np.ones([go.N, go.N, 1], dtype=np.uint8)
@planes(P)
def recent_move_feature(position):
onehot_features = np.zeros([go.N, go.N, P], dtype=np.uint8)
for i, player_move in enumerate(reversed(position.recent[-P:])):
_, move = player_move # unpack the info from position.recent
if move is not None:
onehot_features[move[0], move[1], i] = 1
return onehot_features
@planes(3)
def recent_move_feature3(position):
onehot_features = np.zeros([go.N, go.N, 3], dtype=np.uint8)
for i, player_move in enumerate(reversed(position.recent[-3:])):
_, move = player_move # unpack the info from position.recent
if move is not None:
onehot_features[move[0], move[1], i] = 1
return onehot_features
@planes(P)
def liberty_feature(position):
return make_onehot(position.get_liberties(), P)
@planes(3)
def liberty_feature3(position: go.Position):
""" liberties for both black/white """
return make_onehot(position.get_liberties(), 3)
@planes(3)
def few_liberties_feature(position: go.Position):
""" indicates where our stones in danger, see tests """
feature = position.get_liberties()
onehot_features = np.zeros(feature.shape + (3,), dtype=np.uint8)
onehot_index_offsets = np.arange(0, utils.product(
onehot_features.shape), 3) + feature.ravel()
nonzero_elements = ((feature != 0) & (feature <= 3)).ravel()
nonzero_index_offsets = onehot_index_offsets[nonzero_elements] - 1
onehot_features.ravel()[nonzero_index_offsets] = 1
return onehot_features
@planes(1)
def would_capture_feature(position):
""" binary feature: 1 if playing there results in capturing of enemy stones
Equivalently, the empty spot near enemy group of liberty 1
"""
features = np.zeros([go.N, go.N, 1], dtype=np.uint8)
for g in position.lib_tracker.groups.values():
if g.color == position.to_play:
continue
if len(g.liberties) == 1:
lib = next(iter(g.liberties))
features[lib + (0,)] = 1
return features
DEFAULT_FEATURES = [
stone_color_feature,
ones_feature,
liberty_feature,
recent_move_feature,
would_capture_feature,
]
DEFAULT_FEATURES_PLANES = sum(f.planes for f in DEFAULT_FEATURES)
AGZ_FEATURES = [
stone_features,
color_to_play_feature
]
AGZ_FEATURES_PLANES = sum(f.planes for f in AGZ_FEATURES)
MLPERF07_FEATURES = [
stone_features_4,
color_to_play_feature,
few_liberties_feature,
would_capture_feature,
]
MLPERF07_FEATURES_PLANES = sum(f.planes for f in MLPERF07_FEATURES)
""" DLGO sevenplane: black liberties, white liberties, ko violation """
DLGO_FEATURES = [
stone_color_feature,
# ones_feature,
liberty_feature3,
color_to_play_feature
]
DLGO_FEATURES_PLANES = sum(f.planes for f in DLGO_FEATURES)
""" redux: based on DEFAULT_FEATURES. Used in 9x9 exp2 """
REDUX_FEATURES = [
stone_color_feature,
color_to_play_feature,
liberty_feature3,
recent_move_feature3,
would_capture_feature,
]
REDUX_FEATURES_PLANES = sum(f.planes for f in REDUX_FEATURES) # 11
""" a0jax: """
A0JAX_FEATURES = [
recent_board_features,
ones_feature
]
A0JAX_FEATURES_PLANES = sum(f.planes for f in REDUX_FEATURES)
""" REDUX_FEATURES + focus area. Used in 9x9 exp3 """
EXP3_FEATURES = REDUX_FEATURES
EXP3_FEATURES_PLANES = 1 + sum(f.planes for f in EXP3_FEATURES) # 12
def extract_features(position, features, goal: np.array=None):
""" goal right now is the mask: go.N * go.N """
pos_features = np.concatenate([feature(position) for feature in features], axis=2)
if goal is None:
return pos_features
goal_features = np.expand_dims(goal, axis=2)
return np.concatenate([pos_features, goal_features], axis=2)