-
Notifications
You must be signed in to change notification settings - Fork 9
/
alphago-zero.py
66 lines (48 loc) · 1.52 KB
/
alphago-zero.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
#!/usr/bin/python
import keras
from keras.layers import Activation
from keras.layers import BatchNormalization
from keras.layers import Conv2D
from keras.layers import Input, Dense
from keras.models import Model
input_data = Input(shape=(19, 19, 17))
def conv_block(x):
y = Conv2D(256, (3, 3), padding='same')(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
return y
def residual_block(x):
y = Conv2D(256, (3, 3), padding='same')(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(256, (3, 3), padding='same')(y)
y = BatchNormalization()(y)
y = keras.layers.add([x, y])
y = Activation('relu')(y)
return y
def policy_head(x):
y = Conv2D(2, (1, 1), padding='same')(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Dense(19 ** 2 + 1, activation='sigmoid')(y)
return y
def value_head(x):
y = Conv2D(1, (1, 1), padding='same')(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Dense(256)(y)
y = Activation('relu')(y)
y = Dense(1)(y)
y = Activation('tanh')(y)
return y
# in the paper there were either 39 or 19 residual blocks
def alphago_zero_nn(residual_blocks=39):
x = conv_block(input_data)
for i in range(residual_blocks):
x = residual_block(x)
policy_out = policy_head(x)
value_out = value_head(x)
model = Model(inputs=[input_data], outputs=[policy_out, value_out])
return model
model_alphago_zero = alphago_zero_nn()
print(model_alphago_zero.summary())