pushing model
Browse files- README.md +76 -0
- events.out.tfevents.1697013370.3090-172.511069.0 +3 -0
- poetry.lock +0 -0
- pyproject.toml +108 -0
- replay.mp4 +0 -0
- td3_continuous_action_jax.cleanrl_model +0 -0
- td3_continuous_action_jax.py +373 -0
- videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-0.mp4 +0 -0
- videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-1.mp4 +0 -0
- videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-8.mp4 +0 -0
README.md
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- Hopper-v4
|
4 |
+
- deep-reinforcement-learning
|
5 |
+
- reinforcement-learning
|
6 |
+
- custom-implementation
|
7 |
+
library_name: cleanrl
|
8 |
+
model-index:
|
9 |
+
- name: TD3
|
10 |
+
results:
|
11 |
+
- task:
|
12 |
+
type: reinforcement-learning
|
13 |
+
name: reinforcement-learning
|
14 |
+
dataset:
|
15 |
+
name: Hopper-v4
|
16 |
+
type: Hopper-v4
|
17 |
+
metrics:
|
18 |
+
- type: mean_reward
|
19 |
+
value: 5.58 +/- 0.31
|
20 |
+
name: mean_reward
|
21 |
+
verified: false
|
22 |
+
---
|
23 |
+
|
24 |
+
# (CleanRL) **TD3** Agent Playing **Hopper-v4**
|
25 |
+
|
26 |
+
This is a trained model of a TD3 agent playing Hopper-v4.
|
27 |
+
The model was trained by using [CleanRL](https://github.com/vwxyzjn/cleanrl) and the most up-to-date training code can be
|
28 |
+
found [here](https://github.com/vwxyzjn/cleanrl/blob/master/cleanrl/td3_continuous_action_jax.py).
|
29 |
+
|
30 |
+
## Get Started
|
31 |
+
|
32 |
+
To use this model, please install the `cleanrl` package with the following command:
|
33 |
+
|
34 |
+
```
|
35 |
+
pip install "cleanrl[td3_continuous_action_jax]"
|
36 |
+
python -m cleanrl_utils.enjoy --exp-name td3_continuous_action_jax --env-id Hopper-v4
|
37 |
+
```
|
38 |
+
|
39 |
+
Please refer to the [documentation](https://docs.cleanrl.dev/get-started/zoo/) for more detail.
|
40 |
+
|
41 |
+
|
42 |
+
## Command to reproduce the training
|
43 |
+
|
44 |
+
```bash
|
45 |
+
curl -OL https://huggingface.co/sdpkjc/Hopper-v4-td3_continuous_action_jax-seed1/raw/main/td3_continuous_action_jax.py
|
46 |
+
curl -OL https://huggingface.co/sdpkjc/Hopper-v4-td3_continuous_action_jax-seed1/raw/main/pyproject.toml
|
47 |
+
curl -OL https://huggingface.co/sdpkjc/Hopper-v4-td3_continuous_action_jax-seed1/raw/main/poetry.lock
|
48 |
+
poetry install --all-extras
|
49 |
+
python td3_continuous_action_jax.py --env-id Hopper-v4 --learning-starts 100 --batch-size 32 --total-timesteps 105 --save-model --upload-model --hf-entity sdpkjc
|
50 |
+
```
|
51 |
+
|
52 |
+
# Hyperparameters
|
53 |
+
```python
|
54 |
+
{'batch_size': 32,
|
55 |
+
'buffer_size': 1000000,
|
56 |
+
'capture_video': False,
|
57 |
+
'env_id': 'Hopper-v4',
|
58 |
+
'exp_name': 'td3_continuous_action_jax',
|
59 |
+
'exploration_noise': 0.1,
|
60 |
+
'gamma': 0.99,
|
61 |
+
'hf_entity': 'sdpkjc',
|
62 |
+
'learning_rate': 0.0003,
|
63 |
+
'learning_starts': 100,
|
64 |
+
'noise_clip': 0.5,
|
65 |
+
'policy_frequency': 2,
|
66 |
+
'policy_noise': 0.2,
|
67 |
+
'save_model': True,
|
68 |
+
'seed': 1,
|
69 |
+
'tau': 0.005,
|
70 |
+
'total_timesteps': 105,
|
71 |
+
'track': False,
|
72 |
+
'upload_model': True,
|
73 |
+
'wandb_entity': None,
|
74 |
+
'wandb_project_name': 'cleanRL'}
|
75 |
+
```
|
76 |
+
|
events.out.tfevents.1697013370.3090-172.511069.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:05c0fef3ab16248b3346ee5aac9db2c6dfdd28665db0d70bf0a158c680759bd5
|
3 |
+
size 1620
|
poetry.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|
pyproject.toml
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[tool.poetry]
|
2 |
+
name = "cleanrl"
|
3 |
+
version = "1.1.0"
|
4 |
+
description = "High-quality single file implementation of Deep Reinforcement Learning algorithms with research-friendly features"
|
5 |
+
authors = ["Costa Huang <costa.huang@outlook.com>"]
|
6 |
+
packages = [
|
7 |
+
{ include = "cleanrl" },
|
8 |
+
{ include = "cleanrl_utils" },
|
9 |
+
]
|
10 |
+
keywords = ["reinforcement", "machine", "learning", "research"]
|
11 |
+
license="MIT"
|
12 |
+
readme = "README.md"
|
13 |
+
|
14 |
+
[tool.poetry.dependencies]
|
15 |
+
python = ">=3.7.1,<3.11"
|
16 |
+
tensorboard = "^2.10.0"
|
17 |
+
wandb = "^0.13.11"
|
18 |
+
gym = "0.23.1"
|
19 |
+
torch = ">=1.12.1"
|
20 |
+
stable-baselines3 = "1.2.0"
|
21 |
+
gymnasium = ">=0.28.1"
|
22 |
+
moviepy = "^1.0.3"
|
23 |
+
pygame = "2.1.0"
|
24 |
+
huggingface-hub = "^0.11.1"
|
25 |
+
rich = "<12.0"
|
26 |
+
tenacity = "^8.2.2"
|
27 |
+
|
28 |
+
ale-py = {version = "0.7.4", optional = true}
|
29 |
+
AutoROM = {extras = ["accept-rom-license"], version = "^0.4.2", optional = true}
|
30 |
+
opencv-python = {version = "^4.6.0.66", optional = true}
|
31 |
+
procgen = {version = "^0.10.7", optional = true}
|
32 |
+
pytest = {version = "^7.1.3", optional = true}
|
33 |
+
mujoco = {version = "<=2.3.3", optional = true}
|
34 |
+
imageio = {version = "^2.14.1", optional = true}
|
35 |
+
free-mujoco-py = {version = "^2.1.6", optional = true}
|
36 |
+
mkdocs-material = {version = "^8.4.3", optional = true}
|
37 |
+
markdown-include = {version = "^0.7.0", optional = true}
|
38 |
+
openrlbenchmark = {version = "^0.1.1b4", optional = true}
|
39 |
+
jax = {version = "^0.3.17", optional = true}
|
40 |
+
jaxlib = {version = "^0.3.15", optional = true}
|
41 |
+
flax = {version = "^0.6.0", optional = true}
|
42 |
+
optuna = {version = "^3.0.1", optional = true}
|
43 |
+
optuna-dashboard = {version = "^0.7.2", optional = true}
|
44 |
+
envpool = {version = "^0.6.4", optional = true}
|
45 |
+
PettingZoo = {version = "1.18.1", optional = true}
|
46 |
+
SuperSuit = {version = "3.4.0", optional = true}
|
47 |
+
multi-agent-ale-py = {version = "0.1.11", optional = true}
|
48 |
+
boto3 = {version = "^1.24.70", optional = true}
|
49 |
+
awscli = {version = "^1.25.71", optional = true}
|
50 |
+
shimmy = {version = ">=1.0.0", extras = ["dm-control"], optional = true}
|
51 |
+
|
52 |
+
[tool.poetry.group.dev.dependencies]
|
53 |
+
pre-commit = "^2.20.0"
|
54 |
+
|
55 |
+
|
56 |
+
[tool.poetry.group.isaacgym]
|
57 |
+
optional = true
|
58 |
+
[tool.poetry.group.isaacgym.dependencies]
|
59 |
+
isaacgymenvs = {git = "https://github.com/vwxyzjn/IsaacGymEnvs.git", rev = "poetry", python = ">=3.7.1,<3.10"}
|
60 |
+
isaacgym = {path = "cleanrl/ppo_continuous_action_isaacgym/isaacgym", develop = true}
|
61 |
+
|
62 |
+
|
63 |
+
[build-system]
|
64 |
+
requires = ["poetry-core"]
|
65 |
+
build-backend = "poetry.core.masonry.api"
|
66 |
+
|
67 |
+
[tool.poetry.extras]
|
68 |
+
atari = ["ale-py", "AutoROM", "opencv-python"]
|
69 |
+
procgen = ["procgen"]
|
70 |
+
plot = ["pandas", "seaborn"]
|
71 |
+
pytest = ["pytest"]
|
72 |
+
mujoco = ["mujoco", "imageio"]
|
73 |
+
mujoco_py = ["free-mujoco-py"]
|
74 |
+
jax = ["jax", "jaxlib", "flax"]
|
75 |
+
docs = ["mkdocs-material", "markdown-include", "openrlbenchmark"]
|
76 |
+
envpool = ["envpool"]
|
77 |
+
optuna = ["optuna", "optuna-dashboard"]
|
78 |
+
pettingzoo = ["PettingZoo", "SuperSuit", "multi-agent-ale-py"]
|
79 |
+
cloud = ["boto3", "awscli"]
|
80 |
+
dm_control = ["shimmy", "mujoco"]
|
81 |
+
|
82 |
+
# dependencies for algorithm variant (useful when you want to run a specific algorithm)
|
83 |
+
dqn = []
|
84 |
+
dqn_atari = ["ale-py", "AutoROM", "opencv-python"]
|
85 |
+
dqn_jax = ["jax", "jaxlib", "flax"]
|
86 |
+
dqn_atari_jax = [
|
87 |
+
"ale-py", "AutoROM", "opencv-python", # atari
|
88 |
+
"jax", "jaxlib", "flax" # jax
|
89 |
+
]
|
90 |
+
c51 = []
|
91 |
+
c51_atari = ["ale-py", "AutoROM", "opencv-python"]
|
92 |
+
c51_jax = ["jax", "jaxlib", "flax"]
|
93 |
+
c51_atari_jax = [
|
94 |
+
"ale-py", "AutoROM", "opencv-python", # atari
|
95 |
+
"jax", "jaxlib", "flax" # jax
|
96 |
+
]
|
97 |
+
ppo_atari_envpool_xla_jax_scan = [
|
98 |
+
"ale-py", "AutoROM", "opencv-python", # atari
|
99 |
+
"jax", "jaxlib", "flax", # jax
|
100 |
+
"envpool", # envpool
|
101 |
+
]
|
102 |
+
qdagger_dqn_atari_impalacnn = [
|
103 |
+
"ale-py", "AutoROM", "opencv-python"
|
104 |
+
]
|
105 |
+
qdagger_dqn_atari_jax_impalacnn = [
|
106 |
+
"ale-py", "AutoROM", "opencv-python", # atari
|
107 |
+
"jax", "jaxlib", "flax", # jax
|
108 |
+
]
|
replay.mp4
ADDED
Binary file (18.1 kB). View file
|
|
td3_continuous_action_jax.cleanrl_model
ADDED
Binary file (838 kB). View file
|
|
td3_continuous_action_jax.py
ADDED
@@ -0,0 +1,373 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# docs and experiment results can be found at https://docs.cleanrl.dev/rl-algorithms/td3/#td3_continuous_action_jaxpy
|
2 |
+
import argparse
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import time
|
6 |
+
from distutils.util import strtobool
|
7 |
+
|
8 |
+
import flax
|
9 |
+
import flax.linen as nn
|
10 |
+
import gymnasium as gym
|
11 |
+
import jax
|
12 |
+
import jax.numpy as jnp
|
13 |
+
import numpy as np
|
14 |
+
import optax
|
15 |
+
from flax.training.train_state import TrainState
|
16 |
+
from stable_baselines3.common.buffers import ReplayBuffer
|
17 |
+
from torch.utils.tensorboard import SummaryWriter
|
18 |
+
|
19 |
+
|
20 |
+
def parse_args():
|
21 |
+
# fmt: off
|
22 |
+
parser = argparse.ArgumentParser()
|
23 |
+
parser.add_argument("--exp-name", type=str, default=os.path.basename(__file__).rstrip(".py"),
|
24 |
+
help="the name of this experiment")
|
25 |
+
parser.add_argument("--seed", type=int, default=1,
|
26 |
+
help="seed of the experiment")
|
27 |
+
parser.add_argument("--track", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
28 |
+
help="if toggled, this experiment will be tracked with Weights and Biases")
|
29 |
+
parser.add_argument("--wandb-project-name", type=str, default="cleanRL",
|
30 |
+
help="the wandb's project name")
|
31 |
+
parser.add_argument("--wandb-entity", type=str, default=None,
|
32 |
+
help="the entity (team) of wandb's project")
|
33 |
+
parser.add_argument("--capture-video", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
34 |
+
help="whether to capture videos of the agent performances (check out `videos` folder)")
|
35 |
+
parser.add_argument("--save-model", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
36 |
+
help="whether to save model into the `runs/{run_name}` folder")
|
37 |
+
parser.add_argument("--upload-model", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
38 |
+
help="whether to upload the saved model to huggingface")
|
39 |
+
parser.add_argument("--hf-entity", type=str, default="",
|
40 |
+
help="the user or org name of the model repository from the Hugging Face Hub")
|
41 |
+
|
42 |
+
# Algorithm specific arguments
|
43 |
+
parser.add_argument("--env-id", type=str, default="HalfCheetah-v4",
|
44 |
+
help="the id of the environment")
|
45 |
+
parser.add_argument("--total-timesteps", type=int, default=1000000,
|
46 |
+
help="total timesteps of the experiments")
|
47 |
+
parser.add_argument("--learning-rate", type=float, default=3e-4,
|
48 |
+
help="the learning rate of the optimizer")
|
49 |
+
parser.add_argument("--buffer-size", type=int, default=int(1e6),
|
50 |
+
help="the replay memory buffer size")
|
51 |
+
parser.add_argument("--gamma", type=float, default=0.99,
|
52 |
+
help="the discount factor gamma")
|
53 |
+
parser.add_argument("--tau", type=float, default=0.005,
|
54 |
+
help="target smoothing coefficient (default: 0.005)")
|
55 |
+
parser.add_argument("--policy-noise", type=float, default=0.2,
|
56 |
+
help="the scale of policy noise")
|
57 |
+
parser.add_argument("--batch-size", type=int, default=256,
|
58 |
+
help="the batch size of sample from the reply memory")
|
59 |
+
parser.add_argument("--exploration-noise", type=float, default=0.1,
|
60 |
+
help="the scale of exploration noise")
|
61 |
+
parser.add_argument("--learning-starts", type=int, default=25e3,
|
62 |
+
help="timestep to start learning")
|
63 |
+
parser.add_argument("--policy-frequency", type=int, default=2,
|
64 |
+
help="the frequency of training policy (delayed)")
|
65 |
+
parser.add_argument("--noise-clip", type=float, default=0.5,
|
66 |
+
help="noise clip parameter of the Target Policy Smoothing Regularization")
|
67 |
+
args = parser.parse_args()
|
68 |
+
# fmt: on
|
69 |
+
return args
|
70 |
+
|
71 |
+
|
72 |
+
def make_env(env_id, seed, idx, capture_video, run_name):
|
73 |
+
def thunk():
|
74 |
+
if capture_video and idx == 0:
|
75 |
+
env = gym.make(env_id, render_mode="rgb_array")
|
76 |
+
env = gym.wrappers.RecordVideo(env, f"videos/{run_name}")
|
77 |
+
else:
|
78 |
+
env = gym.make(env_id)
|
79 |
+
env = gym.wrappers.RecordEpisodeStatistics(env)
|
80 |
+
env.action_space.seed(seed)
|
81 |
+
return env
|
82 |
+
|
83 |
+
return thunk
|
84 |
+
|
85 |
+
|
86 |
+
# ALGO LOGIC: initialize agent here:
|
87 |
+
class QNetwork(nn.Module):
|
88 |
+
@nn.compact
|
89 |
+
def __call__(self, x: jnp.ndarray, a: jnp.ndarray):
|
90 |
+
x = jnp.concatenate([x, a], -1)
|
91 |
+
x = nn.Dense(256)(x)
|
92 |
+
x = nn.relu(x)
|
93 |
+
x = nn.Dense(256)(x)
|
94 |
+
x = nn.relu(x)
|
95 |
+
x = nn.Dense(1)(x)
|
96 |
+
return x
|
97 |
+
|
98 |
+
|
99 |
+
class Actor(nn.Module):
|
100 |
+
action_dim: int
|
101 |
+
action_scale: jnp.ndarray
|
102 |
+
action_bias: jnp.ndarray
|
103 |
+
|
104 |
+
@nn.compact
|
105 |
+
def __call__(self, x):
|
106 |
+
x = nn.Dense(256)(x)
|
107 |
+
x = nn.relu(x)
|
108 |
+
x = nn.Dense(256)(x)
|
109 |
+
x = nn.relu(x)
|
110 |
+
x = nn.Dense(self.action_dim)(x)
|
111 |
+
x = nn.tanh(x)
|
112 |
+
x = x * self.action_scale + self.action_bias
|
113 |
+
return x
|
114 |
+
|
115 |
+
|
116 |
+
class TrainState(TrainState):
|
117 |
+
target_params: flax.core.FrozenDict
|
118 |
+
|
119 |
+
|
120 |
+
if __name__ == "__main__":
|
121 |
+
import stable_baselines3 as sb3
|
122 |
+
|
123 |
+
if sb3.__version__ < "2.0":
|
124 |
+
raise ValueError(
|
125 |
+
"""Ongoing migration: run the following command to install the new dependencies:
|
126 |
+
poetry run pip install "stable_baselines3==2.0.0a1"
|
127 |
+
"""
|
128 |
+
)
|
129 |
+
args = parse_args()
|
130 |
+
run_name = f"{args.env_id}__{args.exp_name}__{args.seed}__{int(time.time())}"
|
131 |
+
if args.track:
|
132 |
+
import wandb
|
133 |
+
|
134 |
+
wandb.init(
|
135 |
+
project=args.wandb_project_name,
|
136 |
+
entity=args.wandb_entity,
|
137 |
+
sync_tensorboard=True,
|
138 |
+
config=vars(args),
|
139 |
+
name=run_name,
|
140 |
+
monitor_gym=True,
|
141 |
+
save_code=True,
|
142 |
+
)
|
143 |
+
writer = SummaryWriter(f"runs/{run_name}")
|
144 |
+
writer.add_text(
|
145 |
+
"hyperparameters",
|
146 |
+
"|param|value|\n|-|-|\n%s" % ("\n".join([f"|{key}|{value}|" for key, value in vars(args).items()])),
|
147 |
+
)
|
148 |
+
video_filenames = set()
|
149 |
+
|
150 |
+
# TRY NOT TO MODIFY: seeding
|
151 |
+
random.seed(args.seed)
|
152 |
+
np.random.seed(args.seed)
|
153 |
+
key = jax.random.PRNGKey(args.seed)
|
154 |
+
key, actor_key, qf1_key, qf2_key = jax.random.split(key, 4)
|
155 |
+
|
156 |
+
# env setup
|
157 |
+
envs = gym.vector.SyncVectorEnv([make_env(args.env_id, args.seed, 0, args.capture_video, run_name)])
|
158 |
+
assert isinstance(envs.single_action_space, gym.spaces.Box), "only continuous action space is supported"
|
159 |
+
|
160 |
+
max_action = float(envs.single_action_space.high[0])
|
161 |
+
envs.single_observation_space.dtype = np.float32
|
162 |
+
rb = ReplayBuffer(
|
163 |
+
args.buffer_size,
|
164 |
+
envs.single_observation_space,
|
165 |
+
envs.single_action_space,
|
166 |
+
device="cpu",
|
167 |
+
handle_timeout_termination=False,
|
168 |
+
)
|
169 |
+
|
170 |
+
# TRY NOT TO MODIFY: start the game
|
171 |
+
obs, _ = envs.reset(seed=args.seed)
|
172 |
+
|
173 |
+
actor = Actor(
|
174 |
+
action_dim=np.prod(envs.single_action_space.shape),
|
175 |
+
action_scale=jnp.array((envs.action_space.high - envs.action_space.low) / 2.0),
|
176 |
+
action_bias=jnp.array((envs.action_space.high + envs.action_space.low) / 2.0),
|
177 |
+
)
|
178 |
+
actor_state = TrainState.create(
|
179 |
+
apply_fn=actor.apply,
|
180 |
+
params=actor.init(actor_key, obs),
|
181 |
+
target_params=actor.init(actor_key, obs),
|
182 |
+
tx=optax.adam(learning_rate=args.learning_rate),
|
183 |
+
)
|
184 |
+
qf = QNetwork()
|
185 |
+
qf1_state = TrainState.create(
|
186 |
+
apply_fn=qf.apply,
|
187 |
+
params=qf.init(qf1_key, obs, envs.action_space.sample()),
|
188 |
+
target_params=qf.init(qf1_key, obs, envs.action_space.sample()),
|
189 |
+
tx=optax.adam(learning_rate=args.learning_rate),
|
190 |
+
)
|
191 |
+
qf2_state = TrainState.create(
|
192 |
+
apply_fn=qf.apply,
|
193 |
+
params=qf.init(qf2_key, obs, envs.action_space.sample()),
|
194 |
+
target_params=qf.init(qf2_key, obs, envs.action_space.sample()),
|
195 |
+
tx=optax.adam(learning_rate=args.learning_rate),
|
196 |
+
)
|
197 |
+
actor.apply = jax.jit(actor.apply)
|
198 |
+
qf.apply = jax.jit(qf.apply)
|
199 |
+
|
200 |
+
@jax.jit
|
201 |
+
def update_critic(
|
202 |
+
actor_state: TrainState,
|
203 |
+
qf1_state: TrainState,
|
204 |
+
qf2_state: TrainState,
|
205 |
+
observations: np.ndarray,
|
206 |
+
actions: np.ndarray,
|
207 |
+
next_observations: np.ndarray,
|
208 |
+
rewards: np.ndarray,
|
209 |
+
terminations: np.ndarray,
|
210 |
+
key: jnp.ndarray,
|
211 |
+
):
|
212 |
+
# TODO Maybe pre-generate a lot of random keys
|
213 |
+
# also check https://jax.readthedocs.io/en/latest/jax.random.html
|
214 |
+
key, noise_key = jax.random.split(key, 2)
|
215 |
+
clipped_noise = (
|
216 |
+
jnp.clip(
|
217 |
+
(jax.random.normal(noise_key, actions.shape) * args.policy_noise),
|
218 |
+
-args.noise_clip,
|
219 |
+
args.noise_clip,
|
220 |
+
)
|
221 |
+
* actor.action_scale
|
222 |
+
)
|
223 |
+
next_state_actions = jnp.clip(
|
224 |
+
actor.apply(actor_state.target_params, next_observations) + clipped_noise,
|
225 |
+
envs.single_action_space.low,
|
226 |
+
envs.single_action_space.high,
|
227 |
+
)
|
228 |
+
qf1_next_target = qf.apply(qf1_state.target_params, next_observations, next_state_actions).reshape(-1)
|
229 |
+
qf2_next_target = qf.apply(qf2_state.target_params, next_observations, next_state_actions).reshape(-1)
|
230 |
+
min_qf_next_target = jnp.minimum(qf1_next_target, qf2_next_target)
|
231 |
+
next_q_value = (rewards + (1 - terminations) * args.gamma * (min_qf_next_target)).reshape(-1)
|
232 |
+
|
233 |
+
def mse_loss(params):
|
234 |
+
qf_a_values = qf.apply(params, observations, actions).squeeze()
|
235 |
+
return ((qf_a_values - next_q_value) ** 2).mean(), qf_a_values.mean()
|
236 |
+
|
237 |
+
(qf1_loss_value, qf1_a_values), grads1 = jax.value_and_grad(mse_loss, has_aux=True)(qf1_state.params)
|
238 |
+
(qf2_loss_value, qf2_a_values), grads2 = jax.value_and_grad(mse_loss, has_aux=True)(qf2_state.params)
|
239 |
+
qf1_state = qf1_state.apply_gradients(grads=grads1)
|
240 |
+
qf2_state = qf2_state.apply_gradients(grads=grads2)
|
241 |
+
|
242 |
+
return (qf1_state, qf2_state), (qf1_loss_value, qf2_loss_value), (qf1_a_values, qf2_a_values), key
|
243 |
+
|
244 |
+
@jax.jit
|
245 |
+
def update_actor(
|
246 |
+
actor_state: TrainState,
|
247 |
+
qf1_state: TrainState,
|
248 |
+
qf2_state: TrainState,
|
249 |
+
observations: np.ndarray,
|
250 |
+
):
|
251 |
+
def actor_loss(params):
|
252 |
+
return -qf.apply(qf1_state.params, observations, actor.apply(params, observations)).mean()
|
253 |
+
|
254 |
+
actor_loss_value, grads = jax.value_and_grad(actor_loss)(actor_state.params)
|
255 |
+
actor_state = actor_state.apply_gradients(grads=grads)
|
256 |
+
actor_state = actor_state.replace(
|
257 |
+
target_params=optax.incremental_update(actor_state.params, actor_state.target_params, args.tau)
|
258 |
+
)
|
259 |
+
|
260 |
+
qf1_state = qf1_state.replace(
|
261 |
+
target_params=optax.incremental_update(qf1_state.params, qf1_state.target_params, args.tau)
|
262 |
+
)
|
263 |
+
qf2_state = qf2_state.replace(
|
264 |
+
target_params=optax.incremental_update(qf2_state.params, qf2_state.target_params, args.tau)
|
265 |
+
)
|
266 |
+
return actor_state, (qf1_state, qf2_state), actor_loss_value
|
267 |
+
|
268 |
+
start_time = time.time()
|
269 |
+
for global_step in range(args.total_timesteps):
|
270 |
+
# ALGO LOGIC: put action logic here
|
271 |
+
if global_step < args.learning_starts:
|
272 |
+
actions = np.array([envs.single_action_space.sample() for _ in range(envs.num_envs)])
|
273 |
+
else:
|
274 |
+
actions = actor.apply(actor_state.params, obs)
|
275 |
+
actions = np.array(
|
276 |
+
[
|
277 |
+
(
|
278 |
+
jax.device_get(actions)[0]
|
279 |
+
+ np.random.normal(0, max_action * args.exploration_noise, size=envs.single_action_space.shape)
|
280 |
+
).clip(envs.single_action_space.low, envs.single_action_space.high)
|
281 |
+
]
|
282 |
+
)
|
283 |
+
|
284 |
+
# TRY NOT TO MODIFY: execute the game and log data.
|
285 |
+
next_obs, rewards, terminations, truncations, infos = envs.step(actions)
|
286 |
+
|
287 |
+
# TRY NOT TO MODIFY: record rewards for plotting purposes
|
288 |
+
if "final_info" in infos:
|
289 |
+
for info in infos["final_info"]:
|
290 |
+
print(f"global_step={global_step}, episodic_return={info['episode']['r']}")
|
291 |
+
writer.add_scalar("charts/episodic_return", info["episode"]["r"], global_step)
|
292 |
+
writer.add_scalar("charts/episodic_length", info["episode"]["l"], global_step)
|
293 |
+
break
|
294 |
+
|
295 |
+
# TRY NOT TO MODIFY: save data to replay buffer; handle `terminal_observation`
|
296 |
+
real_next_obs = next_obs.copy()
|
297 |
+
for idx, trunc in enumerate(truncations):
|
298 |
+
if trunc:
|
299 |
+
real_next_obs[idx] = infos["final_observation"][idx]
|
300 |
+
rb.add(obs, real_next_obs, actions, rewards, terminations, infos)
|
301 |
+
|
302 |
+
# TRY NOT TO MODIFY: CRUCIAL step easy to overlook
|
303 |
+
obs = next_obs
|
304 |
+
|
305 |
+
# ALGO LOGIC: training.
|
306 |
+
if global_step > args.learning_starts:
|
307 |
+
data = rb.sample(args.batch_size)
|
308 |
+
|
309 |
+
(qf1_state, qf2_state), (qf1_loss_value, qf2_loss_value), (qf1_a_values, qf2_a_values), key = update_critic(
|
310 |
+
actor_state,
|
311 |
+
qf1_state,
|
312 |
+
qf2_state,
|
313 |
+
data.observations.numpy(),
|
314 |
+
data.actions.numpy(),
|
315 |
+
data.next_observations.numpy(),
|
316 |
+
data.rewards.flatten().numpy(),
|
317 |
+
data.dones.flatten().numpy(),
|
318 |
+
key,
|
319 |
+
)
|
320 |
+
|
321 |
+
if global_step % args.policy_frequency == 0:
|
322 |
+
actor_state, (qf1_state, qf2_state), actor_loss_value = update_actor(
|
323 |
+
actor_state,
|
324 |
+
qf1_state,
|
325 |
+
qf2_state,
|
326 |
+
data.observations.numpy(),
|
327 |
+
)
|
328 |
+
|
329 |
+
if global_step % 100 == 0:
|
330 |
+
writer.add_scalar("losses/qf1_loss", qf1_loss_value.item(), global_step)
|
331 |
+
writer.add_scalar("losses/qf2_loss", qf2_loss_value.item(), global_step)
|
332 |
+
writer.add_scalar("losses/qf1_values", qf1_a_values.item(), global_step)
|
333 |
+
writer.add_scalar("losses/qf2_values", qf2_a_values.item(), global_step)
|
334 |
+
writer.add_scalar("losses/actor_loss", actor_loss_value.item(), global_step)
|
335 |
+
print("SPS:", int(global_step / (time.time() - start_time)))
|
336 |
+
writer.add_scalar("charts/SPS", int(global_step / (time.time() - start_time)), global_step)
|
337 |
+
|
338 |
+
if args.save_model:
|
339 |
+
model_path = f"runs/{run_name}/{args.exp_name}.cleanrl_model"
|
340 |
+
with open(model_path, "wb") as f:
|
341 |
+
f.write(
|
342 |
+
flax.serialization.to_bytes(
|
343 |
+
[
|
344 |
+
actor_state.params,
|
345 |
+
qf1_state.params,
|
346 |
+
qf2_state.params,
|
347 |
+
]
|
348 |
+
)
|
349 |
+
)
|
350 |
+
print(f"model saved to {model_path}")
|
351 |
+
from cleanrl_utils.evals.td3_jax_eval import evaluate
|
352 |
+
|
353 |
+
episodic_returns = evaluate(
|
354 |
+
model_path,
|
355 |
+
make_env,
|
356 |
+
args.env_id,
|
357 |
+
eval_episodes=10,
|
358 |
+
run_name=f"{run_name}-eval",
|
359 |
+
Model=(Actor, QNetwork),
|
360 |
+
exploration_noise=args.exploration_noise,
|
361 |
+
)
|
362 |
+
for idx, episodic_return in enumerate(episodic_returns):
|
363 |
+
writer.add_scalar("eval/episodic_return", episodic_return, idx)
|
364 |
+
|
365 |
+
if args.upload_model:
|
366 |
+
from cleanrl_utils.huggingface import push_to_hub
|
367 |
+
|
368 |
+
repo_name = f"{args.env_id}-{args.exp_name}-seed{args.seed}"
|
369 |
+
repo_id = f"{args.hf_entity}/{repo_name}" if args.hf_entity else repo_name
|
370 |
+
push_to_hub(args, episodic_returns, repo_id, "TD3", f"runs/{run_name}", f"videos/{run_name}-eval")
|
371 |
+
|
372 |
+
envs.close()
|
373 |
+
writer.close()
|
videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-0.mp4
ADDED
Binary file (15.6 kB). View file
|
|
videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-1.mp4
ADDED
Binary file (14.6 kB). View file
|
|
videos/Hopper-v4__td3_continuous_action_jax__1__1697013370-eval/rl-video-episode-8.mp4
ADDED
Binary file (18.1 kB). View file
|
|