# Optuna with huggingface

**URL:** <https://discuss.huggingface.co/t/optuna-with-huggingface/16827>\
**Category:** Intermediate\
**Created:** [April 14, 2022, 7:34am UTC](https://discuss.huggingface.co/t/optuna-with-huggingface/16827 "2022-04-14T07:34:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaryaAI](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/maryaai/32/14313_2.png) [@MaryaAI](https://discuss.huggingface.co/u/MaryaAI)\
**Post date:** [April 14, 2022, 7:34am UTC](https://discuss.huggingface.co/t/optuna-with-huggingface/16827/1 "2022-04-14T07:34:56Z")

</div>

Hello all, Is there any example using Optuna with huggingface?

---

<div class="post-metadata">

**Author:** ![osanseviero](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/osanseviero/32/38738_2.png) [@osanseviero](https://discuss.huggingface.co/u/osanseviero)\
**Post date:** [April 16, 2022, 4:53pm UTC](https://discuss.huggingface.co/t/optuna-with-huggingface/16827/2 "2022-04-16T16:53:07Z")

</div>

Hi there 👋

You can find a self-contained example in the “Finding Good HYperparameters with Optuna” section at [notebooks/08\_model-compression.ipynb at main · nlp-with-transformers/notebooks · GitHub](https://github.com/nlp-with-transformers/notebooks/blob/main/08_model-compression.ipynb). In short, you can use the `hyperparameter_search` method from the `Trainer`.

```python
def hp_space(trial):
    return {"num_train_epochs": trial.suggest_int("num_train_epochs", 5, 10),
        "alpha": trial.suggest_float("alpha", 0, 1),
        "temperature": trial.suggest_int("temperature", 2, 20)}

best_run = trainer.hyperparameter_search(
    n_trials=20, direction="maximize", hp_space=hp_space)

```
