-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcritic.py
322 lines (280 loc) · 13.1 KB
/
critic.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# ===========================
# Critic DNN
# ===========================
import tensorflow as tf
import numpy as np
# Network Parameters - Hidden layers
n_hidden_1 = 100
n_hidden_2 = 100
n_hidden_3 = 100
def reinforce(x, reward, n_hidden=500, scope=None):
with tf.variable_scope(scope or 'REINFORCE_layer'):
# important: stop the gradients
x = tf.stop_gradient(x)
reward = tf.stop_gradient(reward)
# baseline: central
# init = tf.constant(2.9428)
init = tf.constant(0.)
baseline_c = tf.get_variable('baseline_c', initializer=init)
# baseline: data dependent
baseline_x = (linear(
tf.sigmoid(
linear(
tf.sigmoid(linear(
x, n_hidden, True, scope='l1')),
n_hidden,
True,
scope='l2')),
1,
True,
scope='l3'))
reward = reward - baseline_c - baseline_x
# reward = reward - baseline_x
return reward
def linear(inputs,
output_size,
bias,
bias_start_zero=False,
matrix_start_zero=False,
scope=None):
"""Define a linear connection that can customise the parameters."""
shape = inputs.get_shape().as_list()
if len(shape) != 2:
raise ValueError('Linear is expecting 2D arguments: %s' % str(shape))
if not shape[1]:
raise ValueError('Linear expects shape[1] of arguments: %s' % str(shape))
input_size = shape[1]
# Now the computation.
with tf.variable_scope(scope or 'Linear'):
if matrix_start_zero:
matrix = tf.get_variable(
'Matrix', [input_size, output_size],
initializer=tf.constant_initializer(0))
else:
matrix = tf.get_variable('Matrix', [input_size, output_size])
res = tf.matmul(inputs, matrix)
if not bias:
return res
if bias_start_zero:
bias_term = tf.get_variable(
'Bias', [output_size], initializer=tf.constant_initializer(0))
else:
bias_term = tf.get_variable('Bias', [output_size])
return res + bias_term
def batch_norm(x, n_out, phase_train):
"""
Batch normalization on convolutional maps.
Ref.: http://stackoverflow.com/questions/33949786/how-could-i-use-batch-normalization-in-tensorflow
Args:
x: Tensor, 4D BHWD input maps
n_out: integer, depth of input maps
phase_train: boolean tf.Varialbe, true indicates training phase
scope: string, variable scope
Return:
normed: batch-normalized maps
"""
with tf.variable_scope('bn'):
beta = tf.Variable(tf.constant(0.0, shape=[n_out]),
name='beta', trainable=True)
gamma = tf.Variable(tf.constant(1.0, shape=[n_out]),
name='gamma', trainable=True)
batch_mean, batch_var = tf.nn.moments(x, [0], name='moments')
ema = tf.train.ExponentialMovingAverage(decay=0.5)
def mean_var_with_update():
ema_apply_op = ema.apply([batch_mean, batch_var])
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean), tf.identity(batch_var)
mean, var = tf.cond(phase_train,
mean_var_with_update,
lambda: (ema.average(batch_mean), ema.average(batch_var)))
normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-3)
return normed
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial, name="Weight")
def bias_variable(shape, val=0.03):
initial = tf.constant(val, shape=shape)
return tf.Variable(initial, name="Bias")
def weight_variable_uniform(shape, value):
initial = tf.random_uniform(shape, minval=-value, maxval=value, dtype=tf.float32)
return tf.Variable(initial, name="Weights")
def bias_variable_uniform(shape, value):
initial = tf.random_uniform(shape, minval=-value, maxval=value, dtype=tf.float32)
return tf.Variable(initial, name="Bias")
def conv1d(x, W, stride):
return tf.nn.conv1d(x, W, stride = stride, padding = "SAME")
class CriticNetwork(object):
"""
Input to the network is the state and action, output is Q(s,a).
The action must be obtained from the output of the Actor network.
"""
def __init__(self, sess, state_dim, action_dim, switch_dim, learning_rate, tau, num_actor_vars, \
baseline_rate=1., control_variance_flag=True):
self.sess = sess
self.s_dim = state_dim
self.a_dim = action_dim
self.switch_dim = switch_dim
self.learning_rate = learning_rate
self.tau = tau
# Create the critic network
with tf.name_scope("OnlineNet"):
self.inputs, self.action, self.out, self.logits, self.switch_a = self.create_critic_network()
self.network_params = tf.trainable_variables()[num_actor_vars:]
# Target Network
with tf.name_scope("TargetNet"):
self.target_inputs, self.target_action, self.target_out, self.target_logits, self.target_switch_a = \
self.create_critic_network()
self.target_network_params = tf.trainable_variables()[(len(self.network_params) + num_actor_vars):]
# Op for periodically updating target network with online network weights with regularization
self.update_target_network_params = \
[self.target_network_params[i].assign(
tf.multiply(self.network_params[i], self.tau) + tf.multiply(self.target_network_params[i], 1. - self.tau))
for i in range(len(self.target_network_params))]
self.predicted_q_value = tf.placeholder(tf.float32, [None, 1])
self.ISWeights = tf.placeholder(tf.float32, [None, 1], name='IS_weights')
self.episode_r = tf.placeholder(tf.float32, shape=(), name='EpiR')
self.episode_switch = tf.placeholder(tf.int32, shape=[None], name='EpiSwitch')
# Define switch loss
sample_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=self.episode_switch)
if control_variance_flag:
# with control variance
reward_selection = tf.squeeze(reinforce(self.inputs, self.episode_r))
self.switch_loss = tf.multiply(sample_loss, tf.stop_gradient(reward_selection))
self.baseline_loss = tf.square(reward_selection)
loss = self.switch_loss+self.baseline_loss*baseline_rate
self.switch_optimize = tf.train.AdamOptimizer(self.learning_rate).minimize(loss)
else:
# without control variance
self.switch_loss = tf.multiply(sample_loss, self.episode_r)
self.switch_optimize = tf.train.AdamOptimizer(self.learning_rate).minimize(self.switch_loss)
# Define loss and optimization Op
self.abs_errors = tf.reduce_sum(tf.abs(self.predicted_q_value - self.out), axis=1)
self.loss = tf.reduce_mean(self.ISWeights * tf.squared_difference(self.predicted_q_value, self.out))
self.critic_optimize = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
# Get the gradient of the net w.r.t. the action
# using Q value
self.action_grads = tf.gradients(self.out, self.action)
def create_critic_network(self):
inputs = tf.placeholder(tf.float32, [None, self.s_dim])
action = tf.placeholder(tf.float32, [None, self.a_dim])
# # FC1
# with tf.name_scope("FC1"):
# w_fc1 = weight_variable([self.s_dim, n_hidden_1])
# b_fc1 = bias_variable([n_hidden_1])
# # FC2
# with tf.name_scope("FC2"):
# w_fc2 = weight_variable([n_hidden_1 + self.a_dim, n_hidden_2])
# b_fc2 = bias_variable([n_hidden_2])
# # FC2 adv
# with tf.name_scope("FC_adv"):
# w_fc2_adv = weight_variable([n_hidden_1, n_hidden_2/2])
# b_fc2_adv = bias_variable([n_hidden_2/2])
# # FC2 critic
# with tf.name_scope("FC_critic"):
# w_fc2_value = weight_variable([n_hidden_1, n_hidden_2/2])
# b_fc2_value = bias_variable([n_hidden_2/2])
# # Out
# with tf.name_scope("Out"):
# w_out = weight_variable([n_hidden_2, 1])
# b_out = bias_variable([1])
# # Out_adv
# with tf.name_scope("Out_adv"):
# w_out_adv = weight_variable([n_hidden_2/2, self.switch_dim])
# b_out_adv = bias_variable([self.switch_dim])
# # Out_value
# with tf.name_scope("Out_value"):
# w_out_value = weight_variable([n_hidden_2/2, 1])
# b_out_value = bias_variable([1])
# FC1
with tf.name_scope("FC1"):
w_fc1 = weight_variable_uniform([self.s_dim, n_hidden_1], tf.sqrt(1.0/(self.s_dim)))
b_fc1 = bias_variable_uniform([n_hidden_1], tf.sqrt(1.0/(self.s_dim)))
# FC2
with tf.name_scope("FC2"):
w_fc2 = weight_variable_uniform([n_hidden_1+self.a_dim, n_hidden_2], tf.sqrt(1.0/(n_hidden_1+self.a_dim)))
b_fc2 = bias_variable_uniform([n_hidden_2], tf.sqrt(1.0/(n_hidden_1+self.a_dim)))
# FC2 adv
with tf.name_scope("FC_adv"):
w_fc2_adv = weight_variable_uniform([n_hidden_1, n_hidden_2/2], tf.sqrt(1.0/(n_hidden_1)))
b_fc2_adv = bias_variable_uniform([n_hidden_2/2], tf.sqrt(1.0/(n_hidden_1)))
# FC2 critic
with tf.name_scope("FC_critic"):
w_fc2_value = weight_variable_uniform([n_hidden_1, n_hidden_2/2], tf.sqrt(1.0/(n_hidden_1)))
b_fc2_value = bias_variable_uniform([n_hidden_2/2], tf.sqrt(1.0/(n_hidden_1)))
# Out
with tf.name_scope("Out"):
w_out = weight_variable_uniform([n_hidden_2, 1], 3e-3)
b_out = bias_variable_uniform([1], 3e-3)
# Out_adv
with tf.name_scope("Out_adv"):
# w_out_adv = weight_variable_uniform([n_hidden_2/2, self.switch_dim-1], 3e-3)
# b_out_adv = bias_variable_uniform([self.switch_dim-1], 3e-3)
w_out_adv = weight_variable_uniform([n_hidden_2/2, self.switch_dim-1], 3e-3)
b_out_adv = bias_variable_uniform([self.switch_dim-1], 3e-3)
# Out_adv
with tf.name_scope("Out_value"):
w_out_value = weight_variable_uniform([n_hidden_2/2, 1], 3e-3)
b_out_value = bias_variable_uniform([1], 3e-3)
# critic
h_fc1 = tf.nn.relu(tf.matmul(inputs, w_fc1) + b_fc1)
h_fc1_a = tf.concat([h_fc1, action], axis=1)
h_fc2 = tf.nn.relu(tf.matmul(h_fc1_a, w_fc2) + b_fc2)
out = tf.matmul(h_fc2, w_out) + b_out
# switch
h_fc2_adv = tf.nn.relu(tf.matmul(h_fc1, w_fc2_adv) + b_fc2_adv)
h_fc2_value = tf.nn.relu(tf.matmul(h_fc1, w_fc2_value) + b_fc2_value)
out_adv = tf.matmul(h_fc2_adv, w_out_adv) + b_out_adv
out_value = tf.matmul(h_fc2_value, w_out_value) + b_out_value
advAvg = tf.expand_dims(tf.reduce_mean(out_adv, axis=1), axis=1)
advIdentifiable = tf.subtract(out_adv, advAvg)
unscaled_logits = tf.add(out_value, advIdentifiable)
probs = tf.sigmoid(unscaled_logits)
[prob1, prob2] = tf.unstack(probs, axis=1)
logit1 = tf.log(prob1)
logit2 = tf.log((1-prob1)*prob2)
logit3 = tf.log((1-prob1)*(1-prob2))
logits = tf.stack([logit1, logit2, logit3], axis=1)
# [prob1, temp_prob2, temp_prob3] = tf.unstack(probs, axis=1)
# prob2 = temp_prob2/(temp_prob2+temp_prob3)
# prob3 = temp_prob3/(temp_prob2+temp_prob3)
# logit1 = tf.log(prob1)
# logit2 = tf.log((1-prob1)*prob2)
# logit3 = tf.log((1-prob1)*prob3)
# logits = tf.stack([logit1, logit2, logit3], axis=1)
switch_a = tf.multinomial(logits, 1)
switch_a = tf.reshape(switch_a, [-1])
return inputs, action, out, logits, switch_a
def train(self, inputs, action, predicted_q_value, ISWeights):
return self.sess.run([self.out, self.abs_errors, self.critic_optimize], feed_dict={
self.inputs: inputs,
self.action: action,
self.predicted_q_value: predicted_q_value,
self.ISWeights: ISWeights
})
def switch_train(self, ep_state, ep_switch, ep_r):
return self.sess.run(self.switch_optimize, feed_dict={
self.inputs: ep_state,
self.episode_switch: ep_switch,
self.episode_r: ep_r
})
def predict(self, inputs, action):
return self.sess.run(self.out, feed_dict={
self.inputs: inputs,
self.action: action
})
def predict_target(self, inputs, action):
return self.sess.run(self.target_out, feed_dict={
self.target_inputs: inputs,
self.target_action: action
})
def action_gradients(self, inputs, actions):
return self.sess.run(self.action_grads, feed_dict={
self.inputs: inputs,
self.action: actions
})
def update_target_network(self):
self.sess.run(self.update_target_network_params)
def predict_switch(self, inputs):
return self.sess.run([self.logits, self.switch_a], feed_dict={
self.inputs: inputs
})