pushing model
Browse files- .gitattributes +1 -0
- README.md +80 -0
- dqn_atari.cleanrl_model +3 -0
- dqn_atari.py +289 -0
- events.out.tfevents.1702690709.4090-171.1321592.0 +3 -0
- poetry.lock +0 -0
- pyproject.toml +108 -0
- replay.mp4 +0 -0
- videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-0.mp4 +0 -0
- videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-1.mp4 +0 -0
- videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-8.mp4 +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
dqn_atari.cleanrl_model filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- ALE/Qbert-v5
|
| 4 |
+
- deep-reinforcement-learning
|
| 5 |
+
- reinforcement-learning
|
| 6 |
+
- custom-implementation
|
| 7 |
+
library_name: cleanrl
|
| 8 |
+
model-index:
|
| 9 |
+
- name: DQN
|
| 10 |
+
results:
|
| 11 |
+
- task:
|
| 12 |
+
type: reinforcement-learning
|
| 13 |
+
name: reinforcement-learning
|
| 14 |
+
dataset:
|
| 15 |
+
name: ALE/Qbert-v5
|
| 16 |
+
type: ALE/Qbert-v5
|
| 17 |
+
metrics:
|
| 18 |
+
- type: mean_reward
|
| 19 |
+
value: 9930.00 +/- 2476.26
|
| 20 |
+
name: mean_reward
|
| 21 |
+
verified: false
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# (CleanRL) **DQN** Agent Playing **ALE/Qbert-v5**
|
| 25 |
+
|
| 26 |
+
This is a trained model of a DQN agent playing ALE/Qbert-v5.
|
| 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/dqn_atari.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[dqn_atari]"
|
| 36 |
+
python -m cleanrl_utils.enjoy --exp-name dqn_atari --env-id ALE/Qbert-v5
|
| 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/Qbert-v5-dqn_atari-seed1/raw/main/dqn_atari.py
|
| 46 |
+
curl -OL https://huggingface.co/sdpkjc/Qbert-v5-dqn_atari-seed1/raw/main/pyproject.toml
|
| 47 |
+
curl -OL https://huggingface.co/sdpkjc/Qbert-v5-dqn_atari-seed1/raw/main/poetry.lock
|
| 48 |
+
poetry install --all-extras
|
| 49 |
+
python dqn_atari.py --save-model --upload-model --hf-entity sdpkjc --env-id ALE/Qbert-v5 --track
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
# Hyperparameters
|
| 53 |
+
```python
|
| 54 |
+
{'batch_size': 32,
|
| 55 |
+
'buffer_size': 1000000,
|
| 56 |
+
'capture_video': False,
|
| 57 |
+
'cuda': True,
|
| 58 |
+
'end_e': 0.01,
|
| 59 |
+
'env_id': 'ALE/Qbert-v5',
|
| 60 |
+
'exp_name': 'dqn_atari',
|
| 61 |
+
'exploration_fraction': 0.1,
|
| 62 |
+
'gamma': 0.99,
|
| 63 |
+
'hf_entity': 'sdpkjc',
|
| 64 |
+
'learning_rate': 0.0001,
|
| 65 |
+
'learning_starts': 80000,
|
| 66 |
+
'num_envs': 1,
|
| 67 |
+
'save_model': True,
|
| 68 |
+
'seed': 1,
|
| 69 |
+
'start_e': 1,
|
| 70 |
+
'target_network_frequency': 1000,
|
| 71 |
+
'tau': 1.0,
|
| 72 |
+
'torch_deterministic': True,
|
| 73 |
+
'total_timesteps': 10000000,
|
| 74 |
+
'track': True,
|
| 75 |
+
'train_frequency': 4,
|
| 76 |
+
'upload_model': True,
|
| 77 |
+
'wandb_entity': None,
|
| 78 |
+
'wandb_project_name': 'cleanRL'}
|
| 79 |
+
```
|
| 80 |
+
|
dqn_atari.cleanrl_model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d68be8f6fdee551505db6b680cc132d4b82ced32c6b451bf94e055cfe257cf97
|
| 3 |
+
size 6752415
|
dqn_atari.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# docs and experiment results can be found at https://docs.cleanrl.dev/rl-algorithms/dqn/#dqn_ataripy
|
| 2 |
+
import argparse
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import time
|
| 6 |
+
from distutils.util import strtobool
|
| 7 |
+
|
| 8 |
+
import gymnasium as gym
|
| 9 |
+
import numpy as np
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
import torch.nn.functional as F
|
| 13 |
+
import torch.optim as optim
|
| 14 |
+
from stable_baselines3.common.atari_wrappers import (
|
| 15 |
+
ClipRewardEnv,
|
| 16 |
+
EpisodicLifeEnv,
|
| 17 |
+
FireResetEnv,
|
| 18 |
+
MaxAndSkipEnv,
|
| 19 |
+
NoopResetEnv,
|
| 20 |
+
)
|
| 21 |
+
from stable_baselines3.common.buffers import ReplayBuffer
|
| 22 |
+
from torch.utils.tensorboard import SummaryWriter
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def parse_args():
|
| 26 |
+
# fmt: off
|
| 27 |
+
parser = argparse.ArgumentParser()
|
| 28 |
+
parser.add_argument("--exp-name", type=str, default=os.path.basename(__file__).rstrip(".py"),
|
| 29 |
+
help="the name of this experiment")
|
| 30 |
+
parser.add_argument("--seed", type=int, default=1,
|
| 31 |
+
help="seed of the experiment")
|
| 32 |
+
parser.add_argument("--torch-deterministic", type=lambda x: bool(strtobool(x)), default=True, nargs="?", const=True,
|
| 33 |
+
help="if toggled, `torch.backends.cudnn.deterministic=False`")
|
| 34 |
+
parser.add_argument("--cuda", type=lambda x: bool(strtobool(x)), default=True, nargs="?", const=True,
|
| 35 |
+
help="if toggled, cuda will be enabled by default")
|
| 36 |
+
parser.add_argument("--track", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
| 37 |
+
help="if toggled, this experiment will be tracked with Weights and Biases")
|
| 38 |
+
parser.add_argument("--wandb-project-name", type=str, default="cleanRL",
|
| 39 |
+
help="the wandb's project name")
|
| 40 |
+
parser.add_argument("--wandb-entity", type=str, default=None,
|
| 41 |
+
help="the entity (team) of wandb's project")
|
| 42 |
+
parser.add_argument("--capture-video", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
| 43 |
+
help="whether to capture videos of the agent performances (check out `videos` folder)")
|
| 44 |
+
parser.add_argument("--save-model", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
| 45 |
+
help="whether to save model into the `runs/{run_name}` folder")
|
| 46 |
+
parser.add_argument("--upload-model", type=lambda x: bool(strtobool(x)), default=False, nargs="?", const=True,
|
| 47 |
+
help="whether to upload the saved model to huggingface")
|
| 48 |
+
parser.add_argument("--hf-entity", type=str, default="",
|
| 49 |
+
help="the user or org name of the model repository from the Hugging Face Hub")
|
| 50 |
+
|
| 51 |
+
# Algorithm specific arguments
|
| 52 |
+
parser.add_argument("--env-id", type=str, default="BreakoutNoFrameskip-v4",
|
| 53 |
+
help="the id of the environment")
|
| 54 |
+
parser.add_argument("--total-timesteps", type=int, default=10000000,
|
| 55 |
+
help="total timesteps of the experiments")
|
| 56 |
+
parser.add_argument("--learning-rate", type=float, default=1e-4,
|
| 57 |
+
help="the learning rate of the optimizer")
|
| 58 |
+
parser.add_argument("--num-envs", type=int, default=1,
|
| 59 |
+
help="the number of parallel game environments")
|
| 60 |
+
parser.add_argument("--buffer-size", type=int, default=1000000,
|
| 61 |
+
help="the replay memory buffer size")
|
| 62 |
+
parser.add_argument("--gamma", type=float, default=0.99,
|
| 63 |
+
help="the discount factor gamma")
|
| 64 |
+
parser.add_argument("--tau", type=float, default=1.,
|
| 65 |
+
help="the target network update rate")
|
| 66 |
+
parser.add_argument("--target-network-frequency", type=int, default=1000,
|
| 67 |
+
help="the timesteps it takes to update the target network")
|
| 68 |
+
parser.add_argument("--batch-size", type=int, default=32,
|
| 69 |
+
help="the batch size of sample from the reply memory")
|
| 70 |
+
parser.add_argument("--start-e", type=float, default=1,
|
| 71 |
+
help="the starting epsilon for exploration")
|
| 72 |
+
parser.add_argument("--end-e", type=float, default=0.01,
|
| 73 |
+
help="the ending epsilon for exploration")
|
| 74 |
+
parser.add_argument("--exploration-fraction", type=float, default=0.10,
|
| 75 |
+
help="the fraction of `total-timesteps` it takes from start-e to go end-e")
|
| 76 |
+
parser.add_argument("--learning-starts", type=int, default=80000,
|
| 77 |
+
help="timestep to start learning")
|
| 78 |
+
parser.add_argument("--train-frequency", type=int, default=4,
|
| 79 |
+
help="the frequency of training")
|
| 80 |
+
args = parser.parse_args()
|
| 81 |
+
# fmt: on
|
| 82 |
+
assert args.num_envs == 1, "vectorized envs are not supported at the moment"
|
| 83 |
+
|
| 84 |
+
return args
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def make_env(env_id, seed, idx, capture_video, run_name):
|
| 88 |
+
def thunk():
|
| 89 |
+
# Machado et al. 2017 (Revisitng ALE: Eval protocols)
|
| 90 |
+
if capture_video and idx == 0:
|
| 91 |
+
env = gym.make(env_id, render_mode="rgb_array")
|
| 92 |
+
env = gym.wrappers.RecordVideo(env, f"videos/{run_name}")
|
| 93 |
+
else:
|
| 94 |
+
env = gym.make(env_id)
|
| 95 |
+
env = gym.wrappers.TimeLimit(env, max_episode_steps=108000 // 4)
|
| 96 |
+
env = gym.wrappers.RecordEpisodeStatistics(env)
|
| 97 |
+
env = NoopResetEnv(env, noop_max=1)
|
| 98 |
+
# env = MaxAndSkipEnv(env, skip=4)
|
| 99 |
+
# env = EpisodicLifeEnv(env)
|
| 100 |
+
if "FIRE" in env.unwrapped.get_action_meanings():
|
| 101 |
+
env = FireResetEnv(env)
|
| 102 |
+
env = ClipRewardEnv(env)
|
| 103 |
+
env = gym.wrappers.ResizeObservation(env, (84, 84))
|
| 104 |
+
env = gym.wrappers.GrayScaleObservation(env)
|
| 105 |
+
env = gym.wrappers.FrameStack(env, 4)
|
| 106 |
+
env.action_space.seed(seed)
|
| 107 |
+
env.observation_space.seed(seed)
|
| 108 |
+
return env
|
| 109 |
+
|
| 110 |
+
return thunk
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ALGO LOGIC: initialize agent here:
|
| 114 |
+
class QNetwork(nn.Module):
|
| 115 |
+
def __init__(self, env):
|
| 116 |
+
super().__init__()
|
| 117 |
+
self.network = nn.Sequential(
|
| 118 |
+
nn.Conv2d(4, 32, 8, stride=4),
|
| 119 |
+
nn.ReLU(),
|
| 120 |
+
nn.Conv2d(32, 64, 4, stride=2),
|
| 121 |
+
nn.ReLU(),
|
| 122 |
+
nn.Conv2d(64, 64, 3, stride=1),
|
| 123 |
+
nn.ReLU(),
|
| 124 |
+
nn.Flatten(),
|
| 125 |
+
nn.Linear(3136, 512),
|
| 126 |
+
nn.ReLU(),
|
| 127 |
+
nn.Linear(512, env.single_action_space.n),
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
def forward(self, x):
|
| 131 |
+
return self.network(x / 255.0)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def linear_schedule(start_e: float, end_e: float, duration: int, t: int):
|
| 135 |
+
slope = (end_e - start_e) / duration
|
| 136 |
+
return max(slope * t + start_e, end_e)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
import stable_baselines3 as sb3
|
| 141 |
+
|
| 142 |
+
if sb3.__version__ < "2.0":
|
| 143 |
+
raise ValueError(
|
| 144 |
+
"""Ongoing migration: run the following command to install the new dependencies:
|
| 145 |
+
|
| 146 |
+
poetry run pip install "stable_baselines3==2.0.0a1" "gymnasium[atari,accept-rom-license]==0.28.1" "ale-py==0.8.1"
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
+
args = parse_args()
|
| 150 |
+
run_name = f"{args.env_id}__{args.exp_name}__{args.seed}__{int(time.time())}"
|
| 151 |
+
if args.track:
|
| 152 |
+
import wandb
|
| 153 |
+
|
| 154 |
+
wandb.init(
|
| 155 |
+
project=args.wandb_project_name,
|
| 156 |
+
entity=args.wandb_entity,
|
| 157 |
+
sync_tensorboard=True,
|
| 158 |
+
config=vars(args),
|
| 159 |
+
name=run_name,
|
| 160 |
+
monitor_gym=True,
|
| 161 |
+
save_code=True,
|
| 162 |
+
)
|
| 163 |
+
writer = SummaryWriter(f"runs/{run_name}")
|
| 164 |
+
writer.add_text(
|
| 165 |
+
"hyperparameters",
|
| 166 |
+
"|param|value|\n|-|-|\n%s" % ("\n".join([f"|{key}|{value}|" for key, value in vars(args).items()])),
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# TRY NOT TO MODIFY: seeding
|
| 170 |
+
random.seed(args.seed)
|
| 171 |
+
np.random.seed(args.seed)
|
| 172 |
+
torch.manual_seed(args.seed)
|
| 173 |
+
torch.backends.cudnn.deterministic = args.torch_deterministic
|
| 174 |
+
|
| 175 |
+
device = torch.device("cuda" if torch.cuda.is_available() and args.cuda else "cpu")
|
| 176 |
+
|
| 177 |
+
# env setup
|
| 178 |
+
envs = gym.vector.SyncVectorEnv(
|
| 179 |
+
[make_env(args.env_id, args.seed + i, i, args.capture_video, run_name) for i in range(args.num_envs)]
|
| 180 |
+
)
|
| 181 |
+
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"
|
| 182 |
+
|
| 183 |
+
q_network = QNetwork(envs).to(device)
|
| 184 |
+
optimizer = optim.Adam(q_network.parameters(), lr=args.learning_rate)
|
| 185 |
+
target_network = QNetwork(envs).to(device)
|
| 186 |
+
target_network.load_state_dict(q_network.state_dict())
|
| 187 |
+
|
| 188 |
+
rb = ReplayBuffer(
|
| 189 |
+
args.buffer_size,
|
| 190 |
+
envs.single_observation_space,
|
| 191 |
+
envs.single_action_space,
|
| 192 |
+
device,
|
| 193 |
+
optimize_memory_usage=True,
|
| 194 |
+
handle_timeout_termination=False,
|
| 195 |
+
)
|
| 196 |
+
start_time = time.time()
|
| 197 |
+
|
| 198 |
+
# TRY NOT TO MODIFY: start the game
|
| 199 |
+
obs, _ = envs.reset(seed=args.seed)
|
| 200 |
+
for global_step in range(args.total_timesteps):
|
| 201 |
+
# ALGO LOGIC: put action logic here
|
| 202 |
+
epsilon = linear_schedule(args.start_e, args.end_e, args.exploration_fraction * args.total_timesteps, global_step)
|
| 203 |
+
if random.random() < epsilon:
|
| 204 |
+
actions = np.array([envs.single_action_space.sample() for _ in range(envs.num_envs)])
|
| 205 |
+
else:
|
| 206 |
+
q_values = q_network(torch.Tensor(obs).to(device))
|
| 207 |
+
actions = torch.argmax(q_values, dim=1).cpu().numpy()
|
| 208 |
+
|
| 209 |
+
# TRY NOT TO MODIFY: execute the game and log data.
|
| 210 |
+
next_obs, rewards, terminations, truncations, infos = envs.step(actions)
|
| 211 |
+
|
| 212 |
+
# TRY NOT TO MODIFY: record rewards for plotting purposes
|
| 213 |
+
if "final_info" in infos:
|
| 214 |
+
for info in infos["final_info"]:
|
| 215 |
+
# Skip the envs that are not done
|
| 216 |
+
if "episode" not in info:
|
| 217 |
+
continue
|
| 218 |
+
print(f"global_step={global_step}, episodic_return={info['episode']['r']}")
|
| 219 |
+
writer.add_scalar("charts/episodic_return", info["episode"]["r"], global_step)
|
| 220 |
+
writer.add_scalar("charts/episodic_length", info["episode"]["l"], global_step)
|
| 221 |
+
writer.add_scalar("charts/epsilon", epsilon, global_step)
|
| 222 |
+
break
|
| 223 |
+
|
| 224 |
+
# TRY NOT TO MODIFY: save data to reply buffer; handle `final_observation`
|
| 225 |
+
real_next_obs = next_obs.copy()
|
| 226 |
+
for idx, trunc in enumerate(truncations):
|
| 227 |
+
if trunc:
|
| 228 |
+
real_next_obs[idx] = infos["final_observation"][idx]
|
| 229 |
+
rb.add(obs, real_next_obs, actions, rewards, terminations, infos)
|
| 230 |
+
|
| 231 |
+
# TRY NOT TO MODIFY: CRUCIAL step easy to overlook
|
| 232 |
+
obs = next_obs
|
| 233 |
+
|
| 234 |
+
# ALGO LOGIC: training.
|
| 235 |
+
if global_step > args.learning_starts:
|
| 236 |
+
if global_step % args.train_frequency == 0:
|
| 237 |
+
data = rb.sample(args.batch_size)
|
| 238 |
+
with torch.no_grad():
|
| 239 |
+
target_max, _ = target_network(data.next_observations).max(dim=1)
|
| 240 |
+
td_target = data.rewards.flatten() + args.gamma * target_max * (1 - data.dones.flatten())
|
| 241 |
+
old_val = q_network(data.observations).gather(1, data.actions).squeeze()
|
| 242 |
+
loss = F.mse_loss(td_target, old_val)
|
| 243 |
+
|
| 244 |
+
if global_step % 100 == 0:
|
| 245 |
+
writer.add_scalar("losses/td_loss", loss, global_step)
|
| 246 |
+
writer.add_scalar("losses/q_values", old_val.mean().item(), global_step)
|
| 247 |
+
print("SPS:", int(global_step / (time.time() - start_time)))
|
| 248 |
+
writer.add_scalar("charts/SPS", int(global_step / (time.time() - start_time)), global_step)
|
| 249 |
+
|
| 250 |
+
# optimize the model
|
| 251 |
+
optimizer.zero_grad()
|
| 252 |
+
loss.backward()
|
| 253 |
+
optimizer.step()
|
| 254 |
+
|
| 255 |
+
# update target network
|
| 256 |
+
if global_step % args.target_network_frequency == 0:
|
| 257 |
+
for target_network_param, q_network_param in zip(target_network.parameters(), q_network.parameters()):
|
| 258 |
+
target_network_param.data.copy_(
|
| 259 |
+
args.tau * q_network_param.data + (1.0 - args.tau) * target_network_param.data
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
if args.save_model:
|
| 263 |
+
model_path = f"runs/{run_name}/{args.exp_name}.cleanrl_model"
|
| 264 |
+
torch.save(q_network.state_dict(), model_path)
|
| 265 |
+
print(f"model saved to {model_path}")
|
| 266 |
+
from cleanrl_utils.evals.dqn_eval import evaluate
|
| 267 |
+
|
| 268 |
+
episodic_returns = evaluate(
|
| 269 |
+
model_path,
|
| 270 |
+
make_env,
|
| 271 |
+
args.env_id,
|
| 272 |
+
eval_episodes=10,
|
| 273 |
+
run_name=f"{run_name}-eval",
|
| 274 |
+
Model=QNetwork,
|
| 275 |
+
device=device,
|
| 276 |
+
epsilon=0.05,
|
| 277 |
+
)
|
| 278 |
+
for idx, episodic_return in enumerate(episodic_returns):
|
| 279 |
+
writer.add_scalar("eval/episodic_return", episodic_return, idx)
|
| 280 |
+
|
| 281 |
+
if args.upload_model:
|
| 282 |
+
from cleanrl_utils.huggingface import push_to_hub
|
| 283 |
+
env_id = str(args.env_id).split("/")[1]
|
| 284 |
+
repo_name = f"{env_id}-{args.exp_name}-seed{args.seed}"
|
| 285 |
+
repo_id = f"{args.hf_entity}/{repo_name}" if args.hf_entity else repo_name
|
| 286 |
+
push_to_hub(args, episodic_returns, repo_id, "DQN", f"runs/{run_name}", f"videos/{run_name}-eval")
|
| 287 |
+
|
| 288 |
+
envs.close()
|
| 289 |
+
writer.close()
|
events.out.tfevents.1702690709.4090-171.1321592.0
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ba0dddc87bf9930c7daa5483371f2af5c8451e910a2f4b77233852d38c7269d
|
| 3 |
+
size 18513724
|
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 <[email protected]>"]
|
| 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 (202 kB). View file
|
|
|
videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-0.mp4
ADDED
|
Binary file (265 kB). View file
|
|
|
videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-1.mp4
ADDED
|
Binary file (256 kB). View file
|
|
|
videos/ALE/Qbert-v5__dqn_atari__1__1702690701-eval/rl-video-episode-8.mp4
ADDED
|
Binary file (202 kB). View file
|
|
|