-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrained_player.py
244 lines (152 loc) · 5.85 KB
/
trained_player.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from player import Player
import tensorflow as tf
import functools
def lazy_property(fn):
attribute = '_cache_' + fn.__name__
@property
@functools.wraps(fn)
def decorator(self):
if not hasattr(self, attribute):
setattr(self, attribute, fn(self))
return getattr(self, attribute)
return decorator
def with_graph(fn):
@functools.wraps(fn)
def decorator(self, *args, **kwargs):
with self.graph.as_default():
return fn(self, *args, **kwargs)
return decorator
class TrainedPlayer(Player):
@with_graph
def __init__(self):
self.x = tf.placeholder(tf.float32, [None, 9], name="x")
self.y_ = tf.placeholder(tf.float32, [None, 9], name="y_")
self.prediction
self.optimize
self.accuracy
self._loaded = False
@lazy_property
def graph(self):
return tf.Graph()
@lazy_property
@with_graph
def prediction(self):
# Initialise weights with some noise, as suggested in Deep MNIST tutorial
W = tf.Variable(tf.truncated_normal([9, 9], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[9]), name="b")
y = tf.nn.softmax(tf.matmul(self.x, W) + b)
return y
@lazy_property
@with_graph
def cost(self):
# We will use tf.nn.softmax_cross_entropy_with_logits, which is a more stable version of this calc:
# cross_entropy = tf.reduce_mean(-tf.reduce_sum(self.y_ * tf.log(self.prediction), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y_, logits=self.prediction))
return cross_entropy
@lazy_property
@with_graph
def optimize(self):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(self.cost)
return train_step
@lazy_property
@with_graph
def accuracy(self):
correct_prediction = tf.equal(tf.argmax(self.prediction,1), tf.argmax(self.y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return accuracy
def get_model_path(self):
return "models/model_{0}.ckpt".format(self.name())
@with_graph
def save(self):
sess = tf.get_default_session()
saver = tf.train.Saver()
saver.save(sess, self.get_model_path())
@lazy_property
def infer_session(self):
return tf.Session(graph=self.graph)
@with_graph
def load(self):
sess = self.infer_session
saver = tf.train.Saver()
saver.restore(sess, self.get_model_path())
@with_graph
def do_move(self, board):
sess = self.infer_session
if not self._loaded:
self.load()
self._loaded = True
weights_deep = sess.run(self.prediction, feed_dict={self.x: [board]})
weights = weights_deep[0]
free_cells = self.get_free_cells(board)
d = dict(zip(range(0,len(weights)), weights))
d = [(a,d[a]) for a in free_cells]
d = sorted(d, key=lambda x: x[1], reverse=True)
return d[0][0]
class DeepTrainedPlayer(TrainedPlayer):
hidden_nodes = 10
@lazy_property
@with_graph
def prediction(self):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
W_layer1 = weight_variable([9, self.hidden_nodes])
b_layer1 = bias_variable([self.hidden_nodes])
W_layer2 = weight_variable([self.hidden_nodes, 9])
b_layer2 = bias_variable([9])
hidden_layer1 = tf.matmul(self.x, W_layer1) + b_layer1
hidden_layer2 = tf.nn.relu(hidden_layer1)
output_layer = tf.matmul(hidden_layer2, W_layer2) + b_layer2
y = tf.nn.softmax(output_layer)
return y
class DeepTrainedPlayer2x100(DeepTrainedPlayer):
hidden_nodes = 100
class DeepTrainedPlayer10x100(DeepTrainedPlayer):
hidden_layers = 10
hidden_nodes = 100
@lazy_property
@with_graph
def prediction(self):
output = self.x
for layer in range(self.hidden_layers):
output = tf.layers.dense(output, self.hidden_nodes, name="hidden{}".format(layer), activation=tf.nn.relu)
y = tf.layers.dense(output, 9, name="logits", activation=tf.nn.softmax)
return y
class DeepTrainedPlayerTF2x100(DeepTrainedPlayer10x100):
hidden_layers = 2
class DeepTrainedPlayer5x100(DeepTrainedPlayer10x100):
hidden_layers = 5
class DeepTrainedPlayer5x200(DeepTrainedPlayer10x100):
hidden_layers = 5
hidden_nodes = 200
class DeepTrainedPlayer5x300(DeepTrainedPlayer10x100):
hidden_layers = 5
hidden_nodes = 300
class DeepTrainedPlayer2x200(DeepTrainedPlayer10x100):
hidden_layers = 2
hidden_nodes = 200
class DeepTrainedPlayer2x200retrained(DeepTrainedPlayer2x200):
pass
class DeepTrainedPlayer2x200rt_drop(DeepTrainedPlayer2x200retrained):
hidden_layers = 2
hidden_nodes = 200
dropout_rate = 0.5
@lazy_property
@with_graph
def prediction(self):
self.training = tf.placeholder_with_default(False, shape=(), name="training")
output = self.x
output = tf.layers.dropout(output, self.dropout_rate, training=self.training)
for layer in range(self.hidden_layers):
output = tf.layers.dense(output, self.hidden_nodes, name="hidden{}".format(layer), activation=tf.nn.relu)
output = tf.layers.dropout(output, self.dropout_rate, training=self.training)
y = tf.layers.dense(output, 9, name="logits", activation=tf.nn.softmax)
return y
def validate_feed_dict(self, feed, training=False):
feed[self.training] = training
return feed
class DeepTrainedPlayer2x200drop(DeepTrainedPlayer2x200rt_drop):
pass