2018-11-29 13:33:39 -08:00
|
|
|
"""Example of using a custom model with batch norm."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
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, \
|
|
|
|
TorchBatchNormModel
|
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
|
|
|
|
|
|
|
tf = try_import_tf()
|
2018-11-29 13:33:39 -08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--run", type=str, default="PPO")
|
2020-05-12 08:23:10 +02:00
|
|
|
parser.add_argument("--as-test", action="store_true")
|
|
|
|
parser.add_argument("--torch", action="store_true")
|
|
|
|
parser.add_argument("--stop-iters", type=int, default=200)
|
|
|
|
parser.add_argument("--stop-timesteps", type=int, default=100000)
|
|
|
|
parser.add_argument("--stop-reward", type=float, default=150)
|
2018-11-29 13:33:39 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
2020-05-12 08:23:10 +02:00
|
|
|
ray.init(local_mode=True)
|
2018-11-29 13:33:39 -08:00
|
|
|
|
2020-05-12 08:23:10 +02:00
|
|
|
ModelCatalog.register_custom_model(
|
|
|
|
"bn_model", TorchBatchNormModel if args.torch else BatchNormModel)
|
2020-04-29 12:12:59 +02:00
|
|
|
|
|
|
|
config = {
|
|
|
|
"env": "Pendulum-v0" if args.run == "DDPG" else "CartPole-v0",
|
|
|
|
"model": {
|
|
|
|
"custom_model": "bn_model",
|
|
|
|
},
|
|
|
|
"num_workers": 0,
|
2020-05-27 16:19:13 +02:00
|
|
|
"framework": "torch" if args.torch else "tf",
|
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
|
|
|
}
|
|
|
|
|
2020-05-12 08:23:10 +02:00
|
|
|
results = tune.run(args.run, stop=stop, config=config)
|
|
|
|
|
|
|
|
if args.as_test:
|
|
|
|
check_learning_achieved(results, args.stop_reward)
|
|
|
|
|
|
|
|
ray.shutdown()
|