fix: avoids local ruff mismatch
This commit is contained in:
parent
a452912b36
commit
98a8f529e1
3 changed files with 24 additions and 33 deletions
|
|
@ -1,4 +1,12 @@
|
||||||
repos:
|
repos:
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: ruff-format
|
||||||
|
name: ruff format (pipx)
|
||||||
|
entry: pipx run ruff format
|
||||||
|
language: system
|
||||||
|
types: [python]
|
||||||
|
|
||||||
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
|
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
|
||||||
rev: v9.16.0
|
rev: v9.16.0
|
||||||
hooks:
|
hooks:
|
||||||
|
|
@ -6,13 +14,6 @@ repos:
|
||||||
stages: [commit-msg]
|
stages: [commit-msg]
|
||||||
additional_dependencies: ["@commitlint/config-conventional"]
|
additional_dependencies: ["@commitlint/config-conventional"]
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
||||||
rev: v0.9.9
|
|
||||||
hooks:
|
|
||||||
- id: ruff
|
|
||||||
args: [ --fix ]
|
|
||||||
- id: ruff-format
|
|
||||||
|
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v5.0.0
|
rev: v5.0.0
|
||||||
hooks:
|
hooks:
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
line-length = 100
|
||||||
|
|
||||||
[lint]
|
[lint]
|
||||||
extend-select = [
|
extend-select = [
|
||||||
# "PLC0103", # invalid-name
|
# "PLC0103", # invalid-name
|
||||||
|
|
@ -347,6 +349,7 @@ extend-ignore = [
|
||||||
# "PLR1705", # no-else-return
|
# "PLR1705", # no-else-return
|
||||||
# "PLR1706", # consider-using-ternary
|
# "PLR1706", # consider-using-ternary
|
||||||
# "PLR1707", # trailing-comma-tuple
|
# "PLR1707", # trailing-comma-tuple
|
||||||
|
"PLR1708", # stop-iteration-return
|
||||||
# "PLR1709", # simplify-boolean-expression
|
# "PLR1709", # simplify-boolean-expression
|
||||||
# "PLR1710", # inconsistent-return-statements
|
# "PLR1710", # inconsistent-return-statements
|
||||||
"PLR1711", # useless-return
|
"PLR1711", # useless-return
|
||||||
|
|
@ -391,4 +394,3 @@ extend-ignore = [
|
||||||
# "PLW1404", # implicit-str-concat
|
# "PLW1404", # implicit-str-concat
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
38
src/ppo.py
38
src/ppo.py
|
|
@ -8,9 +8,7 @@ import jax.numpy as jnp
|
||||||
# Chose to use a class as it seemed the easiest way to integrate the CleanRL code style
|
# Chose to use a class as it seemed the easiest way to integrate the CleanRL code style
|
||||||
# with our need to seperate concerns
|
# with our need to seperate concerns
|
||||||
class PPO:
|
class PPO:
|
||||||
def __init__(
|
def __init__(self, args, input_network, action_network, critic, message_passer=None):
|
||||||
self, args, input_network, action_network, critic, message_passer=None
|
|
||||||
):
|
|
||||||
self.args = args
|
self.args = args
|
||||||
|
|
||||||
if not message_passer:
|
if not message_passer:
|
||||||
|
|
@ -50,15 +48,13 @@ class PPO:
|
||||||
shuffled_storage = jax.tree.map(convert_data, flatten_storage)
|
shuffled_storage = jax.tree.map(convert_data, flatten_storage)
|
||||||
|
|
||||||
def update_minibatch(agent_state, minibatch):
|
def update_minibatch(agent_state, minibatch):
|
||||||
(loss, (pg_loss, v_loss, entropy_loss, approx_kl)), grads = (
|
(loss, (pg_loss, v_loss, entropy_loss, approx_kl)), grads = ppo_loss_grad_fn(
|
||||||
ppo_loss_grad_fn(
|
agent_state.params,
|
||||||
agent_state.params,
|
minibatch.obs,
|
||||||
minibatch.obs,
|
minibatch.actions,
|
||||||
minibatch.actions,
|
minibatch.logprobs,
|
||||||
minibatch.logprobs,
|
minibatch.advantages,
|
||||||
minibatch.advantages,
|
minibatch.returns,
|
||||||
minibatch.returns,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
agent_state = agent_state.apply_gradients(grads=grads)
|
agent_state = agent_state.apply_gradients(grads=grads)
|
||||||
return agent_state, (
|
return agent_state, (
|
||||||
|
|
@ -70,15 +66,11 @@ class PPO:
|
||||||
grads,
|
grads,
|
||||||
)
|
)
|
||||||
|
|
||||||
agent_state, metrics = jax.lax.scan(
|
agent_state, metrics = jax.lax.scan(update_minibatch, agent_state, shuffled_storage)
|
||||||
update_minibatch, agent_state, shuffled_storage
|
|
||||||
)
|
|
||||||
return (agent_state, key), metrics
|
return (agent_state, key), metrics
|
||||||
|
|
||||||
(agent_state, key), (loss, pg_loss, v_loss, entropy_loss, approx_kl, grads) = (
|
(agent_state, key), (loss, pg_loss, v_loss, entropy_loss, approx_kl, grads) = jax.lax.scan(
|
||||||
jax.lax.scan(
|
update_epoch, (agent_state, key), (), length=args.update_epochs
|
||||||
update_epoch, (agent_state, key), (), length=args.update_epochs
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return agent_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key
|
return agent_state, loss, pg_loss, v_loss, entropy_loss, approx_kl, key
|
||||||
|
|
||||||
|
|
@ -106,9 +98,7 @@ def get_action_and_value2(
|
||||||
mean, log_std = action_apply(params["actor_params"], hidden)
|
mean, log_std = action_apply(params["actor_params"], hidden)
|
||||||
std = jnp.exp(log_std)
|
std = jnp.exp(log_std)
|
||||||
|
|
||||||
logprob = -0.5 * (
|
logprob = -0.5 * (((action - mean) / std) ** 2 + 2 * log_std + jnp.log(2 * jnp.pi)).sum(-1)
|
||||||
((action - mean) / std) ** 2 + 2 * log_std + jnp.log(2 * jnp.pi)
|
|
||||||
).sum(-1)
|
|
||||||
entropy = (0.5 + 0.5 * jnp.log(2 * jnp.pi) + log_std).sum(-1)
|
entropy = (0.5 + 0.5 * jnp.log(2 * jnp.pi) + log_std).sum(-1)
|
||||||
value = critic_apply(params["critic_params"], hidden).squeeze(-1)
|
value = critic_apply(params["critic_params"], hidden).squeeze(-1)
|
||||||
|
|
||||||
|
|
@ -142,9 +132,7 @@ def ppo_loss(
|
||||||
approx_kl = ((ratio - 1) - logratio).mean()
|
approx_kl = ((ratio - 1) - logratio).mean()
|
||||||
|
|
||||||
if args.norm_adv:
|
if args.norm_adv:
|
||||||
mb_advantages = (mb_advantages - mb_advantages.mean()) / (
|
mb_advantages = (mb_advantages - mb_advantages.mean()) / (mb_advantages.std() + 1e-8)
|
||||||
mb_advantages.std() + 1e-8
|
|
||||||
)
|
|
||||||
|
|
||||||
pg_loss1 = -mb_advantages * ratio
|
pg_loss1 = -mb_advantages * ratio
|
||||||
pg_loss2 = -mb_advantages * jnp.clip(ratio, 1 - args.clip_coef, 1 + args.clip_coef)
|
pg_loss2 = -mb_advantages * jnp.clip(ratio, 1 - args.clip_coef, 1 + args.clip_coef)
|
||||||
|
|
|
||||||
Reference in a new issue