-
Notifications
You must be signed in to change notification settings - Fork 8
/
TD3_per_class.py
392 lines (333 loc) · 15.7 KB
/
TD3_per_class.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""
1. 换成了per_buffer的类,在td3的类中,变动内容有:
1.增加计算abs_error的句柄:
self.abs_errors = tf.abs(min_q_targ - self.q1)
2.更新q值的时候,增加重要性采样的系数
self.q_loss = self.ISWeights * (q1_loss + q2_loss)
3.修改存储transition,因为per_buffer里面,存的格式是列表,不需要拆解transition。
self.replay_buffer.store(transition)
4.修改更新参数的函数,对buffer的采样,数据格式也不一样:
tree_idx, batch_memory, ISWeights = self.replay_buffer.sample(batch_size=batch_size)
但这个其实改的可以很少。
2. 调用TD3_per和普通TD3没有任何区别。
3. 做了测试对比,在HalfCheetah-v2任务中,TD3_per每个回合更新时间是26秒,TD3的只要4秒,实在是浪费时间。这里面也可能是因为我的cpu资源被其他进程占满了。性能这块的话,其实影响不大。
"""
import numpy as np
import tensorflow as tf
import gym
import time
from td3_sp import core
from td3_sp.core import get_vars, mlp_actor_critic
class ReplayBuffer:
"""
A simple FIFO experience replay buffer for TD3 agents.
"""
def __init__(self,
obs_dim,
act_dim,
size):
self.obs1_buf = np.zeros([size, obs_dim], dtype=np.float32)
self.obs2_buf = np.zeros([size, obs_dim], dtype=np.float32)
self.acts_buf = np.zeros([size, act_dim], dtype=np.float32)
self.rews_buf = np.zeros(size, dtype=np.float32)
self.done_buf = np.zeros(size, dtype=np.float32)
self.ptr, self.size, self.max_size = 0, 0, size
def store(self, obs, act, rew, next_obs, done):
self.obs1_buf[self.ptr] = obs
self.obs2_buf[self.ptr] = next_obs
self.acts_buf[self.ptr] = act
self.rews_buf[self.ptr] = rew
self.done_buf[self.ptr] = done
self.ptr = (self.ptr + 1) % self.max_size
self.size = min(self.size + 1, self.max_size)
def sample_batch(self, batch_size=32):
idxs = np.random.randint(0, self.size, size=batch_size)
return dict(obs1=self.obs1_buf[idxs],
obs2=self.obs2_buf[idxs],
acts=self.acts_buf[idxs],
rews=self.rews_buf[idxs],
done=self.done_buf[idxs])
class TD3:
def __init__(self,
a_dim, obs_dim, a_bound,
mlp_actor_critic=core.mlp_actor_critic,
ac_kwargs=dict(), seed=0,
replay_size=int(1e6), gamma=0.9,
polyak=0.99, pi_lr=1e-3, q_lr=1e-3,
batch_size=100,
# start_steps=10000,
act_noise=0.1, target_noise=0.2,
noise_clip=0.5, policy_delay=2,
sess_opt=None,
# max_ep_len=1000,
# logger_kwargs=dict(), save_freq=1
sess = None,
buffer=None,
per_flag=True,
):
# self.sess = sess
self.learn_step = 0
self.obs_dim = obs_dim
self.act_dim = a_dim
self.act_limit = a_bound
self.policy_delay = policy_delay
self.action_noise = act_noise
# Share information about action space with policy architecture
ac_kwargs['action_space'] = a_bound
# Inputs to computation graph
self.ISWeights = tf.placeholder(tf.float32, [None, 1], name='IS_weights')
self.x_ph, self.a_ph, self.x2_ph, self.r_ph, self.d_ph = core.placeholders(obs_dim, a_dim, obs_dim, None, None)
self.actor_lr = tf.placeholder(tf.float32, shape=[], name='actor_lr')
self.critic_lr = tf.placeholder(tf.float32, shape=[], name='critic_lr')
# Main outputs from computation graph
with tf.variable_scope('main'):
self.pi, self.q1, self.q2, self.q1_pi = mlp_actor_critic(self.x_ph, self.a_ph, **ac_kwargs)
# Target policy network
with tf.variable_scope('target'):
pi_targ, _, _, _ = mlp_actor_critic(self.x2_ph, self.a_ph, **ac_kwargs)
# Target Q networks
with tf.variable_scope('target', reuse=True):
# Target policy smoothing, by adding clipped noise to target actions
epsilon = tf.random_normal(tf.shape(pi_targ), stddev=target_noise)
epsilon = tf.clip_by_value(epsilon, -noise_clip, noise_clip)
a2 = pi_targ + epsilon
a2 = tf.clip_by_value(a2, -self.act_limit, self.act_limit)
# Target Q-values, using action from target policy
_, q1_targ, q2_targ, _ = mlp_actor_critic(self.x2_ph, a2, **ac_kwargs)
self.per_flag = per_flag
# Experience buffer
if buffer:
self.replay_buffer = buffer
else:
if self.per_flag:
from memory.sp_per_memory import ReplayBuffer
self.replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=self.act_dim, size=replay_size)
# Count variables
var_counts = tuple(core.count_vars(scope)
for scope in ['main/pi',
'main/q1',
'main/q2',
'main'])
print('\nNumber of parameters: \t pi: %d, \t q1: %d, \t q2: %d, \t total: %d\n' % var_counts)
# Bellman backup for Q functions, using Clipped Double-Q targets
min_q_targ = tf.minimum(q1_targ, q2_targ)
# backup = tf.stop_gradient(self.r_ph + gamma * (1 - self.d_ph) * min_q_targ)
backup = self.r_ph + gamma * min_q_targ
# TD3 losses
self.pi_loss = -tf.reduce_mean(self.q1_pi)
q1_loss = tf.reduce_mean((self.q1 - backup) ** 2)
q2_loss = tf.reduce_mean((self.q2 - backup) ** 2)
if self.per_flag:
# 也许可以选q2,但是一般来说q1和q2值相差不大。
self.abs_errors = tf.abs(backup - self.q1)
# 是不是这么乘的,我也迷惑,等下测试一下,看看效果有没有提升。
self.q_loss = self.ISWeights * (q1_loss + q2_loss)
else:
# 正常的!
self.q_loss = q1_loss + q2_loss
# Separate train ops for pi, q
pi_optimizer = tf.train.AdamOptimizer(learning_rate=self.actor_lr)
q_optimizer = tf.train.AdamOptimizer(learning_rate=self.critic_lr)
self.train_pi_op = pi_optimizer.minimize(self.pi_loss,
var_list=get_vars('main/pi'))
# 这里的参数,怎么是总的q?
# 难道这里的字符串只需要匹配就好了?
self.train_q_op = q_optimizer.minimize(self.q_loss,
var_list=get_vars('main/q'))
# Polyak averaging for target variables
self.target_update = tf.group([tf.assign(v_targ, polyak * v_targ + (1 - polyak) * v_main)
for v_main, v_targ in zip(get_vars('main'), get_vars('target'))])
# Initializing targets to match main variables
target_init = tf.group([tf.assign(v_targ, v_main)
for v_main, v_targ in zip(get_vars('main'), get_vars('target'))])
if sess_opt:
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=sess_opt)
self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
else:
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
self.sess.run(target_init)
def get_action(self, s, noise_scale=0):
a = self.sess.run(self.pi,
feed_dict={self.x_ph: s.reshape(1, -1),
})[0]
a += noise_scale * np.random.randn(self.act_dim)
return np.clip(a, -self.act_limit, self.act_limit)
def get_q(self, s, a):
Q = self.sess.run([self.q1, self.q2],
feed_dict={self.x_ph: s.reshape(1, -1),
self.a_ph: a.reshape(1, -1),
})
q1, q2 = Q[0][0], Q[1][0]
return q1, q2
def store_transition(self, transition):
if self.per_flag:
self.replay_buffer.store(transition)
else:
(s, a, r, s_, done) = transition
self.replay_buffer.store(s, a, r, s_, done)
def test_agent(self, env, max_ep_len=1000, n=5):
ep_reward_list = []
for j in range(n):
s = env.reset()
ep_reward = 0
for i in range(max_ep_len):
# Take deterministic actions at test time (noise_scale=0)
s, r, d, _ = env.step(self.get_action(s))
ep_reward += r
ep_reward_list.append(ep_reward)
mean_ep_reward = np.mean(np.array(ep_reward_list))
return mean_ep_reward
def learn(self, batch_size=100,
actor_lr_input=0.001,
critic_lr_input=0.001,
):
if self.per_flag:
tree_idx, batch_memory, ISWeights = self.replay_buffer.sample(batch_size=batch_size)
batch_states, batch_actions, batch_rewards, batch_states_, batch_dones = [], [], [], [], []
for i in range(batch_size):
batch_states.append(batch_memory[i][0])
batch_actions.append(batch_memory[i][1])
batch_rewards.append(batch_memory[i][2])
batch_states_.append(batch_memory[i][3])
batch_dones.append(batch_memory[i][4])
feed_dict = {self.x_ph: np.array(batch_states),
self.x2_ph: np.array(batch_states_),
self.a_ph: np.array(batch_actions),
self.r_ph: np.array(batch_rewards),
self.d_ph: np.array(batch_dones),
self.actor_lr: actor_lr_input,
self.critic_lr: critic_lr_input,
self.ISWeights: ISWeights
}
q_step_ops = [self.q_loss, self.q1,
self.q2, self.train_q_op,
self.abs_errors,
]
outs = self.sess.run(q_step_ops, feed_dict)
q_loss, q1, q2, train_q_op, abs_errors = outs
if self.learn_step % self.policy_delay == 0:
# Delayed policy update
outs = self.sess.run([self.pi_loss,
self.train_pi_op,
self.target_update],
feed_dict)
self.replay_buffer.batch_update(tree_idx,
abs_errors) # update priority
self.learn_step += 1
return outs
else:
batch = self.replay_buffer.sample_batch(batch_size)
feed_dict = {self.x_ph: batch['obs1'],
self.x2_ph: batch['obs2'],
self.a_ph: batch['acts'],
self.r_ph: batch['rews'],
self.d_ph: batch['done'],
self.actor_lr: actor_lr_input,
self.critic_lr: critic_lr_input,
}
q_step_ops = [self.q_loss, self.q1, self.q2, self.train_q_op]
outs = self.sess.run(q_step_ops, feed_dict)
if self.learn_step % self.policy_delay == 0:
# Delayed policy update
outs = self.sess.run([self.pi_loss, self.train_pi_op, self.target_update],
feed_dict)
self.learn_step += 1
return outs
def load_step_network(self, saver, load_path):
checkpoint = tf.train.get_checkpoint_state(load_path)
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(self.sess, tf.train.latest_checkpoint(load_path))
print("Successfully loaded:", checkpoint.model_checkpoint_path)
self.learn_step = int(checkpoint.model_checkpoint_path.split('-')[-1])
else:
print("Could not find old network weights")
def save_step_network(self, time_step, saver, save_path):
saver.save(self.sess, save_path + 'network', global_step=time_step,
write_meta_graph=False)
def load_simple_network(self, path):
saver = tf.train.Saver()
saver.restore(self.sess, tf.train.latest_checkpoint(path))
print("restore model successful")
def save_simple_network(self, save_path):
saver = tf.train.Saver()
saver.save(self.sess, save_path=save_path + "/params", write_meta_graph=False)
if __name__ == '__main__':
import argparse
random_seed = int(time.time() * 1000 % 1000)
parser = argparse.ArgumentParser()
parser.add_argument('--env', type=str, default='HalfCheetah-v2')
parser.add_argument('--hid', type=int, default=300)
parser.add_argument('--l', type=int, default=1)
parser.add_argument('--gamma', type=float, default=0.99)
parser.add_argument('--seed', '-s', type=int, default=random_seed)
parser.add_argument('--epochs', type=int, default=3000)
parser.add_argument('--max_steps', type=int, default=1000)
parser.add_argument('--exp_name', type=str, default='td3_class')
args = parser.parse_args()
env = gym.make(args.env)
env = env.unwrapped
env.seed(args.seed)
s_dim = env.observation_space.shape[0]
a_dim = env.action_space.shape[0]
a_bound = env.action_space.high[0]
net = TD3(a_dim, s_dim, a_bound,
batch_size=100,
sess_opt=0.1,
)
ep_reward_list = []
test_ep_reward_list = []
for i in range(args.epochs):
s = env.reset()
ep_reward = 0
st = time.time()
for j in range(args.max_steps):
# Add exploration noise
if i < 10:
a = np.random.rand(a_dim) * a_bound
else:
# a = net.choose_action(s)
a = net.get_action(s, 0.1)
# a = noise.add_noise(a)
a = np.clip(a, -a_bound, a_bound)
s_, r, done, info = env.step(a)
done = False if j == args.max_steps - 1 else done
net.store_transition((s, a, r, s_, done))
s = s_
ep_reward += r
if j == args.max_steps - 1:
up_st = time.time()
for _ in range(args.max_steps):
net.learn()
ep_update_time = time.time() - up_st
ep_reward_list.append(ep_reward)
print('Episode:', i, ' Reward: %i' % int(ep_reward),
# 'Explore: %.2f' % var,
"learn step:", net.learn_step,
"ep_time:", np.round(time.time()-st, 3),
"up_time:", np.round(ep_update_time, 3),
)
# if ep_reward > -300:RENDER = True
# 增加测试部分!
if i % 20 == 0:
test_ep_reward = net.test_agent(env=env, n=5)
test_ep_reward_list.append(test_ep_reward)
print("-" * 20)
print('Episode:', i, ' Reward: %i' % int(ep_reward),
'Test Reward: %i' % int(test_ep_reward),
)
print("-" * 20)
break
import matplotlib.pyplot as plt
plt.plot(ep_reward_list)
img_name = str(args.exp_name + "_" + args.env + "_epochs" +
str(args.epochs) +
"_seed" + str(args.seed))
plt.title(img_name + "_train")
plt.savefig(img_name + ".png")
plt.show()
plt.close()
plt.plot(test_ep_reward_list)
plt.title(img_name + "_test")
plt.savefig(img_name + ".png")
plt.show()