[tune] refactor and add examples (#11931)

This commit is contained in:
Richard Liaw
2020-11-14 20:43:28 -08:00
committed by GitHub
parent 5891759a3e
commit 8b3f79f307
57 changed files with 587 additions and 503 deletions
+15 -18
View File
@@ -1,10 +1,9 @@
"""This test checks that ZOOptSearch is functional.
"""This example demonstrates the usage of ZOOptSearch.
It also checks that it is usable with a separate scheduler.
"""
import time
import ray
from ray import tune
from ray.tune.suggest.zoopt import ZOOptSearch
from ray.tune.schedulers import AsyncHyperBandScheduler
@@ -12,6 +11,7 @@ from zoopt import ValueType # noqa: F401
def evaluation_fn(step, width, height):
time.sleep(0.1)
return (0.1 + width * step / 100)**(-1) + height * 0.1
@@ -24,7 +24,6 @@ def easy_objective(config):
intermediate_score = evaluation_fn(step, width, height)
# Feed the score back back to Tune.
tune.report(iterations=step, mean_loss=intermediate_score)
time.sleep(0.1)
if __name__ == "__main__":
@@ -34,16 +33,8 @@ if __name__ == "__main__":
parser.add_argument(
"--smoke-test", action="store_true", help="Finish quickly for testing")
args, _ = parser.parse_known_args()
ray.init()
tune_kwargs = {
"num_samples": 10 if args.smoke_test else 1000,
"config": {
"steps": 10,
"height": tune.quniform(-10, 10, 1e-2),
"width": tune.randint(0, 10)
}
}
num_samples = 10 if args.smoke_test else 1000
# Optional: Pass the parameter space yourself
# space = {
@@ -61,17 +52,23 @@ if __name__ == "__main__":
zoopt_search = ZOOptSearch(
algo="Asracos", # only support ASRacos currently
budget=tune_kwargs["num_samples"],
budget=num_samples,
# dim_dict=space, # If you want to set the space yourself
metric="mean_loss",
mode="min",
**zoopt_search_config)
scheduler = AsyncHyperBandScheduler(metric="mean_loss", mode="min")
scheduler = AsyncHyperBandScheduler()
tune.run(
analysis = tune.run(
easy_objective,
metric="mean_loss",
mode="min",
search_alg=zoopt_search,
name="zoopt_search",
scheduler=scheduler,
**tune_kwargs)
num_samples=num_samples,
config={
"steps": 10,
"height": tune.quniform(-10, 10, 1e-2),
"width": tune.randint(0, 10)
})
print("Best config found: ", analysis.best_config)