Created
February 18, 2019 04:54
-
-
Save toshihikoyanase/c90209d0352a119b26e1be7f152dbf53 to your computer and use it in GitHub Desktop.
An 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_trial_import.py | |
(2) Execute through CLI. | |
$ STUDY_NAME=`optuna create-study --storage sqlite:///example.db` | |
$ optuna study optimize quadratic_trial_import.py objective --n-trials=100 \ | |
--study $STUDY_NAME --storage sqlite:///example.db | |
""" | |
import optuna | |
from optuna.trial import InjectedTrial | |
def suggest_all(trial): | |
x = trial.suggest_uniform('x', -100, 100) | |
y = trial.suggest_categorical('y', [-1, 0, 1]) | |
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 import_trials(study, param_value_pairs): | |
for pv in param_value_pairs: | |
trial = InjectedTrial(study, pv[0]) | |
# We can use InjectedTrial.suggest_xx to save the parameter values along with their ranges. | |
suggest_all(trial) | |
# Save the objective value and change trial state from RUNNING to COMPLETE. | |
trial.report(pv[1]) | |
study.storage.set_trial_state(trial.trial_id, optuna.structs.TrialState.COMPLETE) | |
if __name__ == '__main__': | |
# Let us minimize the objective function above. | |
study = optuna.create_study() | |
# 1. Run grid search. | |
param_value_pairs = [] | |
step = 40 | |
for x in range(-100, 100 + step, step): | |
for y in [-1, 0, 1]: | |
value = quadratic(x, y) | |
param_value_pairs.append(({'x': x, 'y': y}, value)) | |
# 2. Import grid-search results. | |
import_trials(study, param_value_pairs) | |
print(study.trials_dataframe()) | |
print('Best value: {} (params: {})\n'.format(study.best_value, study.best_params)) | |
# 3. Continue optimization with Optuna. | |
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