-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueNet.py
218 lines (159 loc) · 7.15 KB
/
valueNet.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
import numpy as np
from keras.layers import Input, Flatten, Conv2D, Dense, ReLU, add, LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import regularizers
from keras import backend as K
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
from sklearn.utils import shuffle
from sgfmill import ascii_boards
from sgfmill import sgf
from sgfmill import sgf_moves
import utils
class ValueNet():
"""Value network"""
def __init__(self, weights_path=False):
inputs = Input(shape=(3, 19, 19, ))
# Convolutional block
x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(inputs)
x = BatchNormalization()(x)
x = ReLU()(x)
# Residual block
for i in range(11):
x = self.residual_block(x)
# Value head
v = Conv2D(1, (1, 1), strides=(1, 1), padding='same', kernel_regularizer=regularizers.l2(0.0001))(x)
v = BatchNormalization()(v)
v = ReLU()(v)
v = Flatten()(v)
v = Dense(256, kernel_regularizer=regularizers.l2(0.0001))(v)
v = ReLU()(v)
v = Dense(1, activation = 'tanh', kernel_regularizer=regularizers.l2(0.0001))(v)
self.model = Model(inputs=inputs, outputs=v)
if weights_path:
self.model.load_weights(weights_path)
def residual_block(self, s):
# Residual block
shortcut = s
s = Conv2D(256, (3, 3), strides=(1, 1), padding='same', kernel_regularizer=regularizers.l2(0.0001))(s)
s = BatchNormalization()(s)
s = ReLU()(s)
s = Conv2D(256, (3, 3), strides=(1, 1), padding='same', kernel_regularizer=regularizers.l2(0.0001))(s)
s = BatchNormalization()(s)
s = add([shortcut, s])
s = ReLU()(s)
return s
def loss(self, y_true, y_pred):
return K.categorical_crossentropy(y_true, y_pred)
def predict_from_board(self, board, next_player):
if next_player not in ['b', 'w']:
raise ValueError
np_board = np.asarray(board.board)
b_board = (np_board == "b").astype(int)
w_board = (np_board == "w").astype(int)
if next_player == 'w':
whose_turn = np.zeros((19,19))
else:
whose_turn = np.full((19,19), 1)
# Create the feature array black board, white board, next player
features = np.stack([b_board, w_board, whose_turn], axis=0)
features = np.expand_dims(features, axis=0)
pred = self.model.predict(features)
return pred[0]
def generator(self, batch_size, paths):
current_idx = 0
while True:
data_to_return, label_to_return = [], []
while len(data_to_return) < batch_size:
path = paths[current_idx]
data, label = self.read_sgf(path)
if data is not False:
data_to_return.append(data)
label_to_return.append(label)
current_idx += 1
if current_idx >= len(paths):
current_idx = 0
yield np.asarray(data_to_return), np.asarray(label_to_return)
def read_sgf(self, file_path):
# Open the game and setup the sgf object
with open(file_path, "rb") as fp:
sgf_src = fp.read()
try:
sgf_game = sgf.Sgf_game.from_bytes(sgf_src)
board, plays = sgf_moves.get_setup_and_moves(sgf_game)
except ValueError:
return False, False
# Choose a turn at random from which the net will have to predict the winner
length_game = len(plays)
if length_game:
turn = np.random.randint(0, length_game)
else:
return False, False
# Read the game until the play
_ = plays[:turn]
for play in _:
board = utils.play_turn_train(board, play)
if board is False:
return False, False
np_board = np.asarray(board.board)
b_board = (np_board == "b").astype(int)
w_board = (np_board == "w").astype(int)
if plays[turn][0] == 'b':
whose_turn = np.full((19,19), 1)
elif plays[turn][0] == 'w':
whose_turn = np.zeros((19,19))
else:
return False, False
# Create the feature array black board, white board, next player
features = np.stack([b_board, w_board, whose_turn], axis=0)
# Label is one-hot of winner: black/white
winner = utils.get_winner(file_path)
if winner == "b":
label = [1.]
elif winner == "w":
label = [-1.]
else:
return False, False
return features, label
def train(self, sgf_paths, batch_size=2048, epochs=100, lr=0.01, freeze=None):
gen_train = self.generator(batch_size, shuffle(sgf_paths))
gen_test = self.generator(batch_size, shuffle(sgf_paths))
optimizer = SGD(lr=lr, momentum=0.9, decay=0., nesterov=True, clipnorm=1.)
checkp = ModelCheckpoint(filepath="weights_ValueNet.h5", verbose=1, save_best_only=True)
self.model.compile(loss='mse', optimizer=optimizer, metrics=['mse', 'acc'])
hist = self.model.fit_generator(gen_train,
steps_per_epoch = 1000,
epochs = epochs,
shuffle = True,
verbose = 1,
validation_data = gen_test,
validation_steps = 50,
callbacks = [checkp])
return hist
def test_accuracy(self, sgf_paths, sample_size=1000):
correct = 0
total = 0
while total != sample_size:
path = np.random.choice(sgf_paths, 1)[0]
try:
with open(path, "rb") as fp:
sgf_src = fp.read()
sgf_game = sgf.Sgf_game.from_bytes(sgf_src)
board, plays = sgf_moves.get_setup_and_moves(sgf_game)
length_game = len(plays)
turn = np.random.randint(0, length_game)
_ = plays[:turn]
for play in _:
board = utils.play_turn_train(board, play)
pred = self.predict_from_board(board, plays[turn][0])
print(pred)
pred_winner = np.argmax(pred)
winner = utils.get_winner(path)
label = 0 if winner == "b" else 1
if pred_winner == label:
correct += 1
total += 1
except:
pass
return correct/total