Nevergrad: Gradient-Free Global Optimization for AI Researchers

Jul 7, 2025

Introduction

Finding the optimal hyperparameters for a machine learning model often feels like searching for a needle in a haystack, especially when the objective function is non-differentiable or noisy. Nevergrad, an open-source library developed by Facebook Research, solves this by providing a comprehensive suite of gradient-free optimization algorithms. With its flexible API and support for complex parameter spaces, Nevergrad allows researchers to optimize complex functions without the need for gradient computation, making it a powerful alternative to traditional grid or random search.

What Is Nevergrad?

Nevergrad is a Python toolbox for performing gradient-free optimization designed for AI researchers and developers. It provides a standard “ask-and-tell” framework that allows users to minimize or maximize objective functions using a wide variety of derivative-free algorithms. Maintained by Facebook Research (FAIR), the project is released under the MIT license and is compatible with Python 3.8+.

The library is structured into several subpackages: optimization for the algorithms themselves, instrumentation for converting code into a well-defined function to optimize, functions for benchmark functions, and benchmark for running experiments to compare different optimizers.

Why Nevergrad Matters

In the world of deep learning, gradient descent is the dominant paradigm. However, many real-world optimization problems—such as hyperparameter tuning, architecture search, and reinforcement learning—are not differentiable. When the objective function is a “black box” and you cannot compute a gradient, traditional methods fail.

Nevergrad fills this gap by offering a unified interface to state-of-the-art derivative-free methods. Instead of implementing custom-built versions of Differential Evolution or Particle Swarm Optimization, developers can now compare multiple algorithms within a single framework. This reduces the time between “zero” and “Hello World,” allowing researchers to focus on the problem rather than the optimization plumbing.

The library’s ability to handle mixed-variable spaces (continuous, discrete, and categorical) makes it particularly valuable for ML practitioners who need to tune both learning rates (continuous) and the number of layers in a network (discrete) simultaneously.

Key Features

  • Extensive Algorithm Suite: Nevergrad includes a wide range of optimizers, including Differential Evolution, Sequential Quadratic Programming, FastGA, Covariance Matrix Adaptation (CMA-ES), and Particle Swarm Optimization (PSO).
  • Mixed-Variable Parametrization: The instrumentation package allows users to define search spaces that combine continuous scalars, log-distributed variables, integers, and categorical choices, ensuring the optimizer knows exactly how to sample the space.
  • Ask-and-Tell Framework: This architecture allows for asynchronous optimization, where the optimizer suggests a point to evaluate (ask) and the user provides the result (tell), which is essential for long-running ML training jobs.
  • Built-in Benchmarking Tools: The library provides a suite of benchmark functions and routines to evaluate how a specific optimizer performs on a given problem type (e.g., multimodal or noisy functions).
  • Noise Management: Several algorithms within the library are specifically designed to handle noisy objective functions, which is common in ML where validation loss can fluctuate.
  • Parallelization Support: Nevergrad supports parallel evaluation of points, allowing users to leverage multiple workers to speed up the optimization process.

How Nevergrad Compares

Feature Nevergrad Optuna Hyperopt
Primary Focus General Gradient-Free Optimization Hyperparameter Tuning (HPO) Bayesian Optimization
Algorithm Variety Very High (Evolutionary, PSO, etc.) High (TPE, CMA-ES) Moderate (TPE)
Search Space Mixed (Continuous, Discrete, Categorical) Dynamic (Define-by-Run) Static
Noise Handling Dedicated Algorithms Pruning/Early Stopping Standard Bayesian

While Optuna and Hyperopt are specifically tailored for the machine learning hyperparameter tuning workflow (offering features like pruning and trial databases), Nevergrad is a more general-purpose optimization toolbox. It excels when you need a wider variety of evolutionary and swarm-based algorithms that go beyond the Tree-structured Parzen Estimator (TPE) typically used in HPO libraries.

The primary tradeoff is that Nevergrad requires a more explicit definition of the parametrization space up front, whereas Optuna allows for a more dynamic “define-by-run” approach. However, for researchers who need to test the effectiveness of different optimization strategies (e.g., comparing CMA-ES vs. PSO), Nevergrad’s unified interface is superior.

Getting Started: Installation

Nevergrad is a Python 3.8+ library and can be installed through several methods depending on your environment.

Using pip

The simplest way to install the latest stable release is via pip:

pip install nevergrad

Using conda

A conda-forge version is available for those using the Anaconda distribution:

conda install -c conda-forge nevergrad

Installing from Source

If you want to use the latest features from the main branch or contribute to the project, you can install it in editable mode:

git clone https://github.com/facebookresearch/nevergrad.git
pip install -e .

Special Installation Flags

To install the benchmarking tools and test utilities, use the following flags:

pip install nevergrad[benchmark]
pip install nevergrad[all]

How to Use Nevergrad

The core workflow in Nevergrad involves defining an objective function, specifying the parametrization (the search space), and choosing an optimizer to minimize that function.

For a simple problem where the input is a single number or a vector of numbers, you can simply pass an integer to the parametrization argument of the optimizer. This tells Nevergrad that the function takes a vector of size n.

Once the optimizer is initialized, you call the minimize method, which handles the loop of suggesting points, evaluating the function, and updating the internal model of the search space.

Code Examples

Basic Optimization

This example demonstrates how to minimize a simple quadratic function where the optimal value is 0.5.

import nevergrad as ng

def square(x):
    return sum((x - 0.5) ** 2)

# parametrization=2 means the function takes a vector of size 2
optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100)
recommendation = optimizer.minimize(square)

print(f"Best value found: {recommendation.value}")

Complex Parametrization

This example shows how to optimize a function with mixed types: a continuous learning rate, an integer batch size, and a categorical architecture choice.

import nevergrad as ng

def fake_training(learning_rate: float, batch_size: int, architecture: str) -> float:
    # Optimal for learning_rate=0.2, batch_size=4, architecture="conv"
    return (learning_rate - 0.2)**2 + (batch_size - 4)**2 + (0 if architecture == "conv" else 10)

# Define the search space
parametrization = ng.p.Instrumentation(
    learning_rate=ng.p.Log(lower=0.001, upper=1.0),
    batch_size=ng.p.Scalar(lower=1, upper=12).set_integer_casting(),
    architecture=ng.p.Choice(["conv", "fc"])
)

optimizer = ng.optimizers.NGOpt(parametrization=parametrization, budget=100)
recommendation = optimizer.minimize(fake_training)

print(f"Recommended hyperparameters: {recommendation.kwargs}")

Real-World Use Cases

Nevergrad is particularly effective in scenarios where traditional gradient-based optimization is impossible or impractical.

  • Hyperparameter Tuning for Non-Differentiable Models: When tuning parameters for a Random Forest or XGBoost model, the objective function (validation accuracy) is not differentiable. Nevergrad can efficiently search the space of learning rates, tree depths, and regularization strengths.
  • Neural Architecture Search (NAS): In NAS, the “parameters” being optimized are the number of layers, the type of activation function, and the connection patterns. These are discrete choices. Nevergrad’s Choice and Scalar parametrizations allow it to optimize these architectural decisions.
  • Reinforcement Learning (RL) Reward Optimization: In RL, the reward function is often noisy and the environment is a black box. Nevergrad’s noise-resistant algorithms (like TBPSA) can be used to find the optimal weights or hyperparameters for an RL agent.
  • Scientific Engineering: In physics or chemistry simulations where a single evaluation of the objective function takes hours and is computationally expensive, Nevergrad’s efficient sampling strategies reduce the number of evaluations needed to find a global minimum.

Contributing to Nevergrad

The project welcomes contributions from the community. To get started, you can fork the repository and create a branch from main. The project uses black for code formatting to ensure consistency across the codebase.

If you are adding a new optimization algorithm, there are specific guidelines in the adding_an_algorithm.md file in the documentation. For bug reports and feature requests, use the GitHub Issues tracker. All contributors are required to sign the Facebook Contributor License Agreement (CLA) before their pull requests can be merged.

Community and Support

Nevergrad is maintained by Facebook Research and is supported by a community of AI researchers. The primary channel for support is the Nevergrad users Facebook group. For technical discussions and issue tracking, the official GitHub repository is the best place to start.

Detailed documentation is available at the official documentation site, which provides a comprehensive guide to all available optimizers and the parametrization system.

Conclusion

Nevergrad is an essential tool for anyone dealing with “black box” optimization problems. By providing a unified interface to a wide array of gradient-free algorithms, it removes the complexity of implementing these methods from scratch and allows researchers to focus on their actual objective functions.

Whether you are tuning hyperparameters for a deep learning model, optimizing a complex engineering simulation, or searching for the optimal architecture of a neural network, Nevergrad provides the flexibility and flexibility to find the global minimum efficiently.

Star the repo, try the quickstart, and join the community to start optimizing your projects today.

What is Nevergrad and what problem does it solve?

Nevergrad is an open-source Python library for gradient-free optimization developed by Facebook Research. It solves the problem of optimizing complex, non-differentiable, or noisy objective functions where traditional gradient-based methods like SGD cannot be used.

How do I install Nevergrad?

You can install Nevergrad using pip by running pip install nevergrad in your terminal. For conda users, conda install -c conda-forge nevergrad is also available.

How does Nevergrad compare to Optuna?

While Optuna is specifically designed for hyperparameter optimization (HPO) and features dynamic search spaces, Nevergrad is a general-purpose gradient-free optimization toolbox with a broader range of evolutionary and swarm-based algorithms.

Can I use Nevergrad for hyperparameter tuning?

Yes, Nevergrad is widely used for hyperparameter tuning. Its ability to handle mixed-variable spaces (continuous, discrete, and categorical) makes it a powerful alternative to random or grid search for tuning ML models.

What is the 'ask-and-tell' framework in Nevergrad?

The ‘ask-and-tell’ framework is an architecture where the optimizer suggests a point to evaluate (ask) and the user provides the result of the evaluation (tell). This allows for asynchronous and parallel optimization of long-running tasks.

What are the best algorithms for noisy functions?

Nevergrad algorithms like TBPSA are specifically designed to handle noise in the objective function, which is common in reinforcement learning and ML validation sets.

Is Nevergrad open source?

Nevergrad is released under the MIT license, making it available for both commercial and open-source projects.