> For the complete documentation index, see [llms.txt](https://gilad-rubin.gitbook.io/hypster/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gilad-rubin.gitbook.io/hypster/integrations/optuna.md).

# Optuna

Optuna is the first supported HPO backend for Hypster. The integration lives in `hypster.hpo.optuna`.

## Install

{% code overflow="wrap" %}

```bash
uv add 'hypster[optuna]'
```

{% endcode %}

or:

{% code overflow="wrap" %}

```bash
pip install 'hypster[optuna]'
```

{% endcode %}

## Basic Pattern

{% code overflow="wrap" %}

```python
import optuna
from sklearn.base import ClassifierMixin
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression

from hypster import HP, instantiate
from hypster.hpo.optuna import suggest_values
from hypster.hpo.types import HpoFloat, HpoInt

def linear_model(hp: HP) -> LogisticRegression:
    C = hp.float(
        1.0,
        name="C",
        min=1e-4,
        max=10.0,
        hpo_spec=HpoFloat(scale="log"),
    )
    return LogisticRegression(C=C, max_iter=1000)

def forest_model(hp: HP) -> RandomForestClassifier:
    n_estimators = hp.int(
        200,
        name="n_estimators",
        min=50,
        max=1000,
        hpo_spec=HpoInt(step=50),
    )
    return RandomForestClassifier(n_estimators=n_estimators, random_state=42)

model_options = {
    "linear": linear_model,
    "forest": forest_model,
}

def model_config(hp: HP) -> ClassifierMixin:
    selected_config = hp.select(model_options, name="model_family", default="forest", options_only=True)
    return hp.nest(selected_config, name="model")

def objective(trial: optuna.Trial) -> float:
    values = suggest_values(trial, model_config)
    model = instantiate(model_config, values=values)
    return train_and_score(model)

study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=30)
```

{% endcode %}

## What Is Supported

* `hp.int`, backed by `trial.suggest_int`
* `hp.float`, backed by `trial.suggest_float`
* `hp.select`, backed by `trial.suggest_categorical`
* `hp.nest`, which prefixes nested parameter paths

Multi-value HP calls are not expanded by the current adapter.

The adapter only accepts HPO spec fields that Optuna can represent. Supported fields include `HpoInt(step=..., scale=..., include_max=...)`, `HpoFloat(step=..., scale=...)`, `HpoFloat(distribution="uniform"|"loguniform")`, and `HpoCategorical(ordered=False, weights=None)`. Unsupported fields such as custom `base=...`, normal/lognormal float distributions, `center=...`, `spread=...`, ordered categoricals, and categorical weights raise instead of being ignored.

Nested explicit overrides passed through `hp.nest(..., values=...)` are validated before `suggest_values()` returns.

## More

* [Perform Hyperparameter Optimization](/hypster/how-to-guides/perform-hyperparameter-optimization.md)
* [Optuna HPO API](/hypster/reference/optuna-hpo.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gilad-rubin.gitbook.io/hypster/integrations/optuna.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
