-
Notifications
You must be signed in to change notification settings - Fork 1
/
neural_nets.py
225 lines (178 loc) · 9.27 KB
/
neural_nets.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
'''
neural_nets.py contains various network structure, including linear model, dense model, conv model and residual net
model
'''
from keras.layers import Input, Reshape, Dense, Conv2D, BatchNormalization, Activation, Flatten, Dropout, Lambda, Multiply, Add
from keras.models import Model
from keras.optimizers import Adam
from keras.regularizers import l2
import keras.backend as K
'''
# Possible alternate loss function
def combined_loss(args):
is_put, pi_put, pi_cap, v = args
not_is_put = Lambda(lambda x: (x - 1) * -1)
put_loss = categorical_crossentropy(
cap_loss =
v_loss =
return is_put * put_loss + not_is_put * cap_loss + v_loss
# This part would go in each model
custom_loss = Lambda(combined_loss, output_shape=(1,), name='combined_loss')([inputs, self.pi_put, self.pi_capture, self.v])
'''
class LinearModel(object):
'''
A linear model takes in a state and estimates the corresponding pi_put, pi_capture and v
'''
def __init__(self, game, config):
'''
Game, an object, needs to have the following attributes:
game.getBoardSize() -> a tuple like (4, 4)
game.getActionSize() -> a tuple of (putActionSize, captureActionSize), where each of the actionSize is a tuple
- putActionSize = (16, 17, 3) * three dimensional
- captureActionSize = (16, 6) * two dimensional
game.getStateDepth() -> int. How deep is each state, such as 11
:param game: A game object
:param config: A config object. It's by default the config.py
'''
self.state_depth, self.board_x, self.board_y = game.board.state.shape
self.put_action_size = game.get_placement_action_size()
self.capture_action_size = game.get_capture_action_size()
self.config = config
inputs = Input(shape=(self.state_depth, self.board_x, self.board_y), name="inputs")
hidden = Flatten()(inputs)
hidden = Dense(self.config.hidden_size, activation='linear')(hidden)
self.pi = Dense(self.put_action_size + self.capture_action_size, activation='softmax', name='pi')(hidden)
self.v = Dense(1, activation='tanh', name='v')(hidden)
self.model = Model(inputs=[inputs], outputs=[self.pi, self.v])
self.model.compile(loss=['categorical_crossentropy', 'mean_squared_error'],
optimizer=Adam(self.config.lr))
class DenseModel(object):
'''
Fully connected neural networks. Number of layers is decided by config.num_layers
'''
def __init__(self, game, config):
'''
Game, an object, needs to have the following attributes:
game.getBoardSize() -> a tuple like (4, 4)
game.getActionSize() -> a tuple of (putActionSize, captureActionSize), where each of the actionSize is a tuple
- putActionSize = (16, 17, 3) * three dimensional
- captureActionSize = (16, 6) * two dimensional
game.getStateDepth() -> int. How deep is each state, such as 11
:param game: A game object
:param config: A config object. It's by default the config.py
'''
self.state_depth, self.board_x, self.board_y = game.board.state.shape
self.put_action_size = game.get_placement_action_size()
self.capture_action_size = game.get_capture_action_size()
self.config = config
inputs = Input(shape=(self.state_depth, self.board_x, self.board_y), name="inputs")
hidden = Flatten()(inputs)
for i in range(self.config.num_layers):
hidden = Dense(self.config.hidden_size, activation='relu')(hidden)
self.pi = Dense(self.put_action_size + self.capture_action_size, activation='softmax', name='pi')(hidden)
self.v = Dense(1, activation='tanh', name='v')(hidden)
self.model = Model(inputs=[inputs], outputs=[self.pi, self.v])
self.model.compile(loss=['categorical_crossentropy', 'mean_squared_error'],
optimizer=Adam(self.config.lr))
class ConvModel(object):
'''
A convolution NN. filters are decided by config.num_filters and config.kernel_size
'''
def __init__(self, game, config):
'''
Game, an object, needs to have the following attributes:
game.getBoardSize() -> a tuple like (4, 4)
game.getActionSize() -> a tuple of (putActionSize, captureActionSize), where each of the actionSize is a tuple
- putActionSize = (16, 17, 3) * three dimensional
- captureActionSize = (16, 6) * two dimensional
game.getStateDepth() -> int. How deep is each state, such as 11
:param game: A game object
:param config: A config object. It's by default the config.py
'''
self.state_depth, self.board_x, self.board_y = game.board.state.shape
self.put_action_size = game.get_placement_action_size()
self.capture_action_size = game.get_capture_action_size()
self.config = config
inputs = Input(shape=(self.state_depth, self.board_x, self.board_y), name="inputs")
hidden = inputs
for i in range(self.config.num_layers):
# Changed axis to 1 since channels first
hidden = Activation('relu')(BatchNormalization(axis=1)(Conv2D(self.config.num_filters,
self.config.kernel_size,
padding='same',
data_format='channels_first',
kernel_regularizer=l2(self.config.regularizer))(hidden)))
hidden = Flatten()(hidden)
hidden = Dropout(self.config.dropout)(Activation('relu')(BatchNormalization(axis=1)(Dense(1024)(hidden))))
hidden = Dropout(self.config.dropout)(Activation('relu')(BatchNormalization(axis=1)(Dense(512)(hidden))))
self.pi = Dense(self.put_action_size + self.capture_action_size, activation='softmax', name='pi')(hidden)
self.v = Dense(1, activation='tanh', name='v')(hidden)
self.model = Model(inputs=[inputs], outputs=[self.pi, self.v])
self.model.compile(loss=['categorical_crossentropy', 'mean_squared_error'], optimizer=Adam(self.config.lr))
class ResNet(object):
'''
ResNet!!
'''
def __init__(self, game, config):
self.config = config
self.game = game
def bn_relu(self, input):
norm = BatchNormalization(axis=1)(input)
return Activation('relu')(norm)
def conv_bn_relu(self, num_filters, kernel_size, strides):
def f(input):
out = Conv2D(filters=num_filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
data_format='channels_first',
kernel_regularizer=l2(self.config.regularizer))(input)
return self.bn_relu(out)
return f
def bn_conv_relu(self, num_filters, kernel_size, strides):
def f(input):
out = self.bn_relu(input)
return Conv2D(filters=num_filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
data_format='channels_first',
kernel_regularizer=l2(self.config.regularizer))(out)
def short_cut(self, input, residual):
input_shape = K.int_shape(input)
residual_shape = K.int_shape(residual)
rate = int(input_shape[2] / residual_shape[2])
if input_shape != residual_shape:
shortcut = Conv2D(filters=residual_shape[0],
kernel_size=1,
strides=rate,
padding='valid',
data_format='channels_first',
kernel_regularizer=l2(self.config.regularizer))(input)
else:
shortcut = input
return Add([shortcut, residual])
def residual_block(self, num_filters, kernel_size, first_block=False, increase_dim=False):
def f(input):
if first_block:
out = Conv2D(filters=num_filters,
kernel_size=kernel_size,
strides=1,
padding='same',
data_format='channels_first',
kernel_regularizer=l2(self.config.regularizer))(input)
else:
out = self.bn_conv_relu(num_filters=num_filters, kernel_size=kernel_size, strides=1 + int(1+increase_dim))(input)
out = self.bn_conv_relu(num_filters=num_filters, kernel_size=kernel_size, strides=1)(out)
return self.short_cut(input=input, residual=out)
return f
def build_graph(self):
self.state_depth, self.board_x, self.board_y = self.game.board.state.shape
self.put_action_size = self.game.get_placement_action_size()
self.capture_action_size = self.game.get_capture_action_size()
inputs = Input(shape=(self.state_depth, self.board_x, self.board_y), name="inputs")
hidden = inputs
hidden = self.conv_bn_relu(num_filters=16, kernel_size=2, strides=1)
for i in range(self.config.num_residual_blocks):
# hidden = self.residual_block(num_filters=16, )
pass