run_simple_tune_job.py 944 B

1234567891011121314151617181920212223242526272829303132333435
  1. # From https://docs.ray.io/en/latest/tune/index.html
  2. import ray
  3. from ray import train, tune
  4. def objective(step, alpha, beta):
  5. return (0.1 + alpha * step / 100) ** (-1) + beta * 0.1
  6. def training_function(config):
  7. # Hyperparameters
  8. alpha, beta = config["alpha"], config["beta"]
  9. for step in range(10):
  10. # Iterative training function - can be any arbitrary training procedure.
  11. intermediate_score = objective(step, alpha, beta)
  12. # Feed the score back back to Tune.
  13. train.report(dict(mean_loss=intermediate_score))
  14. ray.init(address="auto")
  15. print("Starting Ray Tune job")
  16. analysis = tune.run(
  17. training_function,
  18. config={
  19. "alpha": tune.grid_search([0.001, 0.01, 0.1]),
  20. "beta": tune.choice([1, 2, 3]),
  21. },
  22. )
  23. print("Best config: ", analysis.get_best_config(metric="mean_loss", mode="min"))
  24. # Get a dataframe for analyzing trial results.
  25. df = analysis.results_df
  26. print(df)