Sub topic 4
Defining the search space with suggest_float, suggest_int and suggest_categorical
This chapter assumes you can write a basic objective function and run a study, as covered in the previous chapters. It also assumes you know the difference between a model parameter and a hyperparameter.
What a search space is
A search space is the set of values each hyperparameter is allowed to take. In Optuna you do not declare it up front. Each call to a suggest_* method on the trial object both asks for a value and records the range for that parameter. This is the define-by-run style: the space is built as the objective function executes.
The three core methods
| Method | Use for | Key arguments |
|---|---|---|
| `suggest_float(name, low, high)` | Continuous values | `log=True`, `step=...` |
| `suggest_int(name, low, high)` | Whole numbers | `log=True`, `step=...` |
| `suggest_categorical(name, choices)` | A fixed list of options | `choices` |
Both low and high are inclusive. Each name identifies one parameter, so use a different name for each hyperparameter in the same objective.
import optuna
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
X, y = load_iris(return_X_y=True)
def objective(trial):
lr = trial.suggest_float("learning_rate", 1e-3, 1.0, log=True)
n_estimators = trial.suggest_int("n_estimators", 20, 200, step=20)
max_depth = trial.suggest_int("max_depth", 1, 5)
loss = trial.suggest_categorical("loss", ["log_loss", "exponential"])
model = GradientBoostingClassifier(
learning_rate=lr,
n_estimators=n_estimators,
max_depth=max_depth,
loss=loss,
random_state=0,
)
return cross_val_score(model, X, y, cv=3).mean()
study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=20)
print(study.best_params)Choosing linear or log scale
By default, values are sampled uniformly across the range. For a parameter such as a learning rate or a regularisation strength, the useful values span several orders of magnitude. Uniform sampling between 0.001 and 1.0 would put about 90% of the draws above 0.1. With log=True, each power of ten gets about the same share of draws.
Steps and discrete values
Adding step restricts a value to a grid: low, low + step, low + 2*step, and so on. This suits parameters where small differences do not matter, such as the number of trees. You cannot combine step with log=True.
Categorical choices
suggest_categorical returns one item from a list. Choices should be None, booleans, integers, floats or strings. Optuna stores them in the study, so avoid passing objects such as model instances. Pass a string name and build the object inside the objective.
Sizing the ranges
- Start wide, then narrow the range after looking at the results.
- Keep ranges as small as the problem allows. Every extra parameter and every extra unit of range enlarges the space the sampler must explore.
- Check that the best values do not sit on a boundary. If they do, widen that range.
Exercise
You tune the regularisation strength alpha of a linear model and expect good values anywhere between 0.00001 and 10. Which call should you use, and why?
Show answer
Use trial.suggest_float("alpha", 1e-5, 10, log=True). The range covers six orders of magnitude, so log scaling spreads trials evenly across each power of ten. Linear sampling would almost never try values below 0.1.