Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OptunaSearchCV with terminator #225

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ The examples below provide codeblocks similar to the example above for various d
* [Tensorflow (eager)](./tensorflow/tensorflow_eager_simple.py)
* [XGBoost](./xgboost/xgboost_simple.py)


### An example of Optuna Dashboard

The following example demonstrates how to use [Optuna Dashboard](https://github.com/optuna/optuna-dashboard).
Expand All @@ -69,6 +68,7 @@ The following example demonstrates how to use [Optuna Dashboard](https://github.
### An example where an objective function uses additional arguments

The following example demonstrates how to implement an objective function that uses additional arguments other than `trial`.

* [Scikit-learn (callable class version)](./sklearn/sklearn_additional_args.py)

### Examples of Pruning
Expand Down Expand Up @@ -107,6 +107,7 @@ In addition, integration modules are available for the following libraries, prov
### Examples of Terminator

* [Optuna Terminator](./terminator/terminator_simple.py)
* [OptunaSearchCV with Terminator](./terminator/terminator_search_cv.py)

### Examples of Multi-Objective Optimization

Expand Down Expand Up @@ -167,6 +168,7 @@ In addition, integration modules are available for the following libraries, prov
PRs to add additional projects welcome!

### Running with Optuna's Docker images?

You can use our docker images with the tag ending with `-dev` to run most of the examples.
For example, you can run [PyTorch Simple](./pytorch/pytorch_simple.py) via `docker run --rm -v $(pwd):/prj -w /prj optuna/optuna:py3.7-dev python pytorch/pytorch_simple.py`.
Also, you can try our visualization example in Jupyter Notebook by opening `localhost:8888` in your browser after executing this:
Expand Down
37 changes: 37 additions & 0 deletions terminator/terminator_search_cv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Optuna example that optimizes a classifier configuration using OptunaSearchCV.

This example is the same as `sklearn/sklearn_optuna_search_cv_simple.py` except
that you leave termination of the study up to the terminator callback.
"""

import optuna
from optuna.terminator import TerminatorCallback

from sklearn.datasets import load_iris
from sklearn.svm import SVC


if __name__ == "__main__":
clf = SVC(gamma="auto")

param_distributions = {
"C": optuna.distributions.FloatDistribution(1e-10, 1e10, log=True),
"degree": optuna.distributions.IntDistribution(1, 5),
}
terminator = TerminatorCallback()

optuna_search = optuna.integration.OptunaSearchCV(
clf, param_distributions, n_trials=100, timeout=600, verbose=2, callbacks=[terminator]
)

X, y = load_iris(return_X_y=True)
optuna_search.fit(X, y)

print("Best trial:")
trial = optuna_search.study_.best_trial

print(" Value: ", trial.value)
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))