Last active
February 18, 2019 07:21
-
-
Save toshihikoyanase/99f4119f226da4ad227cd6f381136c72 to your computer and use it in GitHub Desktop.
An alternative example to import existing experimental results into Optuna's storage.
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 this example, we demonstrate how to import existing experimental results | |
and continue the optimization. | |
We have the following two ways to execute this example: | |
(1) Execute this code directly. | |
$ python quadratic_change_range.py | |
(2) Execute through CLI. | |
$ STUDY_NAME=`optuna create-study --storage sqlite:///example.db` | |
$ optuna study optimize quadratic_change_range.py objective --n-trials=100 \ | |
--study $STUDY_NAME --storage sqlite:///example.db | |
""" | |
import optuna | |
def suggest_all(trial): | |
x = trial.suggest_uniform('x', -100, 100) | |
y = trial.suggest_int('y', -1, 1) | |
return x, y | |
def suggest_existing_value(trial, x, y): | |
_x = trial.suggest_uniform('x', x, x) | |
_y = trial.suggest_int('y', y, y) | |
return _x, _y | |
# Define a simple 2-dimensional quadratic function whose minimum value is -1 when (x, y) = (0, -1). | |
def quadratic(x, y): | |
return x ** 2 + y | |
def objective(trial): | |
x, y = suggest_all(trial) | |
return quadratic(x, y) | |
def objective_existing_value(trial, x, y): | |
_x, _y = suggest_existing_value(trial, x, y) | |
return quadratic(x, y) | |
if __name__ == '__main__': | |
# Let us minimize the objective function above. | |
# Caveat 1: use RandomSampler to avoid issue #318. | |
study = optuna.create_study(sampler=optuna.samplers.RandomSampler()) | |
# 1. Run grid search. | |
param_value_pairs = [] | |
step = 40 | |
for x in range(-100, 100 + step, step): | |
for y in [-1, 0, 1]: | |
study.optimize(lambda trial: objective_existing_value(trial, x, y), | |
n_trials=1) | |
print(study.trials_dataframe()) | |
print('Best value: {} (params: {})\n'.format(study.best_value, study.best_params)) | |
# 2. Continue optimization with Optuna. | |
# Caveat 2: revert to TPESampler which is Optuna's default sampler. | |
study.optimize(objective, n_trials=10) | |
print(study.trials_dataframe()) | |
print('Best value: {} (params: {})\n'.format(study.best_value, study.best_params)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment