Nevergrad

repository·main·Indexed 26 days ago

https://github.com/facebookresearch/nevergrad

A library for large-scale global optimization (LSGO) providing various algorithms and benchmark functions. It includes implementations of CEC'2013 benchmark functions, tools for noisy and ill-conditioned optimization, and support for discrete objective functions. The library features a modular Optimizer base class for implementing new algorithms and utilities like quasi-randomization for improving diversity in high-dimensional latent variables, such as those used in image generation.

Tokens
9.9K
Snippets
27
Records
54
Agent score
87%

What's inside Nevergrad

  1. Overview of Nevergrad package structure

    main

    Nevergrad is organized into several subpackages corresponding to its core goals:

    • optimization: Implementation of gradient/derivative-free optimization algorithms.
    • parametrization: Tools to specify the parameters (continuous, discrete, or mixed) you want to optimize.
    • functions: Implementations of simple and complex benchmark functions.
    • benchmark: Routines for running experiments and comparing algorithms.
    • common: Shared utility tools used throughout the package.
  2. Use Large-scale global optimization (LSGO) benchmark functions

    main

    Nevergrad provides implementations of benchmark functions for Large-scale global optimization (LSGO) based on the CEC'2013 Special Session and Competition. These functions are useful for evaluating optimization algorithms on high-dimensional problems.

    Note that these implementations aim to reproduce the results of the CPP (C++) implementation rather than the Octave version of the original paper. Specifically:

    • F3, F6, and F10 include Tosz, Tasy, and Lambda parameters (matching CPP).
    • F7 lacks Tasy and Tosz for the side sphere loss to maintain consistency with CPP results.
    • The optimum for F14 is not specified due to computational complexity.
  3. Understand Nevergrad benchmark statistics and criteria

    main

    Nevergrad provides benchmark statistics evaluated across different optimization criteria. When analyzing performance, note that these statistics exclude 'wizards' (adaptive methods) to compare individual methods from specific categories like Bayesian Optimization, Evolutionary Computation, Direct Search, Particle Swarm, Differential Evolution, and Math. Programming.

    Key evaluation criteria used in these benchmarks include:

    • Simple Regret Criterion: Measures how often an algorithm performs best in terms of the absolute minimum value reached.
    • Robustness Criterion: Measures the frequency at which a method outperforms others across different benchmarks, rather than just the absolute regret.
    • Top-3 Frequency: Measures how often a method ranks in the top three performers, indicating its ability to handle diverse contexts (dimensions, budgets, types, and parallelism).
  4. Optimize parameters for reinforcement learning

    main

    When optimizing parameters for reinforcement learning (RL), the environment is often noisy. Instead of manually averaging evaluations over multiple episodes, you should allow the optimizer to handle re-evaluations. TBPSA (and its variants like NaiveTBPSA) is a strong candidate for these noisy, parameter-tuning tasks because it is based on population-control mechanisms.

    To implement this, use the ask() and tell() pattern. This allows for asynchronous execution where you can request multiple parameter sets (ask()), evaluate them (e.g., running a simulation), and then report the results back to the optimizer (tell()).

    import nevergrad as ng
    import numpy as np
    
    def simulate_and_return_test_error_with_rl(x, noisy=True):
        return np.linalg.norm([int(50. * abs(x_ - 0.2)) for x_ in x]) + noisy * len(x) * np.random.normal()
    
    budget = 1200
    # TBPSA is recommended for noisy RL parameter optimization
    optim = ng.optimizers.registry["TBPSA"](parametrization=300, budget=budget)
    
    for u in range(budget // 3):
        # Ask for parameters
        x1 = optim.ask()
        x2 = optim.ask()
        x3 = optim.ask()
        
        # Evaluate (these can be parallelized)
        y1 = simulate_and_return_test_error_with_rl(*x1.args)
        y2 = simulate_and_return_test_error_with_rl(*x2.args)
        y3 = simulate_and_return_test_error_with_rl(*x3.args)
        
        # Tell the results back to the optimizer
        optim.tell(x1, y1)
        optim.tell(x2, y2)
        optim.tell(x3, y3)
    
    recommendation = optim.recommend()
    print("Best parameters:", recommendation.args)
  5. Add custom experiments via the import system

    main

    You can define your own experiments, functions, and optimizers in an external module and import them into the benchmark runner using the --imports flag. Note: This system does not currently work on Windows.

    Example usage:

    python -m nevergrad.benchmark additional_experiment --imports=path/to/your_module.py
    python -m nevergrad.benchmark additional_experiment --imports=nevergrad/benchmark/additional/example.py
  6. Contribute to nevergrad via Pull Request

    main

    To contribute changes to the repository using a fork and a pull request:

    1. Create a Branch: In your PowerShell prompt (within the nevergrad directory), create a new branch:
      git checkout -b <branch_name>
    2. **Stage Changes**: Add your modified files:
     ```bash
    git add <file_path>
    1. Commit: Commit your changes with a descriptive message:
      git commit -am "<commit_message>"
    4. **Push**: Push the branch to your fork:
     ```bash
    git push --set-upstream origin <branch_name>
    1. Open PR: Navigate to the original facebookresearch/nevergrad repository on GitHub. GitHub will detect your recent push and prompt you to Compare & pull request. Click the button, review your changes, and click Create pull request.
    git checkout -b windowsDoc
    git add .\docs\windows.md
    git commit -am "windows documentation"
    git push --set-upstream origin windowsDoc
  7. Install Spyder IDE in Anaconda

    main

    Spyder is a Python IDE that integrates well with Anaconda. To install it for nevergrad:

    1. Open Anaconda Navigator.
    2. Select your nevergrad environment in the "application on" dropdown menu.
    3. Locate Spyder in the application list and click the Install button.
  8. Run Nevergrad benchmarks via CLI

    main

    You can run various optimization benchmarks using the nevergrad.benchmark module. To run the examples provided in the documentation, you must ensure that nevergrad was installed with the benchmark flag enabled.

    Common CLI flags used in benchmarks:

    • --seed: Sets the random seed for reproducibility.
    • --repetitions: Number of independent experiments to run.
    • --plot: Generates plots for the results.
  9. Install Nevergrad with benchmark dependencies

    main

    To use the benchmarking tools, you must install Nevergrad with the benchmark or all extra, as the base installation does not include all required packages for running experiments.

    pip install 'nevergrad[benchmark]'
  10. Install nevergrad on Windows using Anaconda

    main

    To set up a dedicated environment for nevergrad on Windows, use Anaconda Navigator to create a new environment and install the necessary dependencies via PowerShell.

    1. Install Anaconda: Download and install Anaconda for Windows from the official site.
    2. Create Environment: Open Anaconda Navigator, go to the Environments tab, and click Create to make a new environment for nevergrad.
    3. Setup Terminal: In the Navigator Home screen, ensure your new environment is selected in the "application on" menu. Install and launch Powershell Prompt.
    4. Clone Repository: In the PowerShell prompt, create a directory and clone the repository:
      mkdir repos
      cd repos
      git clone https://github.com/facebookresearch/nevergrad.git
    5. Install Dependencies: Navigate to the requirements folder and install the necessary packages:
      cd nevergrad/requirements/
      pip install -r main.txt
      pip install -r bench.txt
      pip install -r dev.txt
      conda install pytorch
      cd ..
    mkdir repos 
    cd repos
    git clone https://github.com/facebookresearch/nevergrad.git
    
    cd nevergrad/requirements/
    pip install -r main.txt
    pip install -r bench.txt
    pip install -r dev.txt
    conda install pytorch
    cd ..