-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
70 lines (52 loc) · 2.07 KB
/
model.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
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
def get_model(input_size, output_size, fs=128, layers_deep=3, kernel_size=3):
inputs = keras.Input(shape=input_size)
x = inputs
x = layers.Conv2D(fs, kernel_size, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
for _ in range(layers_deep):
start_x = x
x = layers.Conv2D(fs, kernel_size, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(fs, kernel_size, strides=1, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
# skip connection
x = layers.Add()([x, start_x])
x = layers.ReLU()(x)
# policy_head
x_pol = layers.Conv2D(32, 1, strides=1, padding='same', kernel_regularizer=keras.regularizers.l2())(x)
x_pol = layers.BatchNormalization()(x_pol)
x_pol = layers.ReLU()(x_pol)
x_pol = layers.Flatten()(x_pol)
pol = layers.Dense(output_size, activation='softmax', name='pol-head')(x_pol)
# value head
x_val = layers.Conv2D(16, 1, strides=1, padding='same', kernel_regularizer=keras.regularizers.l2())(x)
x_val = layers.BatchNormalization()(x_val)
x_val = layers.ReLU()(x_val)
x_val = layers.Flatten()(x_val)
x_val = layers.Dense(128, activation='relu', kernel_regularizer=keras.regularizers.l2())(x_val)
val = layers.Dense(1, activation='tanh', name='val-head')(x_val)
model = keras.Model(inputs=inputs, outputs=[pol, val], name='mini-zero')
loss = {
"pol-head": keras.losses.categorical_crossentropy,
"val-head": keras.losses.MSE
}
loss_weights = {
"pol-head": 1.0,
"val-head": 1.0
}
model.compile(
optimizer=keras.optimizers.SGD(lr=1e-2, nesterov=True),
loss=loss,
loss_weights=loss_weights
)
return model
if __name__ == '__main__':
model = get_model((10, 10, 2), 100)
print(model.summary())
# keras.utils.plot_model(model, 'mini-zero.png')