Hyper-parameter Pruning with Optuna: Efficient Machine Learning Optimization

Krishna Pullakandam
3 min readAug 23, 2024

--

Finding the right hyper-parameters for your model can be a time-consuming and computationally expensive process. Optuna is a powerful hyper-parameter optimization framework that not only helps you find the best hyper-parameters but also includes a feature - pruning. In this blog post, let’s explore how Optuna’s pruning mechanism can significantly speed up your hyperparameter search while maintaining high-quality results.

What is Hyper-parameter Pruning?

Hyper-parameter pruning is a technique that stops unpromising trials early in the optimization process. Instead of waiting for every trial to complete, pruning allows you to halt trials that are unlikely to produce good results, saving valuable time and computational resources.

How Optuna Implements Pruning

Optuna implements pruning through a mechanism called “Trial Pruning.”

Here’s how it works:

  1. During a trial, intermediate results are periodically reported to Optuna.
  2. Optuna evaluates these intermediate results against the best results observed so far.
  3. If a trial’s intermediate results are not promising, Optuna prunes (stops) the trial early.

This approach allows Optuna to focus on more promising hyper-parameter configurations, ultimately leading to faster and more efficient optimization.

Implementing Pruning in Optuna

Let’s walk through an example of how to implement pruning in Optuna using a simple scikit-learn model.

Step 1: Define the Objective Function

First, we need to define an objective function that Optuna will optimize. This function should include pruning checks:

import optuna

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

def objective(trial):

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

n_estimators = trial.suggest_int('n_estimators', 10, 100)
max_depth = trial.suggest_int('max_depth', 1, 32, log=True)

model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)

for step in range(100):
model.fit(X_train, y_train)
intermediate_value = accuracy_score(y_test, model.predict(X_test))

trial.report(intermediate_value, step)

if trial.should_prune():
raise optuna.TrialPruned()

return accuracy_score(y_test, model.predict(X_test))

In this example, we’re optimizing a Random Forest Classifier. We report the model’s accuracy at each step and check if the trial should be pruned.

Step 2: Create a Study with a Pruner

Next, we create an Optuna study and specify a pruner. Optuna offers several pruning algorithms. Let’s use the MedianPruner:

study = optuna.create_study(direction='maximize',
pruner = optuna.pruners.MedianPruner(n_warmup_steps=5, n_startup_trials=5))

The MedianPruner compares the intermediate value of a trial against the median of previous trials. The `n_warmup_steps` parameter specifies how many steps to wait before pruning can happen within a trial, and `n_startup_trials` specifies how many trials to run before pruning can happen at all.

Step 3: Optimize the Study

Finally, we run the optimization:

study.optimize(objective, n_trials=100)
print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial
print(" Value: ", trial.value)
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))

Benefits of Pruning

  1. Faster Optimization: By stopping unpromising trials early, pruning allows Optuna to explore more hyper-parameter configurations in less time.
  2. Resource Efficiency: Pruning reduces the computational resources wasted on poor hyper-parameter configurations.
  3. Better Results: By focusing on promising areas of the hyper-parameter space, pruning can lead to finding better configurations overall.

Considerations When Using Pruning

While pruning is a powerful technique, there are a few things to keep in mind:

  1. Pruning Aggressiveness: Different pruners have different levels of aggressiveness. You may need to experiment to find the right balance for your problem.
  2. Early Stopping: Make sure your model can handle early stopping gracefully. Some models may require a minimum number of iterations to produce meaningful results.
  3. Reporting Frequency: The frequency at which you report intermediate values can affect pruning performance. Reporting too often may lead to premature pruning, while reporting too infrequently may reduce the benefits of pruning.

Conclusion

Hyper-parameter pruning with Optuna is a powerful technique that can significantly speed up your machine learning workflow. By intelligently stopping unpromising trials, Optuna allows you to explore more hyperparameter configurations in less time, potentially leading to better models with less computational expense.

As you incorporate pruning into your optimization processes, remember to experiment with different pruners and settings to find what works best for your specific problem. Happy optimizing!

--

--

Krishna Pullakandam
Krishna Pullakandam

Written by Krishna Pullakandam

AI and Coffee enthusiast. I love to write about technology, business, and culture.