-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppo.py
354 lines (280 loc) · 10.9 KB
/
ppo.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
# PPO
from collections import deque, defaultdict
import numpy as np
import time
import os
import gym
import pprint as pp
import pickle
import sys
from rlkits.sampler import ParallelEnvTrajectorySampler
from rlkits.sampler import estimate_Q, aggregate_experience
from rlkits.policies import PolicyWithValue
import rlkits.utils as U
import rlkits.utils.logger as logger
from rlkits.utils.math import explained_variance, KL
from rlkits.utils.context import timed
import torch
import torch.optim as optim
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_
def compute_loss(oldpi, pi, trajectory, midx, eps, log_dir):
"""compute loss for policy and value net
oldpi: old policy
pi: current policy
trajectory: trajectory to compute loss on
midx: mini-batch indices
eps: clip range
log_dir: directory to write logs
"""
# policy loss
old_log_prob = trajectory['log_prob'][midx]
old_log_prob = torch.from_numpy(old_log_prob)
adv = trajectory['adv'][midx]
adv = (adv - adv.mean()) / adv.std()
adv = torch.from_numpy(adv)
obs = trajectory['obs'][midx]
obs = torch.from_numpy(obs)
dist = pi.dist(pi.policy_net(obs))
if dist is None:
logger.log('Got NaN -- Bad')
pi.save_ckpt()
args = {
"trajectory": trajectory,
"midx": midx,
"eps": eps
}
with open(os.path.join(ckpt_dir, 'local.pkl'), 'wb') as f:
pickle.dump(args, f)
sys.exit()
actions = torch.from_numpy(trajectory['actions'][midx])
log_prob = dist.log_prob(actions)
# importance sampling ratio
ratio = torch.exp(log_prob - old_log_prob)
if len(ratio.shape) > 1:
ratio = ratio.squeeze(dim=1)
assert ratio.shape == adv.shape, f"ratio shape: {ratio.shape}, adv shape : {adv.shape}"
# Clip ratio
upper_bound = torch.zeros_like(ratio, dtype=torch.float32).fill_(1 + eps)
lower_bound = torch.zeros_like(ratio, dtype=torch.float32).fill_(1 - eps)
clipped_ratio = ratio.clone()
above = (ratio > upper_bound)
clipped_ratio[above] = torch.mul(
clipped_ratio[above], torch.div(
upper_bound[above], clipped_ratio[above]
))
below = (ratio < lower_bound)
clipped_ratio[below] = torch.mul(
clipped_ratio[below], torch.div(
lower_bound[below], clipped_ratio[below]
))
clipped_surr_gain = torch.minimum(
ratio * adv, clipped_ratio * adv
)
# Compute KL
with torch.no_grad():
oldpi_dist = oldpi.dist(oldpi.policy_net(obs))
kl = KL(oldpi_dist, dist).mean()
# value loss
# want to make sure the difference between new value prediction
# and the new value prediction is clipped within [-\eps, eps]
vpreds = pi.value_net(obs)
mQ = trajectory['Q'][midx]
mQ = torch.from_numpy(mQ)
if len(vpreds.shape) > 1:
vpreds = vpreds.squeeze(dim=1)
assert vpreds.shape == mQ.shape, f"vpreds shape: {vpreds.shape}, mQ shape : {mQ.shape}"
v_loss = F.mse_loss(vpreds, mQ)
losses = {
"ratio": ratio.mean(),
"clipped_ratio": clipped_ratio.mean(),
"surr_gain": (ratio*adv).mean(),
"clipped_surr_gain": clipped_surr_gain.mean(),
"meankl": kl,
"entropy": dist.entropy().mean(),
"v_loss": v_loss
}
return losses
def sync_policies(oldpi, pi):
# oldpi <- pi
oldpi.policy_net.load_state_dict(pi.policy_net.state_dict())
oldpi.value_net.load_state_dict(pi.value_net.state_dict())
return
def PPO(*,
env,
nsteps,
total_timesteps,
max_kl,
beta,
eps, # epsilon
gamma, # discount factor
pi_lr, # policy learning rate
v_lr, # value net learning rate
ent_coef,
epochs, # number of training epochs per policy update
batch_size,
log_interval,
max_grad_norm,
reward_transform,
log_dir,
ckpt_dir,
**network_kwargs
):
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
pi = PolicyWithValue(ob_space=ob_space,
ac_space=ac_space, ckpt_dir=ckpt_dir,
**network_kwargs)
oldpi = PolicyWithValue(ob_space=ob_space,
ac_space=ac_space, ckpt_dir=ckpt_dir,
**network_kwargs)
poptimizer = optim.Adam(pi.policy_net.parameters(),
lr=pi_lr)
voptimizer = optim.Adam(pi.value_net.parameters(),
lr=v_lr)
sampler = ParallelEnvTrajectorySampler(env, oldpi, nsteps,
reward_transform=reward_transform, gamma=gamma) #
rolling_buf_episode_rets = deque(maxlen=100)
rolling_buf_episode_lens = deque(maxlen=100)
nframes = env.nenvs * nsteps # number of frames processed by update iter
nupdates = total_timesteps // nframes
best_ret = np.float('-inf')
start = time.perf_counter()
for update in range(1, nupdates+1):
sync_policies(oldpi, pi)
tstart = time.perf_counter()
trajectory = sampler(callback=estimate_Q)
# aggregate exps from parallel envs
for k, v in trajectory.items():
if isinstance(v, np.ndarray):
trajectory[k] = aggregate_experience(v)
adv = trajectory['Q'] - trajectory['vpreds']
trajectory['adv'] = (adv - adv.mean())/adv.std()
# determine the clip range for the current update step
frac = 1.0 - (update - 1.0)/nupdates
cliprange = 0.5*eps * frac + 0.5*eps
# update policy
idx = np.arange(len(trajectory['obs']))
lossvals = defaultdict(list)
for _ in range(epochs):
np.random.shuffle(idx)
for i in range(0, len(idx), batch_size):
midx = idx[i:i+batch_size] # indices of exps to train
losses = compute_loss(
oldpi=oldpi,
pi=pi,
trajectory=trajectory,
midx=midx,
eps=cliprange,
log_dir=log_dir
)
meankl = losses['meankl'].detach().item()
if meankl > 1.5 * max_kl:
beta *= 2
elif meankl < max_kl / 1.5:
beta /=2
p_loss = -(losses['clipped_surr_gain'] - beta * losses['meankl'] + \
frac*ent_coef*losses['entropy'])
poptimizer.zero_grad()
p_loss.backward()
clip_grad_norm_(
pi.policy_net.parameters(), max_norm=max_grad_norm
)
poptimizer.step()
v_loss = losses['v_loss']
voptimizer.zero_grad()
v_loss.backward()
clip_grad_norm_(
pi.value_net.parameters(), max_norm=max_grad_norm
)
voptimizer.step()
for k, v in losses.items():
lossvals[k].append(v.detach().item())
tnow = time.perf_counter()
fps = int(nframes / (tnow - tstart)) # frames per seconds
# logging
if update % log_interval == 0 or update==1:
logger.record_tabular('iteration/nupdates', f"{update}/{nupdates}")
logger.record_tabular('cliprange', cliprange)
for ep_rets in trajectory['ep_rets']:
rolling_buf_episode_rets.extend(ep_rets)
for ep_lens in trajectory['ep_lens']:
rolling_buf_episode_lens.extend(ep_lens)
# explained variance
ev = explained_variance(trajectory['vpreds'], trajectory['Q'])
logger.record_tabular('explained_variance', ev)
piw, vw = pi.average_weight()
logger.record_tabular('policy_net_weight', piw.numpy())
logger.record_tabular('value_net_weight', vw.numpy())
# losses
for k, v in lossvals.items():
logger.record_tabular(k, np.mean(v))
# upper and lower bound to clip the importance sampling ratio
logger.record_tabular('upper_bound', 1 + cliprange)
logger.record_tabular('lower_bound', 1 - cliprange)
vqdiff = np.mean((trajectory['Q'] - trajectory['vpreds'])**2)
logger.record_tabular('VQDiff', vqdiff)
logger.record_tabular('Q', np.mean(trajectory['Q']))
logger.record_tabular('vpreds', np.mean(trajectory['vpreds']))
logger.record_tabular('FPS', fps)
ret = safemean(rolling_buf_episode_rets)
logger.record_tabular("ma_ep_ret", ret)
logger.record_tabular('ma_ep_len', safemean(rolling_buf_episode_lens))
logger.record_tabular('mean_step_rew', safemean(trajectory['rews']))
logger.dump_tabular()
if ret !=np.nan and ret > best_ret:
best_ret = ret
pi.save_ckpt('best')
torch.save(poptimizer, os.path.join(ckpt_dir, 'poptim-best.pth'))
torch.save(voptimizer, os.path.join(ckpt_dir, 'voptim-best.pth'))
end = time.perf_counter()
logger.log(f'Total training time: {end - start}')
pi.save_ckpt('final')
torch.save(poptimizer, os.path.join(ckpt_dir, 'poptim-final.pth'))
torch.save(voptimizer, os.path.join(ckpt_dir, 'voptim-final.pth'))
return
def safemean(l):
return np.nan if len(l) == 0 else np.mean(l)
if __name__ == '__main__':
from rlkits.env_batch import ParallelEnvBatch
from rlkits.env_wrappers import AutoReset, StartWithRandomActions
def make_env():
env = gym.make('CartPole-v0').unwrapped
env = AutoReset(env)
env = StartWithRandomActions(env, max_random_actions=5)
return env
def pendulum():
env = gym.make('Pendulum-v0')
env = AutoReset(env)
env = StartWithRandomActions(env, max_random_actions=5)
return env
nenvs = 16
nsteps = 128
env=ParallelEnvBatch(make_env, nenvs=nenvs)
PPO(
env=env,
nsteps=nsteps,
total_timesteps=nenvs*nsteps*10000,
max_kl=1e-2,
beta=0.5,
eps = 0.2,
gamma=0.99,
pi_lr=1e-4,
v_lr = 1e-4,
ent_coef=0.0,
epochs=3,
batch_size=nenvs*nsteps,
log_interval=10,
max_grad_norm=0.1,
reward_transform=None,
log_dir='/home/ubuntu/reinforcement-learning/experiments/ppo/pendulum/0',
ckpt_dir='/home/ubuntu/reinforcement-learning/experiments/ppo/pendulum/0',
hidden_layers=[256, 256, 64],
activation=torch.nn.ReLU,
)
env.close()