Optuna

Last Updated : 18 Jul, 2025

Optuna is a free, open source tool that helps you automatically find the best settings called hyperparameters for your machine learning models. Instead of guessing or trying random values Optuna uses smart algorithms to search efficiently, saving time and improving model performance. It uses advanced algorithms like the Tree structured Parzen Estimator (TPE) to search intelligently and adaptively. It supports pruning which stops poor performing trials early to save time and works seamlessly with popular libraries like PyTorch, TensorFlow, LightGBM and XGBoost.

Optuna-Applications
Optuna

Key Features

  • Define by Run API: Dynamically define the search space within your Python code making it flexible and intuitive to use. You don’t need to predefine the whole search space upfront.
  • Efficient Sampling with TPE: Uses advanced algorithms like the Tree structured Parzen Estimator (TPE) to adaptively and intelligently explore hyperparameter space improving search efficiency over random or grid search.
  • Pruning Mechanism: Optuna’s XGBoostPruningCallback lets you connect Optuna’s pruning mechanism directly to XGBoost’s cross validation or training loop. Supports automatic pruning of unpromising trials based on intermediate results, saving computational resources and speeding up optimization.
  • Scalability and Parallelism: Supports distributed and parallel execution of trials across multiple machines or processors for faster optimization.

Basic Functions

FunctionDescription
create_study()Initializes a new study object to manage the optimization.
study.optimize()Runs the optimization by executing multiple trials.
trial.suggest_int()Suggests an integer hyperparameter within a specified range.
trial.suggest_float()Suggests a float hyperparameter within a range.
trial.suggest_categorical()Suggests a hyperparameter from a list of categories.
study.best_paramsReturns the best hyperparameter set found in the study.
study.best_valueReturns the best objective value obtained in the study.
study.trialsLists all the trials executed in the study.

How does it Work

  1. First you define what hyperparameters you want to tune and their possible values. Then you write an objective function that trains your model using these hyperparameters and returns a score showing how well the model did.
  2. Optuna runs a series of trials where each trial picks a set of hyperparameters to try. But instead of choosing randomly Optuna uses an intelligent algorithm called Tree structured Parzen Estimator (TPE). This algorithm learns from previous trial results to suggest better hyperparameters over time focusing on promising regions in the search space.
  3. To save time Optuna can also prune or stop trials early if they don’t look like they will perform well based on intermediate results which helps it avoids wasting resources on bad trials.
  4. You can run many trials sequentially or in parallel to speed up the search. After all trials finish Optuna reports the best hyperparameters found helping you build a better machine learning model with minimal manual effort.

How to Install Optuna

Step 1: Install Optuna Library

Python
pip install optuna

Step 2: Check Version

Python
print(optuna.__version__)

Output:

4.4.0

For Example

  • This code uses Optuna to find the best hyperparameters (C and gamma) for an SVM classifier on the Iris dataset.
  • The objective function trains and evaluates the model using cross validation and returns the accuracy.
  • Optuna runs 50 trials each testing different hyperparameter values suggested by its optimization algorithm aiming to maximize accuracy.
  • Finally it prints the best hyperparameters and the highest accuracy found.
Python
import optuna
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC

def objective(trial):
    iris = load_iris()
    svc_c = trial.suggest_loguniform('C', 1e-3, 1e3)
    svc_gamma = trial.suggest_loguniform('gamma', 1e-4, 1e-1)

    clf = SVC(C=svc_c, gamma=svc_gamma)
    score = cross_val_score(clf, iris.data, iris.target, n_jobs=-1, cv=3)
    accuracy = score.mean()
    return accuracy

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=50)

print(f"Best parameters: {study.best_params}")
print(f"Best accuracy: {study.best_value}")

Output:

Best parameters: {'C': 638.3599386592521, 'gamma': 0.0009812787128191853}

Best accuracy: 0.9933333333333333

Applications

  1. Machine Learning Model Tuning: Optimizing hyperparameters for models like decision trees, random forests, support vector machines and neural networks to boost accuracy and generalization.
  2. Deep Learning: Fine tuning parameters such as learning rate, batch size, number of layers and dropout rates in frameworks like TensorFlow and PyTorch.
  3. Automated Machine Learning (AutoML): Integrating Optuna in AutoML pipelines to automate model selection and hyperparameter search.
  4. Natural Language Processing (NLP): Tuning transformer models, embeddings and training schedules to enhance language model performance.
  5. Computer Vision: Optimizing convolutional neural network architectures, augmentation parameters and training configurations.
Comment