Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vloss clipping to jax ppo. #426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cleanrl/ppo_atari_envpool_xla_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def parse_args():
help="Toggles advantages normalization")
parser.add_argument("--clip-coef", type=float, default=0.1,
help="the surrogate clipping coefficient")
parser.add_argument("--clip-vloss", type=lambda x: bool(strtobool(x)), default=True, nargs="?", const=True,
help="Toggles whether or not to use a clipped loss for the value function, as per the paper.")
parser.add_argument("--ent-coef", type=float, default=0.01,
help="coefficient of the entropy")
parser.add_argument("--vf-coef", type=float, default=0.5,
Expand Down Expand Up @@ -352,8 +354,9 @@ def update_ppo(
b_actions = storage.actions.reshape((-1,) + envs.single_action_space.shape)
b_advantages = storage.advantages.reshape(-1)
b_returns = storage.returns.reshape(-1)
b_values = storage.values.reshape(-1)

def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
def ppo_loss(params, x, a, logp, mb_advantages, mb_returns, mb_values):
newlogprob, entropy, newvalue = get_action_and_value2(params, x, a)
logratio = newlogprob - logp
ratio = jnp.exp(logratio)
Expand All @@ -368,7 +371,18 @@ def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
pg_loss = jnp.maximum(pg_loss1, pg_loss2).mean()

# Value loss
v_loss = 0.5 * ((newvalue - mb_returns) ** 2).mean()
if args.clip_vloss:
v_loss_unclipped = (newvalue - mb_returns) ** 2
v_clipped = mb_values + jnp.clip(
newvalue - mb_values,
-args.clip_coef,
args.clip_coef,
)
v_loss_clipped = (v_clipped - mb_returns) ** 2
v_loss_max = jnp.maximum(v_loss_unclipped, v_loss_clipped)
v_loss = 0.5 * v_loss_max.mean()
else:
v_loss = 0.5 * ((newvalue - mb_returns) ** 2).mean()

entropy_loss = entropy.mean()
loss = pg_loss - args.ent_coef * entropy_loss + v_loss * args.vf_coef
Expand All @@ -388,6 +402,7 @@ def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
b_logprobs[mb_inds],
b_advantages[mb_inds],
b_returns[mb_inds],
b_values[mb_inds],
)
agent_state = agent_state.apply_gradients(grads=grads)
return agent_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key
Expand Down
18 changes: 16 additions & 2 deletions cleanrl/ppo_atari_envpool_xla_jax_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def parse_args():
help="Toggles advantages normalization")
parser.add_argument("--clip-coef", type=float, default=0.1,
help="the surrogate clipping coefficient")
parser.add_argument("--clip-vloss", type=lambda x: bool(strtobool(x)), default=True, nargs="?", const=True,
help="Toggles whether or not to use a clipped loss for the value function, as per the paper.")
parser.add_argument("--ent-coef", type=float, default=0.01,
help="coefficient of the entropy")
parser.add_argument("--vf-coef", type=float, default=0.5,
Expand Down Expand Up @@ -343,7 +345,7 @@ def compute_gae(
)
return storage

def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
def ppo_loss(params, x, a, logp, mb_advantages, mb_returns, mb_values):
newlogprob, entropy, newvalue = get_action_and_value2(params, x, a)
logratio = newlogprob - logp
ratio = jnp.exp(logratio)
Expand All @@ -358,7 +360,18 @@ def ppo_loss(params, x, a, logp, mb_advantages, mb_returns):
pg_loss = jnp.maximum(pg_loss1, pg_loss2).mean()

# Value loss
v_loss = 0.5 * ((newvalue - mb_returns) ** 2).mean()
if args.clip_vloss:
v_loss_unclipped = (newvalue - mb_returns) ** 2
v_clipped = mb_values + jnp.clip(
newvalue - mb_values,
-args.clip_coef,
args.clip_coef,
)
v_loss_clipped = (v_clipped - mb_returns) ** 2
v_loss_max = jnp.maximum(v_loss_unclipped, v_loss_clipped)
v_loss = 0.5 * v_loss_max.mean()
else:
v_loss = 0.5 * ((newvalue - mb_returns) ** 2).mean()

entropy_loss = entropy.mean()
loss = pg_loss - args.ent_coef * entropy_loss + v_loss * args.vf_coef
Expand Down Expand Up @@ -396,6 +409,7 @@ def update_minibatch(agent_state, minibatch):
minibatch.logprobs,
minibatch.advantages,
minibatch.returns,
minibatch.values,
)
agent_state = agent_state.apply_gradients(grads=grads)
return agent_state, (loss, pg_loss, v_loss, entropy_loss, approx_kl, grads)
Expand Down
Loading