model.py
4.81 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
import config
import numpy as np
from keras.models import load_model, Model
from keras.layers import Input, Dense, Conv1D, Flatten, BatchNormalization, LeakyReLU, add
from keras.optimizers import SGD
from keras import regularizers
from DNN.loss import softmax_cross_entropy_with_logits
class GeneralModel(object):
def __init__(self, reg_const, learning_rate, input_dim, output_dim):
self.reg_const = reg_const
self.learning_rate = learning_rate
self.input_dim = input_dim
self.output_dim = output_dim
def predict(self, x):
return self.model.predict(x)
def fit(self, states, targets, epochs, verbose, validation_split, batch_size):
return self.model.fit(states, targets, epochs=epochs, verbose=verbose, validation_split=validation_split,
batch_size=batch_size)
def write(self, game, version):
self.model.save('models/version' + "{0:0>4}".format(version) + '.h5')
def read(self, game, run_number, version):
return load_model(str(run_number).zfill(4) + "/models/version" + "{0:0>4}".format
(version) + '.h5', custom_objects={'softmax_cross_entropy_with_logits': softmax_cross_entropy_with_logits})
class ResidualCNN(GeneralModel):
def __init__(self, reg_const, learning_rate, input_dim, output_dim, hidden_layers):
GeneralModel.__init__(self, reg_const, learning_rate, input_dim, output_dim)
self.hidden_layers = hidden_layers
self.num_layers = len(hidden_layers)
self.model = self._build_model()
def residual_layer(self, input_block, filters, kernel_size):
x = self.conv_layer(input_block, filters, kernel_size)
x = Conv1D(
filters=filters
, kernel_size=kernel_size
, data_format="channels_last"
, padding='same'
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
)(x)
x = BatchNormalization(axis=1)(x)
x = add([input_block, x])
x = LeakyReLU()(x)
return x
def conv_layer(self, x, filters, kernel_size):
x = Conv1D(
filters=filters
, kernel_size=kernel_size
, data_format="channels_last"
, padding='same'
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
)(x)
x = BatchNormalization(axis=1)(x)
x = LeakyReLU()(x)
return x
def value_head(self, x):
x = Conv1D(
filters=2
, kernel_size=1
, data_format="channels_last"
, padding='same'
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
)(x)
x = BatchNormalization(axis=1)(x)
x = LeakyReLU()(x)
x = Flatten()(x)
x = Dense(
self.output_dim
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
, name='value_head'
)(x)
return x
def policy_head(self, x):
x = Conv1D(
filters=2
, kernel_size=1
, data_format="channels_last"
, padding='same'
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
)(x)
x = BatchNormalization(axis=1)(x)
x = LeakyReLU()(x)
x = Flatten()(x)
x = Dense(
self.output_dim
, use_bias=False
, activation='linear'
, kernel_regularizer=regularizers.l2(self.reg_const)
, name='policy_head'
)(x)
return x
def _build_model(self):
# image shape
# 그냥 배열 shape
main_input = Input(shape=self.input_dim, name='main_input')
x = self.conv_layer(main_input, self.hidden_layers[0]['filters'], self.hidden_layers[0]['kernel_size'])
if len(self.hidden_layers) > 1:
for h in self.hidden_layers[1:]:
x = self.residual_layer(x, h['filters'], h['kernel_size'])
vh = self.value_head(x)
ph = self.policy_head(x)
model = Model(inputs=[main_input], outputs=[ph])
model.compile(loss=softmax_cross_entropy_with_logits,
optimizer=SGD(lr=self.learning_rate, momentum=config.MOMENTUM),
loss_weights=[0.5]
)
return model
def convertToModelInput(self, state):
# [20,20]
inputToModel = state.state_check # np.append(state.binary, [(state.playerTurn + 1)/2] * self.input_dim[1] * self.input_dim[2])
inputToModel = np.reshape(inputToModel, self.input_dim)
return inputToModel