-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpg.py
274 lines (235 loc) · 8.46 KB
/
ddpg.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
# Use parallel env as a quicker way to sample exprs
# Not as a way to solve sample inefficiency problem
# as in many on-policy algorithms
from collections import deque, defaultdict
import time
import numpy as np
import gym
import os
import torch
import torch.optim as optim
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_
import rlkits.utils.logger as logger
from rlkits.policies import DeterministicPolicy
from rlkits.policies import QNetForContinuousAction
from rlkits.memory import Memory
from rlkits.evaluate import evaluate_policy
from rlkits.env_batch import ParallelEnvBatch
from rlkits.env_wrappers import AutoReset, StartWithRandomActions
from rlkits.env_wrappers import RecoverAction
from rlkits.running_mean_std import RunningMeanStd
from ipdb import set_trace
def to_tensor(*args):
new_args = []
for arg in args:
assert isinstance(arg, np.ndarray)
if arg.dtype == np.float64:
arg = arg.astype(np.float32)
new_args.append(torch.from_numpy(arg))
return new_args
def compute_loss(
policy,
target_policy,
value_net,
target_value_net,
batch,
gamma):
"""use a batch of experiences sampled from replay buffer to
compute the loss of policy and value net
batch: a batch of experiences sampled from replay buffer
gamma: discount factor
"""
obs, acs, rews, nxs, dones = batch['obs0'], batch['actions'],\
batch['rewards'], batch['obs1'], batch['terminals1']
obs, acs, rews, nxs, dones = to_tensor(
obs, acs, rews, nxs, dones)
# target for value net
with torch.no_grad():
nx_state_vals = target_value_net(nxs,
target_policy(nxs))
assert rews.shape == nx_state_vals.shape, f"{rews.shape}, {nx_state_vals.shape}"
q_targ = rews + (1 - dones)*gamma*nx_state_vals
# predicted q-value for the current state and action
q_pred = value_net(obs, acs)
value_loss = F.mse_loss(q_pred, q_targ)
# policy loss
policy_loss = -value_net(obs, policy(obs)).mean()
res = {
"policy_loss": policy_loss,
"value_loss": value_loss
}
return res
def DDPG(*,
env_name,
nsteps,
buf_size,
warm_up_steps,
gamma,
pi_lr,
v_lr,
polyak,
batch_size,
log_intervals,
max_grad_norm,
l2_weight_decay,
log_dir,
ckpt_dir,
**network_kwargs,
):
"""
env: gym env (parallel)
nsteps: number of steps to sample from the parallel env
nstep * env.nenvs frames will be sampled
ployak (float): linear interpolation coefficient for updating
the target policy and value net from the current ones;
Interpret it as the weight of the current target network
buf_size: size of the replay buffer
normalize_action: clip the action to [-1, 1]
"""
# env
def make_env():
env = gym.make(env_name)
env = StartWithRandomActions(env, max_random_actions=5)
env = RecoverAction(env)
return env
env = make_env()
if not os.path.exists(log_dir):
os.makedirs(log_dir)
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir)
logger.configure(dir=log_dir)
ob_space = env.observation_space
ac_space = env.action_space
print("======", f"action space shape {ac_space.shape}, \
ob space shape: {ob_space.shape}", "======")
policy = DeterministicPolicy(
ob_space=ob_space, ac_space=ac_space,
ckpt_dir=ckpt_dir,
**network_kwargs
)
target_policy = DeterministicPolicy(
ob_space=ob_space, ac_space=ac_space,
ckpt_dir=ckpt_dir,
**network_kwargs
)
target_policy.model.load_state_dict(policy.model.state_dict())
value_net = QNetForContinuousAction(
ob_space=ob_space, ac_space=ac_space, ckpt_dir=ckpt_dir,
**network_kwargs
)
target_value_net = QNetForContinuousAction(
ob_space=ob_space, ac_space=ac_space, ckpt_dir=ckpt_dir,
**network_kwargs
)
target_value_net.model.load_state_dict(value_net.model.state_dict())
poptimizer = optim.Adam(policy.parameters(), lr=pi_lr,
weight_decay=l2_weight_decay)
voptimizer = optim.Adam(value_net.parameters(), lr=v_lr,
weight_decay=l2_weight_decay)
replay_buffer = Memory(
limit=buf_size,
action_shape=ac_space.shape,
observation_shape=ob_space.shape
)
best_ret = np.float('-inf')
rolling_buf_episode_rets = deque(maxlen=10)
curr_state = env.reset()
policy.reset()
step = 0
episode_rews = 0.0
start = time.perf_counter()
while step <= nsteps:
# warm up steps
if step < warm_up_steps:
action = policy.random_action()
else:
action = policy.step(curr_state)
nx, rew, done, _ = env.step(action)
# record to the replay buffer
assert nx.shape == ob_space.shape, f"{nx.shape},{ob_space.shape}"
assert action.shape == ac_space.shape, f"{action.shape},{ac_space.shape}"
replay_buffer.append(
obs0=curr_state, action=action, reward=rew, obs1=nx, terminal1=done
)
episode_rews += rew
if done:
curr_state = env.reset()
policy.reset() # reset random process
rolling_buf_episode_rets.append(episode_rews)
episode_rews = 0
else:
curr_state = nx
if step > warm_up_steps:
# update policy and value
batch = replay_buffer.sample(batch_size)
losses = compute_loss(
policy, target_policy, value_net,
target_value_net, batch, gamma
)
ploss = losses['policy_loss']
poptimizer.zero_grad()
ploss.backward()
poptimizer.step()
vloss = losses['value_loss']
voptimizer.zero_grad()
vloss.backward()
voptimizer.step()
# update target value net and policy
for p, p_targ in zip(
policy.parameters(),
target_policy.parameters()):
p_targ.data.copy_(polyak*p_targ.data + (1-polyak)*p.data)
for p, p_targ in zip(
value_net.parameters(),
target_value_net.parameters()):
p_targ.data.copy_(polyak*p_targ.data + (1-polyak)*p.data)
if step % log_intervals == 0 and step > warm_up_steps:
# loss from policy and value
for k, v in losses.items():
logger.record_tabular(k, np.mean(v.detach().numpy()))
ret = np.mean(rolling_buf_episode_rets)
logger.record_tabular("ma_ep_ret", ret)
pw, tpw = policy.average_weight(), target_policy.average_weight()
vw, tvw = value_net.average_weight(), target_value_net.average_weight()
logger.record_tabular("policy_net_weight", pw)
logger.record_tabular("target_policy_net_weight", tpw)
logger.record_tabular("value_net_weight", vw)
logger.record_tabular("target_value_net_weight", tvw)
logger.dump_tabular()
if ret > best_ret:
best_ret = ret
policy.save_ckpt('policy-best')
value_net.save_ckpt('value-best')
torch.save(poptimizer, os.path.join(ckpt_dir,
'poptim-best.pth'))
torch.save(voptimizer, os.path.join(ckpt_dir,
'voptim-best.pth'))
step += 1
end = time.perf_counter()
logger.log(f"Total time elapsed: {end - start}")
policy.save_ckpt('policy-final')
value_net.save_ckpt('value-final')
torch.save(poptimizer, os.path.join(ckpt_dir, 'poptim-final.pth'))
torch.save(voptimizer, os.path.join(ckpt_dir, 'voptim-final.pth'))
return
if __name__ == '__main__':
DDPG(
env_name='Pendulum-v0',
nsteps=int(2e5),
buf_size=int(2e5),
warm_up_steps=int(1e3),
gamma=0.99,
pi_lr=1e-4,
v_lr=1e-4,
l2_weight_decay=1e-4,
polyak=0.99,
batch_size=128,
log_intervals=int(2e3),
max_grad_norm=0.1,
log_dir="/tmp/ddpg",
ckpt_dir="/tmp/ddpg",
hidden_layers=[256, 256, 64]
)
# 32 x 8 = 256 exprs sampled per iter
# 256000