Skip to content

Instantly share code, notes, and snippets.

@toshihikoyanase
Created August 28, 2019 06:55
Show Gist options
  • Save toshihikoyanase/593246a6d00d6e9ec52de7b7c0f346f3 to your computer and use it in GitHub Desktop.
Save toshihikoyanase/593246a6d00d6e9ec52de7b7c0f346f3 to your computer and use it in GitHub Desktop.
Optuna example that optimizes a quadratic function in parallel using joblib.
"""
Optuna example that optimizes a simple quadratic function in parallel using joblib.
In this example, we optimize a simple quadratic function. We also demonstrate how to continue an
optimization and to use timeouts.
We have the following way to execute this example:
$ python quadratic_joblib_simple.py
"""
from joblib import Parallel, delayed
import optuna
# Define a simple 2-dimensional objective function whose minimum value is -1 when (x, y) = (0, -1).
def objective(trial):
x = trial.suggest_uniform('x', -100, 100)
y = trial.suggest_categorical('y', [-1, 0, 1])
return x**2 + y
def optimize(n_trials):
study = optuna.load_study(study_name='joblib-quadratic', storage='sqlite:///example.db')
study.optimize(objective, n_trials=n_trials)
if __name__ == '__main__':
# Let us minimize the objective function above.
print('Running 10 trials...')
study = optuna.create_study(study_name='joblib-quadratic', storage='sqlite:///example.db')
r = Parallel(n_jobs=-1)([delayed(optimize)(10) for _ in range(10)])
print('Number of finished trials: ', len(study.trials))
print('Best trial:')
trial = study.best_trial
print(' Value: ', trial.value)
print(' Params: ')
for key, value in trial.params.items():
print(' {}: {}'.format(key, value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment