Nevergrad
Nevergrad is a Python 3.6+ library for performing gradient-free optimization. Developed by Facebook AI Research, it provides a rich collection of optimization algorithms (evolutionary, bandit, Bayesian, etc.) and robust tools for parameter and hyperparameter tuning. It can optimize functions with continuous, discrete, or mixed variable types, even in noisy environments. The library maintains an active development status with regular releases.
Warnings
- breaking Nevergrad has experienced compatibility issues with NumPy 2.0 due to expired deprecations in NumPy's API, particularly affecting optimizers like `NGOpt` and `NgDS`. While fixes have been merged into the `main` branch, older versions or complex dependency trees might still encounter these problems.
- gotcha The `parametrization` API (e.g., `ng.p.Instrumentation`, `ng.p.Scalar`, `ng.p.Choice`) is explicitly stated as a 'work in progress' and subject to future breaking changes.
- deprecated The `colorama` dependency was removed in version 1.0.12. If your application indirectly relied on `nevergrad` for `colorama`'s functionality (e.g., colored terminal output), it will cease to work.
- gotcha Some optimizers, particularly Differential Evolution (DE) algorithms, can be inefficient or perform poorly when provided with very small budgets (e.g., `budget < 60`).
- gotcha Not all optimizers support the fully asynchronous 'ask and tell' interface. Optimizers with `no_parallelization=True` will not work correctly in parallel execution environments designed for asynchronous `ask`/`tell` calls.
Install
-
pip install nevergrad
Imports
- ng
import nevergrad as ng
- NGOpt
from nevergrad.optimizers import NGOpt
from nevergrad.optimization import NGOpt
- Instrumentation
from nevergrad.instrumentation import Instrumentation
from nevergrad import parametrization as p parametrization = p.Instrumentation(...)
Quickstart
import nevergrad as ng
import numpy as np
def objective_function(learning_rate: float, batch_size: int, architecture: str) -> float:
# Simulate a training process; optimal for lr=0.2, bs=4, arch='conv'
return (learning_rate - 0.2)**2 + (batch_size - 4)**2 + (0 if architecture == 'conv' else 10)
# Define the parameter space using Instrumentation
parametrization = ng.p.Instrumentation(
# Log-distributed scalar for learning_rate
learning_rate=ng.p.Log(lower=0.001, upper=1.0),
# Integer scalar for batch_size
batch_size=ng.p.Scalar(lower=1, upper=12).set_integer_casting(),
# Categorical choice for architecture
architecture=ng.p.Choice(["conv", "fc"]),
)
# Choose an optimizer (NGOpt is a recommended adaptive optimizer)
optimizer = ng.optimizers.NGOpt(parametrization=parametrization, budget=100)
# Minimize the objective function
recommendation = optimizer.minimize(objective_function)
print(f"Optimal hyperparameters: {recommendation.kwargs}")
print(f"Best objective value: {objective_function(**recommendation.kwargs)}")