You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

example.py 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import time
  2. def evaluation_fn(step, width, height):
  3. return (0.1 + width * step / 100) ** (-1) + height * 0.1
  4. def easy_objective(config):
  5. from ray import tune
  6. # Hyperparameters
  7. width, height = config["width"], config["height"]
  8. for step in range(config["steps"]):
  9. # Iterative training function - can be any arbitrary training procedure
  10. intermediate_score = evaluation_fn(step, width, height)
  11. # Feed the score back back to Tune.
  12. tune.report(iterations=step, mean_loss=intermediate_score)
  13. time.sleep(0.1)
  14. def test_blendsearch_tune(smoke_test=True):
  15. try:
  16. from ray import tune
  17. from ray.tune.schedulers import AsyncHyperBandScheduler
  18. from ray import __version__ as ray_version
  19. if ray_version.startswith("1."):
  20. from ray.tune.suggest import ConcurrencyLimiter
  21. from ray.tune.suggest.flaml import BlendSearch
  22. else:
  23. from ray.tune.search import ConcurrencyLimiter
  24. from ray.tune.search.flaml import BlendSearch
  25. except ImportError:
  26. print("ray[tune] is not installed, skipping test")
  27. return
  28. import numpy as np
  29. algo = BlendSearch()
  30. algo = ConcurrencyLimiter(algo, max_concurrent=4)
  31. scheduler = AsyncHyperBandScheduler()
  32. analysis = tune.run(
  33. easy_objective,
  34. metric="mean_loss",
  35. mode="min",
  36. search_alg=algo,
  37. scheduler=scheduler,
  38. num_samples=10 if smoke_test else 100,
  39. config={
  40. "steps": 100,
  41. "width": tune.uniform(0, 20),
  42. "height": tune.uniform(-100, 100),
  43. # This is an ignored parameter.
  44. "activation": tune.choice(["relu", "tanh"]),
  45. "test4": np.zeros((3, 1)),
  46. },
  47. )
  48. print("Best hyperparameters found were: ", analysis.best_config)
  49. if __name__ == "__main__":
  50. test_blendsearch_tune(False)