ray_tune.py 898 B

1234567891011121314151617181920212223242526272829303132
  1. from ray import tune
  2. from ray.air import session
  3. def objective(step, alpha, beta):
  4. return (0.1 + alpha * step / 100) ** (-1) + beta * 0.1
  5. def training_function(config):
  6. # Hyperparameters
  7. alpha, beta = config["alpha"], config["beta"]
  8. for step in range(10):
  9. # Iterative training function - can be any arbitrary training procedure.
  10. intermediate_score = objective(step, alpha, beta)
  11. # Feed the score back back to Tune.
  12. session.report({"mean_loss": intermediate_score})
  13. tuner = tune.Tuner(
  14. training_function,
  15. param_space={
  16. "alpha": tune.grid_search([0.001, 0.01, 0.1]),
  17. "beta": tune.choice([1, 2, 3]),
  18. },
  19. )
  20. results = tuner.fit()
  21. best_result = results.get_best_result(metric="mean_loss", mode="min")
  22. print("Best result: ", best_result.metrics)
  23. # Get a dataframe for analyzing trial results.
  24. df = results.get_dataframe()