-
Notifications
You must be signed in to change notification settings - Fork 10
/
iddpg.py
300 lines (247 loc) · 13.7 KB
/
iddpg.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
import tensorflow as tf
import numpy as np
import gym
from ou_noise import OUNoise
LAYER_1 = 400
LAYER_2 = 300
LAYER_3 = 300
keep_rate = 0.8
LAMBDA = 0.00001 # regularization term
GAMMA = 0.99
class IDDPG(object):
def __init__(self, sess, state_dim, action_dim, max_action, min_action, actor_learning_rate, critic_learning_rate, tau, RANDOM_SEED, device = '/cpu:0'):
self.sess = sess
np.random.seed(RANDOM_SEED)
tf.set_random_seed(RANDOM_SEED)
self.s_dim = state_dim
self.a_dim = action_dim
self.actor_learning_rate = actor_learning_rate
self.critic_learning_rate = critic_learning_rate
self.tau = tau
self.device = device
self.max_action = max_action
self.min_action = min_action
# Placeholders
self.inputs = tf.placeholder(tf.float32, shape=[None, self.s_dim], name='state')
self.action = tf.placeholder(tf.float32, shape=[None, self.a_dim], name='actions')
scope = 'net'
self.v, self.a, self.scaled_a, self.saver = self._build_net(scope)
self.a_params = tf.trainable_variables(scope=scope + '/actor')
self.c_params = tf.trainable_variables(scope=scope + '/critic')
#self.a_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/actor')
#self.c_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/critic')
scope = 'target'
self.v_target, self.a_target, self.scaled_a_target, self.saver_target = self._build_net(scope)
self.a_params_target = tf.trainable_variables(scope=scope + '/actor')
self.c_params_target = tf.trainable_variables(scope=scope + '/critic')
#self.a_params_target = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/actor')
#self.c_params_target = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/critic')
with tf.variable_scope('learning_rate'):
# global step
self.global_step = tf.Variable(0, trainable=False)
self.actor_decay_learning_rate = tf.train.exponential_decay(self.actor_learning_rate, self.global_step, 100000, 0.96, staircase=True)
self.critic_decay_learning_rate = tf.train.exponential_decay(self.critic_learning_rate, self.global_step, 100000, 0.96, staircase=True)
with tf.device(self.device):
# Op for periodically updating target network with online network
# weights with regularization
self.generate_param_updater()
self.predicted_q_value = tf.placeholder(tf.float32, [None, 1])
# Define loss and optimization Op
self.squared = tf.square(tf.subtract(self.predicted_q_value,self.v))
self.l2_loss = tf.losses.get_regularization_loss(scope="net/critic")
self.loss = tf.reduce_mean(self.squared) + self.l2_loss
self.critic_optimize = tf.train.AdamOptimizer(self.critic_decay_learning_rate).minimize(self.loss, global_step=self.global_step)
self.action_grads = tf.gradients(self.v, self.action)[0]
self.actor_gradients = tf.gradients(self.a, self.a_params, -self.action_grads)
self.actor_optimize = tf.train.AdamOptimizer(self.actor_decay_learning_rate).apply_gradients(zip(self.actor_gradients, self.a_params), global_step=self.global_step)
# inverting gradients
self.inverting_gradients_placeholder = tf.placeholder(tf.float32, shape=[None, self.a_dim], name='inverting_gradients')
self._dq_da = tf.gradients(self.v, self.action)[0] # q, a
self._grad = tf.gradients(self.a, self.a_params, -self.inverting_gradients_placeholder)
self._train_actor = tf.train.AdamOptimizer(self.actor_decay_learning_rate).apply_gradients(zip(self._grad, self.a_params),global_step=self.global_step)
def _build_net(self,scope):
with tf.device(self.device):
with tf.variable_scope(scope + '/critic'):
'''
net = tf.layers.dense(self.inputs, LAYER_1, tf.nn.relu, name='critic_L1')
initializer = tf.variance_scaling_initializer()
s_union_weights = tf.Variable(initializer.__call__([LAYER_1, LAYER_2]), name='critic_L2_Ws')
a_union_weights = tf.Variable(initializer.__call__([self.a_dim, LAYER_2]), name='critic_L2_Wa')
union_biases = tf.Variable(tf.zeros([LAYER_2]), name='critic_L2_b')
net = tf.nn.relu(tf.matmul(net, s_union_weights) + tf.matmul(self.action, a_union_weights) + union_biases,name='critic_L2')
w_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
v = tf.layers.dense(net, self.a_dim, kernel_initializer=w_init, name='critic_output')
'''
regularizer = tf.contrib.layers.l2_regularizer(scale=LAMBDA)
l1 = tf.contrib.layers.fully_connected(self.inputs, LAYER_1, weights_regularizer=regularizer, activation_fn=tf.nn.leaky_relu)
l2_a = tf.contrib.layers.fully_connected(self.action, LAYER_2, weights_regularizer=regularizer, activation_fn=None)
l2_s = tf.contrib.layers.fully_connected(l1, LAYER_2, weights_regularizer=regularizer,activation_fn=None)
l2 = tf.nn.leaky_relu(l2_s + l2_a)
v = tf.contrib.layers.fully_connected(l2, 1, weights_regularizer=regularizer, activation_fn=None)
with tf.variable_scope(scope + '/actor'):
l1 = tf.contrib.layers.fully_connected(self.inputs, LAYER_1, activation_fn=tf.nn.leaky_relu) # tf.nn.leaky_relu tf.nn.relu
l2 = tf.contrib.layers.fully_connected(l1, LAYER_2, activation_fn=tf.nn.leaky_relu)
w_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
a = tf.contrib.layers.fully_connected(l2, self.a_dim, weights_initializer=w_init, activation_fn=None) # None tf.nn.tanh
scaled_a = a
# scaled_a = tf.clip_by_value(a,self.min_action,self.max_action)#tf.multiply(a, self.action_bound)
saver = tf.train.Saver()
return v, a, scaled_a, saver
def train(self, s_batch, a_batch, r_batch, t_batch, s2_batch, MINIBATCH_SIZE):
# get q target
target_q = self.critic_predict_target(s2_batch, self.predict_action_target(s2_batch))
# obtain y
y_i = []
for k in range(MINIBATCH_SIZE):
if t_batch[k]:
y_i.append(r_batch[k])
else:
y_i.append(r_batch[k] + GAMMA * target_q[k])
# train critic
LOSS = self.critic_train(s_batch, a_batch, np.reshape(y_i, (MINIBATCH_SIZE, 1)))
# train critic
#ac_tor_grads = self._critic_train(s_batch, a_batch, np.reshape(y_i, (MINIBATCH_SIZE, 1)))
#print('a grads',ac_tor_grads)
actions = self.predict_action(s_batch)
upper = self.max_action
lower = self.min_action
# get dq/da array, action array
#print(upper, '***************')
dq_das = self.sess.run([self._dq_da], feed_dict={self.inputs: s_batch, self.action:actions})[0]
# inverting gradients, if dq_da >= 0, apply upper method, else lower method
inverting_gradients = []
#'''
# print('1 dq_das, actions',dq_das, actions)
'''
# print('dq_das, actions',dq_das, actions)
for dq_da, action in zip(dq_das, actions):
# print('dq_da, action',dq_da, action)
if dq_da >= 0.0:
inverting_gradients.append(dq_da * (self.max_action - action) / (self.max_action - self.min_action))
else:
inverting_gradients.append(dq_da * (action - self.min_action) / (self.max_action - self.min_action))
inverting_gradients = np.array(inverting_gradients).reshape(-1, 1)
'''
for i in range(MINIBATCH_SIZE):
#print('2', i,dq_das[i])
for j in range(self.a_dim):
if dq_das[i][j] >= 0.0:
dq_das[i][j] = dq_das[i][j] * (self.max_action - actions[i][j]) / (self.max_action - self.min_action)
else:
dq_das[i][j] = dq_das[i][j] * (actions[i][j] - self.min_action) / (self.max_action - self.min_action)
# print(dq_das,inverting_gradients)
# exit()
inverting_gradients = dq_das
# print('2 dq_das, actions',dq_das, actions)
#print('1','inverting_gradients',inverting_gradients)
# print('2','inverting_gradients',inverting_gradients,dq_das, actions)
# time.sleep(1)
# update actor
self.sess.run(self._train_actor, feed_dict={self.inputs: s_batch, self.inverting_gradients_placeholder: inverting_gradients})
self.update_target_network()
return
def _critic_train(self, inputs, action, predicted_q_value):
return self.sess.run([self.action_grads], feed_dict={
self.inputs: inputs,
self.action: action,
self.predicted_q_value: predicted_q_value
})
def update_target_network(self):
self.sess.run([self.a_updater,self.c_updater])
def generate_param_updater(self):
self.a_updater = [self.a_params_target[i].assign(tf.multiply(self.a_params[i], self.tau) + tf.multiply(self.a_params_target[i], 1. - self.tau))
for i in range(len(self.a_params))]
self.c_updater = [self.c_params_target[i].assign(tf.multiply(self.c_params[i], self.tau) + tf.multiply(self.c_params_target[i], 1. - self.tau))
for i in range(len(self.c_params))]
def critic_train(self, inputs, action, predicted_q_value):
return self.sess.run([self.loss,self.critic_optimize], feed_dict={
self.inputs: inputs,
self.action: action,
self.predicted_q_value: predicted_q_value
})
def actor_train(self,inputs, action):
return self.sess.run(self.actor_optimize, feed_dict={
self.inputs: inputs,
self.action: action
})
def save(self):
self.saver.save(self.sess,"./model/model.ckpt")
self.saver_target.save(self.sess,"./model/model_target.ckpt")
print("Model saved in file: actor_model")
def load(self):
self.saver.restore(self.sess,"./model/model.ckpt")
self.saver_target.restore(self.sess,"./model/model_target.ckpt")
def critic_predict_target(self, state, action):
return self.sess.run(self.v_target, feed_dict={
self.inputs: state,
self.action: action
})
def predict_action_target(self, state):
return self.sess.run(self.scaled_a_target, feed_dict={
self.inputs: state
})
def predict_action(self, state):
return self.sess.run(self.scaled_a, feed_dict={
self.inputs: state
})
if __name__ == '__main__':
from replay_buffer import ReplayBuffer
ACTOR_LEARNING_RATE = 0.0001
CRITIC_LEARNING_RATE = 0.001
# Soft target update param
TAU = 0.001
DEVICE = '/cpu:0'
# ENV_NAME = 'MountainCarContinuous-v0'
ENV_NAME = 'Pendulum-v0'
# import gym_foo
# ENV_NAME = 'nessie_end_to_end-v0'
max_action = 2.
min_action = -2.
epochs = 500
epsilon = 1.0
min_epsilon = 0.1
EXPLORE = 200
BUFFER_SIZE = 100000
RANDOM_SEED = 51234
MINIBATCH_SIZE = 64# 32 # 5
with tf.Session() as sess:
np.random.seed(RANDOM_SEED)
tf.set_random_seed(RANDOM_SEED)
env = gym.make(ENV_NAME)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
low = DDPG(sess, state_dim, action_dim, max_action, min_action, ACTOR_LEARNING_RATE, CRITIC_LEARNING_RATE, TAU, RANDOM_SEED,device=DEVICE)
sess.run(tf.global_variables_initializer())
# check that we are effectively updating the parameters
#print(low.a_params_target[0].eval()[0][0],low.a_params[0].eval()[0][0])
#low.update_target_network()
#print(low.a_params_target[0].eval()[0][0],low.a_params[0].eval()[0][0])
replay_buffer = ReplayBuffer(BUFFER_SIZE, RANDOM_SEED)
ruido = OUNoise(action_dim, mu = 0.0)
for i in range(epochs):
state = env.reset()
done = False
epsilon -= (epsilon/EXPLORE)
epsilon = np.maximum(min_epsilon,epsilon)
episode_r = 0.
step = 0
while (not done):
step += 1
action = low.predict_action(np.reshape(state,(1,state_dim)))
action1 = action
action = np.clip(action,min_action,max_action)
action = action + max(epsilon,0)*ruido.noise()
action = np.clip(action,min_action,max_action)
# print(action1, action)
next_state, reward, done, info = env.step(action)
reward = reward + 1.
# reward = np.clip(reward,-1.,1.)
replay_buffer.add(np.reshape(state, (state_dim,)), np.reshape(action, (action_dim,)), reward,
done, np.reshape(next_state, (state_dim,)))
state = next_state
episode_r = episode_r + reward
if replay_buffer.size() > MINIBATCH_SIZE:
s_batch, a_batch, r_batch, t_batch, s2_batch = replay_buffer.sample_batch(MINIBATCH_SIZE)
low.train(s_batch, a_batch, r_batch, t_batch, s2_batch,MINIBATCH_SIZE)
print(i, step, 'last r', round(reward,3), 'episode reward',round(episode_r,3), 'epsilon', round(epsilon,3))
low.save()