Created
August 28, 2019 06:55
-
-
Save toshihikoyanase/593246a6d00d6e9ec52de7b7c0f346f3 to your computer and use it in GitHub Desktop.
Optuna example that optimizes a quadratic function in parallel using joblib.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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