-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tune] Multi-objective support for Optuna #20489
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03f2205
[rllib] Make sure json can serialize result dict (#20439)
a4b2992
Add test
Yard1 b335e98
Add docs, example
Yard1 9130b8c
Merge branch 'master' into optuna_multi_objective
Yard1 0317304
Fix
Yard1 4ef26b9
Fix
Yard1 7aa8bb4
Merge branch 'master' into optuna_multi_objective
Yard1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
:orphan: | ||
|
||
optuna_example | ||
~~~~~~~~~~~~~~~~ | ||
~~~~~~~~~~~~~~ | ||
|
||
.. literalinclude:: /../../python/ray/tune/examples/optuna_example.py |
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,6 @@ | ||
:orphan: | ||
|
||
optuna_multiobjective_example | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. literalinclude:: /../../python/ray/tune/examples/optuna_multiobjective_example.py |
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,74 @@ | ||
"""This example demonstrates the usage of Optuna with Ray Tune for | ||
multi-objective optimization. | ||
|
||
Please note that schedulers may not work correctly with multi-objective | ||
optimization. | ||
""" | ||
import time | ||
|
||
import ray | ||
from ray import tune | ||
from ray.tune.suggest import ConcurrencyLimiter | ||
from ray.tune.suggest.optuna import OptunaSearch | ||
|
||
|
||
def evaluation_fn(step, width, height): | ||
return (0.1 + width * step / 100)**(-1) + height * 0.1 | ||
|
||
|
||
def easy_objective(config): | ||
# Hyperparameters | ||
width, height = config["width"], config["height"] | ||
|
||
for step in range(config["steps"]): | ||
# Iterative training function - can be any arbitrary training procedure | ||
intermediate_score = evaluation_fn(step, width, height) | ||
# Feed the score back back to Tune. | ||
tune.report( | ||
iterations=step, | ||
loss=intermediate_score, | ||
gain=intermediate_score * width) | ||
time.sleep(0.1) | ||
|
||
|
||
def run_optuna_tune(smoke_test=False): | ||
algo = OptunaSearch(metric=["loss", "gain"], mode=["min", "max"]) | ||
algo = ConcurrencyLimiter(algo, max_concurrent=4) | ||
analysis = tune.run( | ||
easy_objective, | ||
search_alg=algo, | ||
num_samples=10 if smoke_test else 100, | ||
config={ | ||
"steps": 100, | ||
"width": tune.uniform(0, 20), | ||
"height": tune.uniform(-100, 100), | ||
# This is an ignored parameter. | ||
"activation": tune.choice(["relu", "tanh"]) | ||
}) | ||
|
||
print("Best hyperparameters for loss found were: ", | ||
analysis.get_best_config("loss", "min")) | ||
print("Best hyperparameters for gain found were: ", | ||
analysis.get_best_config("gain", "max")) | ||
|
||
|
||
if __name__ == "__main__": | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--smoke-test", action="store_true", help="Finish quickly for testing") | ||
parser.add_argument( | ||
"--server-address", | ||
type=str, | ||
default=None, | ||
required=False, | ||
help="The address of server to connect to if using " | ||
"Ray Client.") | ||
args, _ = parser.parse_known_args() | ||
if args.server_address is not None: | ||
ray.init(f"ray://{args.server_address}") | ||
else: | ||
ray.init(configure_logging=False) | ||
|
||
run_optuna_tune(smoke_test=args.smoke_test) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note] Following samplers don't support multi-objective optimization. So, when
self._mode
is a list andself._sampler
is one of them, Optuna would raise an exception (You can choose other samplers listed in https://optuna.readthedocs.io/en/stable/reference/samplers.html).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to let Optuna raise it here.
BTW. would it be possible for Optuna to add a property to its samplers that would inform whether the sampler can support multi-objective optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice question. Such property is not available now but I'll discuss this with other developers. 👀