forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RLlib-contrib] CRR. (ray-project#36616)
- Loading branch information
Showing
11 changed files
with
1,140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# CRR (Critic Regularized Regression) | ||
|
||
|
||
[CRR](https://arxiv.org/abs/2006.15134) is another offline RL algorithm based on Q-learning that can learn from an offline experience replay. The challenge in applying existing Q-learning algorithms to offline RL lies in the overestimation of the Q-function, as well as, the lack of exploration beyond the observed data. The latter becomes increasingly important during bootstrapping in the bellman equation, where the Q-function queried for the next state’s Q-value(s) does not have support in the observed data. To mitigate these issues, CRR implements a simple and yet powerful idea of “value-filtered regression”. The key idea is to use a learned critic to filter-out the non-promising transitions from the replay dataset. | ||
|
||
|
||
## Installation | ||
|
||
``` | ||
conda create -n rllib-crr python=3.10 | ||
conda activate rllib-crr | ||
pip install -r requirements.txt | ||
pip install -e '.[development]' | ||
``` | ||
|
||
## Usage | ||
|
||
[CRR Example]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import argparse | ||
|
||
from rllib_crr.crr import CRR, CRRConfig | ||
|
||
import ray | ||
from ray import air, tune | ||
from ray.rllib.utils.test_utils import check_learning_achieved | ||
|
||
|
||
def get_cli_args(): | ||
"""Create CLI parser and return parsed arguments""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--run-as-test", action="store_true", default=False) | ||
args = parser.parse_args() | ||
print(f"Running with following CLI args: {args}") | ||
return args | ||
|
||
|
||
if __name__ == "__main__": | ||
args = get_cli_args() | ||
|
||
ray.init() | ||
config = ( | ||
CRRConfig() | ||
.environment(env="CartPole-v1", clip_actions=True) | ||
.framework("torch") | ||
.offline_data( | ||
input_="dataset", | ||
input_config={ | ||
"format": "json", | ||
"paths": ["s3://anonymous@air-example-data/rllib/cartpole/large.json"], | ||
}, | ||
actions_in_input_normalized=True, | ||
) | ||
.training( | ||
twin_q=True, | ||
weight_type="exp", | ||
advantage_type="mean", | ||
n_action_sample=4, | ||
target_network_update_freq=10000, | ||
tau=0.0005, | ||
gamma=0.99, | ||
train_batch_size=2048, | ||
critic_hidden_activation="tanh", | ||
critic_hiddens=[128, 128, 128], | ||
critic_lr=0.0003, | ||
actor_hidden_activation="tanh", | ||
actor_hiddens=[128, 128, 128], | ||
actor_lr=0.0003, | ||
temperature=1.0, | ||
max_weight=20.0, | ||
) | ||
.evaluation( | ||
evaluation_interval=1, | ||
evaluation_num_workers=1, | ||
evaluation_duration=10, | ||
evaluation_duration_unit="episodes", | ||
evaluation_parallel_to_training=True, | ||
evaluation_config=CRRConfig.overrides(input_="sampler", explore=False), | ||
) | ||
.rollouts(num_rollout_workers=3) | ||
) | ||
|
||
stop_reward = 200 | ||
|
||
tuner = tune.Tuner( | ||
CRR, | ||
param_space=config.to_dict(), | ||
run_config=air.RunConfig( | ||
stop={ | ||
"evaluation/sampler_results/episode_reward_mean": stop_reward, | ||
"training_iteration": 100, | ||
}, | ||
failure_config=air.FailureConfig(fail_fast="raise"), | ||
), | ||
) | ||
results = tuner.fit() | ||
|
||
if args.run_as_test: | ||
check_learning_achieved( | ||
results, | ||
stop_reward, | ||
metric="evaluation/sampler_results/episode_reward_mean", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[build-system] | ||
requires = ["setuptools>=61.0"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[tool.setuptools.packages.find] | ||
where = ["src"] | ||
|
||
[project] | ||
name = "rllib-crr" | ||
authors = [{name = "Anyscale Inc."}] | ||
version = "0.1.0" | ||
description = "" | ||
readme = "README.md" | ||
requires-python = ">=3.7, <3.11" | ||
dependencies = ["gymnasium", "ray[rllib]==2.5.0"] | ||
|
||
[project.optional-dependencies] | ||
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "torch==1.12.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
torch==1.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rllib_crr.crr.crr import CRR, CRRConfig | ||
from rllib_crr.crr.crr_torch_model import CRRModel | ||
from rllib_crr.crr.crr_torch_policy import CRRTorchPolicy | ||
|
||
from ray.tune.registry import register_trainable | ||
|
||
__all__ = ["CRR", "CRRConfig", "CRRModel", "CRRTorchPolicy"] | ||
|
||
register_trainable("rllib-contrib-crr", CRR) |
Oops, something went wrong.