-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
199 lines (164 loc) · 7.96 KB
/
model.py
File metadata and controls
199 lines (164 loc) · 7.96 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
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
import tensorflow as tf
import gym
class CNN:
'''
CNN
conv4
'''
def __init__(self, env):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=1e-3)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(1e-3, shape=shape)
return tf.Variable(initial)
def conv2d(input_tensor, W):
return tf.nn.conv2d(input_tensor, W, strides=[1, 1, 1, 1], padding='SAME')
def use_relu(conv, conv_biases):
return tf.nn.relu(tf.nn.bias_add(conv, conv_biases))
self.input_length = 8 * 8 * 3
self.input_s = tf.placeholder(shape=[1, self.input_length], dtype=tf.float32)
self.input_1 = tf.reshape(self.input_s, shape=[1, 8, 8, 3])
# layer1-conv64
self.W_conv_1 = weight_variable([3, 3, 3, 64])
self.b_1 = bias_variable([64])
self.conv_1 = conv2d(self.input_1, self.W_conv_1)
self.out_1 = use_relu(self.conv_1, self.b_1)
# layer2-conv64
self.W_conv_2 = weight_variable([3, 3, 64, 64])
self.b_2 = bias_variable([64])
self.conv_2 = conv2d(self.out_1, self.W_conv_2)
self.out_2 = use_relu(self.conv_2, self.b_2)
# layer3-conv128
self.W_conv_3 = weight_variable([3, 3, 64, 128])
self.b_3 = bias_variable([128])
self.conv_3 = conv2d(self.out_2, self.W_conv_3)
self.out_3 = use_relu(self.conv_3, self.b_3)
# layer4-conv128
self.W_conv_4 = weight_variable([3, 3, 128, 128])
self.b_4 = bias_variable([128])
self.conv_4 = conv2d(self.out_3, self.W_conv_4)
self.out_4 = use_relu(self.conv_4, self.b_4)
# layer5-fc128
self.out_4_flat = tf.reshape(self.out_4, [-1, 8 * 8 * 128])
self.W_5 = weight_variable([8 * 8 * 128, 128])
self.b_5 = bias_variable([128])
self.out_5 = tf.nn.relu(tf.matmul(self.out_4_flat, self.W_5) + self.b_5)
# layer6-fc60
self.W_6 = weight_variable([128, env.action_space.n])
self.b_6 = bias_variable([env.action_space.n])
self.Q = tf.nn.relu(tf.matmul(self.out_5, self.W_6) + self.b_6)
self.Q_target = tf.placeholder(shape=[1, env.action_space.n], dtype=tf.float32)
self.loss = tf.reduce_sum(tf.square(self.Q_target - self.Q))
self.update = tf.train.GradientDescentOptimizer(1e-2).minimize(self.loss)
self.init = tf.global_variables_initializer()
class Freezing_CNN:
def __init__(self, env):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=1e-2)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(1e-2, shape=shape)
return tf.Variable(initial)
def conv2d(input_tensor, W):
return tf.nn.conv2d(input_tensor, W, strides=[1, 1, 1, 1], padding='SAME')
def use_relu(conv, conv_biases):
return tf.nn.relu(tf.nn.bias_add(conv, conv_biases))
self.input_length = 8 * 8 * 3
self.input_s = tf.placeholder(shape=[1, self.input_length], dtype=tf.float32)
self.input_1 = tf.reshape(self.input_s, shape=[1, 8, 8, 3])
with tf.variable_scope('main_model'):
# layer1-conv64
W_conv_1 = weight_variable([3, 3, 3, 64])
b_1 = bias_variable([64])
conv_1 = conv2d(self.input_1, W_conv_1)
out_1 = use_relu(conv_1, b_1)
# layer2-conv64
W_conv_2 = weight_variable([3, 3, 64, 64])
b_2 = bias_variable([64])
conv_2 = conv2d(out_1, W_conv_2)
out_2 = use_relu(conv_2, b_2)
# layer3-conv128
W_conv_3 = weight_variable([3, 3, 64, 128])
b_3 = bias_variable([128])
conv_3 = conv2d(out_2, W_conv_3)
out_3 = use_relu(conv_3, b_3)
# layer4-conv128
W_conv_4 = weight_variable([3, 3, 128, 128])
b_4 = bias_variable([128])
conv_4 = conv2d(out_3, W_conv_4)
out_4 = use_relu(conv_4, b_4)
# layer5-fc128
out_4_flat = tf.reshape(out_4, [-1, 8 * 8 * 128])
W_5 = weight_variable([8 * 8 * 128, 128])
b_5 = bias_variable([128])
out_5 = tf.nn.relu(tf.matmul(out_4_flat, W_5) + b_5)
# layer6-fc60
W_6 = weight_variable([128, env.action_space.n])
b_6 = bias_variable([env.action_space.n])
self.Q = tf.nn.relu(tf.matmul(out_5, W_6) + b_6)
self.Q_target = tf.placeholder(shape=[1, env.action_space.n], dtype=tf.float32)
self.loss = tf.reduce_sum(tf.square(self.Q_target - self.Q))
self.update = tf.train.GradientDescentOptimizer(1e-3).minimize(self.loss)
self.init = tf.global_variables_initializer()
with tf.variable_scope('freezing_model'):
# layer1-conv64
W_conv_1 = weight_variable([3, 3, 3, 64])
b_1 = bias_variable([64])
conv_1 = conv2d(self.input_1, W_conv_1)
out_1 = use_relu(conv_1, b_1)
# layer2-conv64
W_conv_2 = weight_variable([3, 3, 64, 64])
b_2 = bias_variable([64])
conv_2 = conv2d(out_1, W_conv_2)
out_2 = use_relu(conv_2, b_2)
# layer3-conv128
W_conv_3 = weight_variable([3, 3, 64, 128])
b_3 = bias_variable([128])
conv_3 = conv2d(out_2, W_conv_3)
out_3 = use_relu(conv_3, b_3)
# layer4-conv128
W_conv_4 = weight_variable([3, 3, 128, 128])
b_4 = bias_variable([128])
conv_4 = conv2d(out_3, W_conv_4)
out_4 = use_relu(conv_4, b_4)
# layer5-fc128
out_4_flat = tf.reshape(out_4, [-1, 8 * 8 * 128])
W_5 = weight_variable([8 * 8 * 128, 128])
b_5 = bias_variable([128])
out_5 = tf.nn.relu(tf.matmul(out_4_flat, W_5) + b_5)
# layer6-fc60
W_6 = weight_variable([128, env.action_space.n])
b_6 = bias_variable([env.action_space.n])
self.freezing_Q = tf.nn.relu(tf.matmul(out_5, W_6) + b_6)
t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='freezing_model')
e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='main_model')
self.replace_model_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]
class Simple_model:
'''
a simple neural network
use flatten input
'''
def __init__(self, env):
'''
self.input_length = env.board_size ** 2 * 3
self.input_s = tf.placeholder(shape=[1, self.input_length], dtype=tf.float32)
self.W = tf.Variable(tf.random_uniform(shape=[self.input_length, env.action_space.n], minval=-1e-4, maxval=1e-4), name = 'w')
# self.Q = tf.matmul(self.input_s, self.W)
self.b1 = tf.Variable(tf.zeros([1, env.action_space.n]) + 1e-4)
self.Q = tf.nn.relu(tf.matmul(self.input_s, self.W) + self.b1)
'''
self.input_length = env.board_size ** 2 * 3
self.input_s = tf.placeholder(shape=[1, self.input_length], dtype=tf.float32)
self.W1 = tf.Variable(tf.random_uniform(shape=[self.input_length, 10], minval=-1e-4, maxval=1e-4))
# self.Q = tf.matmul(self.input_s, self.W)
self.b1 = tf.Variable(tf.zeros([1, 10]) + 1e-4)
self.out_1 = tf.nn.relu(tf.matmul(self.input_s, self.W1) + self.b1)
self.W2 = tf.Variable(tf.random_uniform(shape=[10, env.action_space.n], minval=-1e-4, maxval=1e-4))
self.b2 = tf.Variable(tf.zeros([1, env.action_space.n]) + 1e-4)
self.Q = tf.nn.relu(tf.matmul(self.out_1, self.W2) + self.b2)
# self.predict_action = tf.argmax(self.Q, 1)
self.Q_target = tf.placeholder(shape=[1, env.action_space.n], dtype=tf.float32)
self.loss = tf.reduce_sum(tf.square(self.Q_target - self.Q))
self.update = tf.train.GradientDescentOptimizer(1e-3).minimize(self.loss)
self.init = tf.global_variables_initializer()