/
ConvolutionalNetwork.py
60 lines (41 loc) · 2.77 KB
/
ConvolutionalNetwork.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
# add this at the top of the file
import tensorflow as tf
# Actions are down, stay still, up
class Convolutional_Network():
ACTIONS_COUNT = 3
STATE_FRAMES = 4
RESIZED_SCREEN_X, RESIZED_SCREEN_Y = 80, 80
@staticmethod
def _create_network():
# network weights
convolution_weights_1 = tf.Variable(tf.truncated_normal([8, 8, DeepQPongPlayer.STATE_FRAMES, 32], stddev=0.01))
convolution_bias_1 = tf.Variable(tf.constant(0.01, shape=[32]))
convolution_weights_2 = tf.Variable(tf.truncated_normal([4, 4, 32, 64], stddev=0.01))
convolution_bias_2 = tf.Variable(tf.constant(0.01, shape=[64]))
convolution_weights_3 = tf.Variable(tf.truncated_normal([3, 3, 64, 64], stddev=0.01))
convolution_bias_3 = tf.Variable(tf.constant(0.01, shape=[64]))
feed_forward_weights_1 = tf.Variable(tf.truncated_normal([256, 256], stddev=0.01))
feed_forward_bias_1 = tf.Variable(tf.constant(0.01, shape=[256]))
feed_forward_weights_2 = tf.Variable(tf.truncated_normal([256, DeepQPongPlayer.ACTIONS_COUNT], stddev=0.01))
feed_forward_bias_2 = tf.Variable(tf.constant(0.01, shape=[DeepQPongPlayer.ACTIONS_COUNT]))
input_layer = tf.placeholder("float", [None, DeepQPongPlayer.RESIZED_SCREEN_X, DeepQPongPlayer.RESIZED_SCREEN_Y,
DeepQPongPlayer.STATE_FRAMES])
hidden_convolutional_layer_1 = tf.nn.relu(
tf.nn.conv2d(input_layer, convolution_weights_1, strides=[1, 4, 4, 1], padding="SAME") + convolution_bias_1)
hidden_max_pooling_layer_1 = tf.nn.max_pool(hidden_convolutional_layer_1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding="SAME")
hidden_convolutional_layer_2 = tf.nn.relu(
tf.nn.conv2d(hidden_max_pooling_layer_1, convolution_weights_2, strides=[1, 2, 2, 1],
padding="SAME") + convolution_bias_2)
hidden_max_pooling_layer_2 = tf.nn.max_pool(hidden_convolutional_layer_2, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding="SAME")
hidden_convolutional_layer_3 = tf.nn.relu(
tf.nn.conv2d(hidden_max_pooling_layer_2, convolution_weights_3,
strides=[1, 1, 1, 1], padding="SAME") + convolution_bias_3)
hidden_max_pooling_layer_3 = tf.nn.max_pool(hidden_convolutional_layer_3, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding="SAME")
hidden_convolutional_layer_3_flat = tf.reshape(hidden_max_pooling_layer_3, [-1, 256])
final_hidden_activations = tf.nn.relu(
tf.matmul(hidden_convolutional_layer_3_flat, feed_forward_weights_1) + feed_forward_bias_1)
output_layer = tf.matmul(final_hidden_activations, feed_forward_weights_2) + feed_forward_bias_2
return input_layer, output_layer