mirror of
https://github.com/microsoft/autogen.git
synced 2025-09-24 07:34:02 +00:00

* handle non-flaml scheduler in flaml.tune * revise time budget * Update website/docs/Use-Cases/Tune-User-Defined-Function.md Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update website/docs/Use-Cases/Tune-User-Defined-Function.md Co-authored-by: Chi Wang <wang.chi@microsoft.com> * Update flaml/tune/tune.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * add docstr * remove random seed * StopIteration * StopIteration format * format * Update flaml/tune/tune.py Co-authored-by: Chi Wang <wang.chi@microsoft.com> * revise docstr Co-authored-by: Chi Wang <wang.chi@microsoft.com>
108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
from functools import partial
|
|
import time
|
|
|
|
|
|
def evaluation_fn(step, width, height):
|
|
return (0.1 + width * step / 100) ** (-1) + height * 0.1
|
|
|
|
|
|
def easy_objective(use_raytune, config):
|
|
if use_raytune:
|
|
from ray import tune
|
|
else:
|
|
from flaml import tune
|
|
# 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.
|
|
try:
|
|
tune.report(iterations=step, mean_loss=intermediate_score)
|
|
except StopIteration:
|
|
return
|
|
|
|
|
|
def test_tune_scheduler(smoke_test=True, use_ray=True, use_raytune=False):
|
|
import numpy as np
|
|
from flaml.searcher.blendsearch import BlendSearch
|
|
|
|
np.random.seed(100)
|
|
easy_objective_custom_tune = partial(easy_objective, use_raytune)
|
|
if use_raytune:
|
|
try:
|
|
from ray import tune
|
|
except ImportError:
|
|
print("ray[tune] is not installed, skipping test")
|
|
return
|
|
searcher = BlendSearch(
|
|
space={
|
|
"steps": 100,
|
|
"width": tune.uniform(0, 20),
|
|
"height": tune.uniform(-100, 100),
|
|
# This is an ignored parameter.
|
|
"activation": tune.choice(["relu", "tanh"]),
|
|
"test4": np.zeros((3, 1)),
|
|
}
|
|
)
|
|
analysis = tune.run(
|
|
easy_objective_custom_tune,
|
|
search_alg=searcher,
|
|
metric="mean_loss",
|
|
mode="min",
|
|
num_samples=10 if smoke_test else 100,
|
|
scheduler="asynchyperband",
|
|
config={
|
|
"steps": 100,
|
|
"width": tune.uniform(0, 20),
|
|
"height": tune.uniform(-100, 100),
|
|
# This is an ignored parameter.
|
|
"activation": tune.choice(["relu", "tanh"]),
|
|
"test4": np.zeros((3, 1)),
|
|
},
|
|
)
|
|
else:
|
|
from flaml import tune
|
|
|
|
searcher = BlendSearch(
|
|
space={
|
|
"steps": 100,
|
|
"width": tune.uniform(0, 20),
|
|
"height": tune.uniform(-100, 100),
|
|
# This is an ignored parameter.
|
|
"activation": tune.choice(["relu", "tanh"]),
|
|
"test4": np.zeros((3, 1)),
|
|
}
|
|
)
|
|
analysis = tune.run(
|
|
easy_objective_custom_tune,
|
|
search_alg=searcher,
|
|
metric="mean_loss",
|
|
mode="min",
|
|
num_samples=10 if smoke_test else 100,
|
|
scheduler="asynchyperband",
|
|
resource_attr="iterations",
|
|
max_resource=99,
|
|
# min_resource=1,
|
|
# reduction_factor=4,
|
|
config={
|
|
"steps": 100,
|
|
"width": tune.uniform(0, 20),
|
|
"height": tune.uniform(-100, 100),
|
|
# This is an ignored parameter.
|
|
"activation": tune.choice(["relu", "tanh"]),
|
|
"test4": np.zeros((3, 1)),
|
|
},
|
|
use_ray=use_ray,
|
|
)
|
|
|
|
print("Best hyperparameters found were: ", analysis.best_config)
|
|
print("best results", analysis.best_result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_tune_scheduler(smoke_test=True, use_ray=True, use_raytune=True)
|
|
test_tune_scheduler(smoke_test=True, use_ray=True)
|
|
test_tune_scheduler(smoke_test=True, use_ray=False)
|