2018-11-29 13:33:39 -08:00
|
|
|
"""Example of using a custom model with batch norm."""
|
|
|
|
|
|
|
|
import argparse
|
2020-10-02 23:07:44 +02:00
|
|
|
import os
|
2018-11-29 13:33:39 -08:00
|
|
|
|
|
|
|
import ray
|
2019-03-30 14:07:50 -07:00
|
|
|
from ray import tune
|
2020-05-12 08:23:10 +02:00
|
|
|
from ray.rllib.examples.models.batch_norm_model import (
|
|
|
|
BatchNormModel,
|
2021-01-14 14:44:33 +01:00
|
|
|
KerasBatchNormModel,
|
|
|
|
TorchBatchNormModel,
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
2020-04-29 12:12:59 +02:00
|
|
|
from ray.rllib.models import ModelCatalog
|
2020-06-16 08:52:20 +02:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2020-05-12 08:23:10 +02:00
|
|
|
from ray.rllib.utils.test_utils import check_learning_achieved
|
2019-05-16 22:12:07 -07:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2018-11-29 13:33:39 -08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--run", type=str, default="PPO", help="The RLlib-registered algorithm to use."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--framework",
|
|
|
|
choices=["tf", "tf2", "tfe", "torch"],
|
|
|
|
default="tf",
|
|
|
|
help="The DL framework specifier.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--as-test",
|
|
|
|
action="store_true",
|
|
|
|
help="Whether this script should be run as a test: --stop-reward must "
|
|
|
|
"be achieved within --stop-timesteps AND --stop-iters.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-iters", type=int, default=200, help="Number of iterations to train."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-timesteps", type=int, default=100000, help="Number of timesteps to train."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--stop-reward", type=float, default=150.0, help="Reward at which we stop training."
|
|
|
|
)
|
2018-11-29 13:33:39 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
2020-10-01 16:57:10 +02:00
|
|
|
ray.init()
|
2018-11-29 13:33:39 -08:00
|
|
|
|
2020-05-12 08:23:10 +02:00
|
|
|
ModelCatalog.register_custom_model(
|
2021-05-18 13:18:12 +02:00
|
|
|
"bn_model",
|
|
|
|
TorchBatchNormModel
|
|
|
|
if args.framework == "torch"
|
|
|
|
else KerasBatchNormModel
|
2021-01-14 14:44:33 +01:00
|
|
|
if args.run != "PPO"
|
|
|
|
else BatchNormModel,
|
|
|
|
)
|
2020-04-29 12:12:59 +02:00
|
|
|
|
|
|
|
config = {
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
"env": "Pendulum-v1" if args.run in ["DDPG", "SAC"] else "CartPole-v0",
|
2020-04-29 12:12:59 +02:00
|
|
|
"model": {
|
|
|
|
"custom_model": "bn_model",
|
|
|
|
},
|
2021-01-14 14:44:33 +01:00
|
|
|
"lr": 0.0003,
|
2020-10-02 23:07:44 +02:00
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2020-04-29 12:12:59 +02:00
|
|
|
"num_workers": 0,
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2020-05-12 08:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
2020-04-29 12:12:59 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 14:44:33 +01:00
|
|
|
results = tune.run(args.run, stop=stop, config=config, verbose=2)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|