Skip to content

Instantly share code, notes, and snippets.

@xadrianzetx
Created November 8, 2022 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xadrianzetx/753781c0dd89132343a7c1da6f73dd8e to your computer and use it in GitHub Desktop.
Save xadrianzetx/753781c0dd89132343a7c1da6f73dd8e to your computer and use it in GitHub Desktop.
import time
import optuna
import optuna_distributed
def objective(trial):
x = trial.suggest_float("x", -100, 100)
y = trial.suggest_categorical("y", [-1, 0, 1])
time.sleep(1.0)
return x**2 + y
sequential_start = time.time()
study = optuna.create_study()
study.optimize(objective, n_trials=20)
sequential_duration = time.time() - sequential_start
sequential_best_value = study.best_value
distributed_start = time.time()
distributed_study = optuna_distributed.from_study(study)
distributed_study.optimize(objective, n_trials=20)
distributed_duration = time.time() - distributed_start
distributed_best_value = distributed_study.best_value
print(f"Sequential optimization took {sequential_duration:.2f} seconds with best value of {sequential_best_value:.4f}")
print(f"Asynchronous optimization took {distributed_duration:.2f} seconds with best value of {distributed_best_value:.4f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment