2022-06-14 00:52:42 -04:00
from functools import partial
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 :
print ( " Trial stopped " , step )
return
2022-06-21 18:59:07 -07:00
def test_tune ( externally_setup_searcher = False , use_ray = False , use_raytune = False ) :
2022-06-14 00:52:42 -04:00
from flaml import tune
2022-10-04 16:03:22 -07:00
from flaml . tune . searcher . blendsearch import BlendSearch
2022-06-14 00:52:42 -04:00
easy_objective_custom_tune = partial ( _easy_objective , use_raytune )
search_space = {
" steps " : 100 ,
" width " : tune . uniform ( 0 , 20 ) ,
" height " : tune . uniform ( - 100 , 100 ) ,
}
2023-01-07 18:39:29 -08:00
if externally_setup_searcher is True :
2022-06-14 00:52:42 -04:00
searcher = BlendSearch (
space = search_space ,
time_budget_s = 5 ,
metric = " mean_loss " ,
mode = " min " ,
)
assert (
searcher . cost_attr == " time_total_s "
) , " when time_budget_s is provided, cost_attr should be time_total_s "
searcher = BlendSearch (
space = search_space ,
num_samples = 10 ,
metric = " mean_loss " ,
mode = " min " ,
)
assert (
searcher . cost_attr is None
) , " when time_budget_s is not provided, cost_attr should be None. "
searcher = BlendSearch (
space = search_space ,
num_samples = 10 ,
time_budget_s = 5 ,
metric = " mean_loss " ,
mode = " min " ,
)
assert (
searcher . cost_attr == " time_total_s "
) , " As long as time_budget_s is provided and cost_attr not otherwise specified (i.e., using the default auto value), time_total_s is used as the cost_attr "
searcher = BlendSearch (
space = search_space ,
num_samples = 10 ,
time_budget_s = 5 ,
metric = " mean_loss " ,
mode = " min " ,
cost_attr = None ,
)
assert (
searcher . cost_attr is None
) , " When the cost_attr is explicitly specified to be None, BS should use None as the cost_attr. "
searcher = BlendSearch (
space = search_space ,
metric = " mean_loss " ,
mode = " min " ,
)
2023-01-07 18:39:29 -08:00
elif externally_setup_searcher is False :
2022-06-14 00:52:42 -04:00
searcher = None
2023-01-07 18:39:29 -08:00
else :
searcher = externally_setup_searcher
2022-06-14 00:52:42 -04:00
analysis = tune . run (
easy_objective_custom_tune ,
search_alg = searcher ,
metric = " mean_loss " ,
mode = " min " ,
num_samples = 10 ,
2022-06-21 18:59:07 -07:00
# time_budget_s=5,
2022-06-14 00:52:42 -04:00
use_ray = use_ray ,
config = search_space ,
)
print ( " Best hyperparameters found were: " , analysis . best_config )
print ( " best results " , analysis . best_result )
print ( " best results " , analysis . results )
return analysis . best_config
def test_reproducibility ( ) :
2022-06-21 18:59:07 -07:00
best_config_1 = test_tune ( )
best_config_2 = test_tune ( )
2022-06-14 00:52:42 -04:00
print ( best_config_1 )
print ( best_config_2 )
assert best_config_1 == best_config_2 , " flaml.tune not reproducible "
2022-06-21 18:59:07 -07:00
best_config_1 = test_tune ( externally_setup_searcher = True )
best_config_2 = test_tune ( externally_setup_searcher = True )
2022-06-14 00:52:42 -04:00
print ( best_config_1 )
print ( best_config_2 )
assert (
best_config_1 == best_config_2
) , " flaml.tune not reproducible when the searcher is set up externally "
2023-01-07 18:39:29 -08:00
def test_gs_reproducibility ( ) :
from flaml import BlendSearch , tune
def f ( config ) :
return { " m " : 0.35 }
search_space = { " a " : tune . randint ( 1 , 100 ) }
bs = BlendSearch ( space = search_space , cost_attr = None )
analysis1 = tune . run ( f , search_alg = bs , num_samples = 2 , metric = " m " , mode = " max " )
bs = BlendSearch ( space = search_space , cost_attr = None )
analysis2 = tune . run ( f , search_alg = bs , num_samples = 2 , metric = " m " , mode = " max " )
assert analysis1 . trials [ - 1 ] . config == analysis2 . trials [ - 1 ] . config
2022-06-14 00:52:42 -04:00
if __name__ == " __main__ " :
test_reproducibility ( )