2022-03-08 16:50:57 +01:00
|
|
|
from ray import tune
|
2022-06-30 10:37:31 -07:00
|
|
|
from ray.air import session
|
2022-03-08 16:50:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def objective(step, alpha, beta):
|
|
|
|
return (0.1 + alpha * step / 100) ** (-1) + beta * 0.1
|
|
|
|
|
|
|
|
|
|
|
|
def training_function(config):
|
|
|
|
# Hyperparameters
|
|
|
|
alpha, beta = config["alpha"], config["beta"]
|
|
|
|
for step in range(10):
|
|
|
|
# Iterative training function - can be any arbitrary training procedure.
|
|
|
|
intermediate_score = objective(step, alpha, beta)
|
|
|
|
# Feed the score back back to Tune.
|
2022-06-30 10:37:31 -07:00
|
|
|
session.report({"mean_loss": intermediate_score})
|
2022-03-08 16:50:57 +01:00
|
|
|
|
|
|
|
|
2022-07-24 15:45:24 +01:00
|
|
|
tuner = tune.Tuner(
|
2022-03-08 16:50:57 +01:00
|
|
|
training_function,
|
2022-07-24 15:45:24 +01:00
|
|
|
param_space={
|
2022-03-08 16:50:57 +01:00
|
|
|
"alpha": tune.grid_search([0.001, 0.01, 0.1]),
|
|
|
|
"beta": tune.choice([1, 2, 3]),
|
|
|
|
},
|
|
|
|
)
|
2022-07-24 15:45:24 +01:00
|
|
|
results = tuner.fit()
|
2022-03-08 16:50:57 +01:00
|
|
|
|
2022-07-24 15:45:24 +01:00
|
|
|
best_result = results.get_best_result(metric="mean_loss", mode="min")
|
|
|
|
print("Best result: ", best_result.metrics)
|
2022-03-08 16:50:57 +01:00
|
|
|
|
|
|
|
# Get a dataframe for analyzing trial results.
|
2022-07-24 15:45:24 +01:00
|
|
|
df = results.get_dataframe()
|