forked from DLR-RM/rl-baselines3-zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·257 lines (239 loc) · 10.2 KB
/
train.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
import argparse
import difflib
import importlib
import os
import time
import uuid
import gym
import numpy as np
import seaborn
import torch as th
from stable_baselines3.common.utils import set_random_seed
# Register custom envs
import utils.import_envs # noqa: F401 pytype: disable=import-error
from utils.exp_manager import ExperimentManager
from utils.utils import ALGOS, StoreDict
seaborn.set()
if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser()
parser.add_argument("--algo", help="RL Algorithm", default="ppo", type=str, required=False, choices=list(ALGOS.keys()))
parser.add_argument("--env", type=str, default="CartPole-v1", help="environment ID")
parser.add_argument("-tb", "--tensorboard-log", help="Tensorboard log dir", default="", type=str)
parser.add_argument("-i", "--trained-agent", help="Path to a pretrained agent to continue training", default="", type=str)
parser.add_argument(
"--truncate-last-trajectory",
help="When using HER with online sampling the last trajectory "
"in the replay buffer will be truncated after reloading the replay buffer.",
default=True,
type=bool,
)
parser.add_argument("-n", "--n-timesteps", help="Overwrite the number of timesteps", default=-1, type=int)
parser.add_argument("--num-threads", help="Number of threads for PyTorch (-1 to use default)", default=-1, type=int)
parser.add_argument("--log-interval", help="Override log interval (default: -1, no change)", default=-1, type=int)
parser.add_argument(
"--eval-freq",
help="Evaluate the agent every n steps (if negative, no evaluation). "
"During hyperparameter optimization n-evaluations is used instead",
default=25000,
type=int,
)
parser.add_argument(
"--optimization-log-path",
help="Path to save the evaluation log and optimal policy for each hyperparameter tried during optimization. "
"Disabled if no argument is passed.",
type=str,
)
parser.add_argument("--eval-episodes", help="Number of episodes to use for evaluation", default=5, type=int)
parser.add_argument("--n-eval-envs", help="Number of environments for evaluation", default=1, type=int)
parser.add_argument("--save-freq", help="Save the model every n steps (if negative, no checkpoint)", default=-1, type=int)
parser.add_argument(
"--save-replay-buffer", help="Save the replay buffer too (when applicable)", action="store_true", default=False
)
parser.add_argument("-f", "--log-folder", help="Log folder", type=str, default="logs")
parser.add_argument("--seed", help="Random generator seed", type=int, default=-1)
parser.add_argument("--vec-env", help="VecEnv type", type=str, default="dummy", choices=["dummy", "subproc"])
parser.add_argument("--device", help="PyTorch device to be use (ex: cpu, cuda...)", default="auto", type=str)
parser.add_argument(
"--n-trials",
help="Number of trials for optimizing hyperparameters. "
"This applies to each optimization runner, not the entire optimization process.",
type=int,
default=500,
)
parser.add_argument(
"--max-total-trials",
help="Number of (potentially pruned) trials for optimizing hyperparameters. "
"This applies to the entire optimization process and takes precedence over --n-trials if set.",
type=int,
default=None,
)
parser.add_argument(
"-optimize", "--optimize-hyperparameters", action="store_true", default=False, help="Run hyperparameters search"
)
parser.add_argument(
"--no-optim-plots", action="store_true", default=False, help="Disable hyperparameter optimization plots"
)
parser.add_argument("--n-jobs", help="Number of parallel jobs when optimizing hyperparameters", type=int, default=1)
parser.add_argument(
"--sampler",
help="Sampler to use when optimizing hyperparameters",
type=str,
default="tpe",
choices=["random", "tpe", "skopt"],
)
parser.add_argument(
"--pruner",
help="Pruner to use when optimizing hyperparameters",
type=str,
default="median",
choices=["halving", "median", "none"],
)
parser.add_argument("--n-startup-trials", help="Number of trials before using optuna sampler", type=int, default=10)
parser.add_argument(
"--n-evaluations",
help="Training policies are evaluated every n-timesteps // n-evaluations steps when doing hyperparameter optimization."
"Default is 1 evaluation per 100k timesteps.",
type=int,
default=None,
)
parser.add_argument(
"--storage", help="Database storage path if distributed optimization should be used", type=str, default=None
)
parser.add_argument("--study-name", help="Study name for distributed optimization", type=str, default=None)
parser.add_argument("--verbose", help="Verbose mode (0: no output, 1: INFO)", default=1, type=int)
parser.add_argument(
"--gym-packages",
type=str,
nargs="+",
default=[],
help="Additional external Gym environment package modules to import (e.g. gym_minigrid)",
)
parser.add_argument(
"--env-kwargs", type=str, nargs="+", action=StoreDict, help="Optional keyword argument to pass to the env constructor"
)
parser.add_argument(
"-params",
"--hyperparams",
type=str,
nargs="+",
action=StoreDict,
help="Overwrite hyperparameter (e.g. learning_rate:0.01 train_freq:10)",
)
parser.add_argument(
"-yaml", "--yaml-file", type=str, default=None, help="Custom yaml file from which the hyperparameters will be loaded"
)
parser.add_argument("-uuid", "--uuid", action="store_true", default=False, help="Ensure that the run has a unique ID")
parser.add_argument(
"--track",
action="store_true",
default=False,
help="if toggled, this experiment will be tracked with Weights and Biases",
)
parser.add_argument("--wandb-project-name", type=str, default="sb3", help="the wandb's project name")
parser.add_argument("--wandb-entity", type=str, default=None, help="the entity (team) of wandb's project")
parser.add_argument(
"-P",
"--progress",
action="store_true",
default=False,
help="if toggled, display a progress bar using tqdm and rich",
)
args = parser.parse_args()
# Going through custom gym packages to let them register in the global registory
for env_module in args.gym_packages:
importlib.import_module(env_module)
env_id = args.env
registered_envs = set(gym.envs.registry.env_specs.keys()) # pytype: disable=module-attr
# If the environment is not found, suggest the closest match
if env_id not in registered_envs:
try:
closest_match = difflib.get_close_matches(env_id, registered_envs, n=1)[0]
except IndexError:
closest_match = "'no close match found...'"
raise ValueError(f"{env_id} not found in gym registry, you maybe meant {closest_match}?")
# Unique id to ensure there is no race condition for the folder creation
uuid_str = f"_{uuid.uuid4()}" if args.uuid else ""
if args.seed < 0:
# Seed but with a random one
args.seed = np.random.randint(2**32 - 1, dtype="int64").item()
set_random_seed(args.seed)
# Setting num threads to 1 makes things run faster on cpu
if args.num_threads > 0:
if args.verbose > 1:
print(f"Setting torch.num_threads to {args.num_threads}")
th.set_num_threads(args.num_threads)
if args.trained_agent != "":
assert args.trained_agent.endswith(".zip") and os.path.isfile(
args.trained_agent
), "The trained_agent must be a valid path to a .zip file"
print("=" * 10, env_id, "=" * 10)
print(f"Seed: {args.seed}")
if args.track:
try:
import wandb
except ImportError:
raise ImportError(
"if you want to use Weights & Biases to track experiment, please install W&B via `pip install wandb`"
)
run_name = f"{args.env}__{args.algo}__{args.seed}__{int(time.time())}"
run = wandb.init(
name=run_name,
project=args.wandb_project_name,
entity=args.wandb_entity,
config=vars(args),
sync_tensorboard=True, # auto-upload sb3's tensorboard metrics
monitor_gym=True, # auto-upload the videos of agents playing the game
save_code=True, # optional
)
args.tensorboard_log = f"runs/{run_name}"
exp_manager = ExperimentManager(
args,
args.algo,
env_id,
args.log_folder,
args.tensorboard_log,
args.n_timesteps,
args.eval_freq,
args.eval_episodes,
args.save_freq,
args.hyperparams,
args.env_kwargs,
args.trained_agent,
args.optimize_hyperparameters,
args.storage,
args.study_name,
args.n_trials,
args.max_total_trials,
args.n_jobs,
args.sampler,
args.pruner,
args.optimization_log_path,
n_startup_trials=args.n_startup_trials,
n_evaluations=args.n_evaluations,
truncate_last_trajectory=args.truncate_last_trajectory,
uuid_str=uuid_str,
seed=args.seed,
log_interval=args.log_interval,
save_replay_buffer=args.save_replay_buffer,
verbose=args.verbose,
vec_env_type=args.vec_env,
n_eval_envs=args.n_eval_envs,
no_optim_plots=args.no_optim_plots,
device=args.device,
yaml_file=args.yaml_file,
show_progress=args.progress,
)
# Prepare experiment and launch hyperparameter optimization if needed
results = exp_manager.setup_experiment()
if results is not None:
model, saved_hyperparams = results
if args.track:
# we need to save the loaded hyperparameters
args.saved_hyperparams = saved_hyperparams
run.config.setdefaults(vars(args))
# Normal training
if model is not None:
exp_manager.learn(model)
exp_manager.save_trained_model(model)
else:
exp_manager.hyperparameters_optimization()