• Stars
    star
    475
  • Rank 92,465 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Evolution Strategies in JAX 🦎

evosax: JAX-Based Evolution Strategies 🦎

Pyversions PyPI version Code style: black codecov Paper

Tired of having to handle asynchronous processes for neuroevolution? Do you want to leverage massive vectorization and high-throughput accelerators for evolution strategies (ES)? evosax allows you to leverage JAX, XLA compilation and auto-vectorization/parallelization to scale ES to your favorite accelerators. The API is based on the classical ask, evaluate, tell cycle of ES. Both ask and tell calls are compatible with jit, vmap/pmap and lax.scan. It includes a vast set of both classic (e.g. CMA-ES, Differential Evolution, etc.) and modern neuroevolution (e.g. OpenAI-ES, Augmented RS, etc.) strategies. You can get started here πŸ‘‰ Colab

Basic evosax API Usage 🍲

import jax
from evosax import CMA_ES

# Instantiate the search strategy
rng = jax.random.PRNGKey(0)
strategy = CMA_ES(popsize=20, num_dims=2, elite_ratio=0.5)
es_params = strategy.default_params
state = strategy.initialize(rng, es_params)

# Run ask-eval-tell loop - NOTE: By default minimization!
for t in range(num_generations):
    rng, rng_gen, rng_eval = jax.random.split(rng, 3)
    x, state = strategy.ask(rng_gen, state, es_params)
    fitness = ...  # Your population evaluation fct 
    state = strategy.tell(x, fitness, state, es_params)

# Get best overall population member & its fitness
state.best_member, state.best_fitness

Implemented Evolution Strategies 🦎

Strategy Reference Import Example
OpenAI-ES Salimans et al. (2017) OpenES Colab
PGPE Sehnke et al. (2010) PGPE Colab
ARS Mania et al. (2018) ARS Colab
ESMC Merchant et al. (2021) ESMC Colab
Persistent ES Vicol et al. (2021) PersistentES Colab
Noise-Reuse ES Li et al. (2023) NoiseReuseES Colab
xNES Wierstra et al. (2014) XNES Colab
SNES Wierstra et al. (2014) SNES Colab
CR-FM-NES Nomura & Ono (2022) CR_FM_NES Colab
Guided ES Maheswaranathan et al. (2018) GuidedES Colab
ASEBO Choromanski et al. (2019) ASEBO Colab
CMA-ES Hansen & Ostermeier (2001) CMA_ES Colab
Sep-CMA-ES Ros & Hansen (2008) Sep_CMA_ES Colab
BIPOP-CMA-ES Hansen (2009) BIPOP_CMA_ES Colab
IPOP-CMA-ES Auer & Hansen (2005) IPOP_CMA_ES Colab
Full-iAMaLGaM Bosman et al. (2013) Full_iAMaLGaM Colab
Independent-iAMaLGaM Bosman et al. (2013) Indep_iAMaLGaM Colab
MA-ES Bayer & Sendhoff (2017) MA_ES Colab
LM-MA-ES Loshchilov et al. (2017) LM_MA_ES Colab
RmES Li & Zhang (2017) RmES Colab
Simple Genetic Such et al. (2017) SimpleGA Colab
SAMR-GA Clune et al. (2008) SAMR_GA Colab
GESMR-GA Kumar et al. (2022) GESMR_GA Colab
MR15-GA Rechenberg (1978) MR15_GA Colab
LGA Lange et al. (2023b) LGA Colab
Simple Gaussian Rechenberg (1978) SimpleES Colab
DES Lange et al. (2023a) DES Colab
LES Lange et al. (2023a) LES Colab
Particle Swarm Optimization Kennedy & Eberhart (1995) PSO Colab
Differential Evolution Storn & Price (1997) DE Colab
GLD Golovin et al. (2019) GLD Colab
Simulated Annealing Rasdi Rere et al. (2015) SimAnneal Colab
Population-Based Training Jaderberg et al. (2017) PBT Colab
Random Search Bergstra & Bengio (2012) RandomSearch Colab

Installation ⏳

The latest evosax release can directly be installed from PyPI:

pip install evosax

If you want to get the most recent commit, please install directly from the repository:

pip install git+https://github.com/RobertTLange/evosax.git@main

In order to use JAX on your accelerators, you can find more details in the JAX documentation.

Examples πŸ“–

Key Features πŸ’΅

  • Strategy Diversity: evosax implements more than 30 classical and modern neuroevolution strategies. All of them follow the same simple ask/eval API and come with tailored tools such as the ClipUp optimizer, parameter reshaping into PyTrees and fitness shaping (see below).

  • Vectorization/Parallelization of ask/tell Calls: Both ask and tell calls can leverage jit, vmap/pmap. This enables vectorized/parallel rollouts of different evolution strategies.

from evosax.strategies.ars import ARS, EvoParams
# E.g. vectorize over different initial perturbation stds
strategy = ARS(popsize=100, num_dims=20)
es_params = EvoParams(sigma_init=jnp.array([0.1, 0.01, 0.001]), sigma_decay=0.999, ...)

# Specify how to map over ES hyperparameters 
map_dict = EvoParams(sigma_init=0, sigma_decay=None, ...)

# Vmap-composed batch initialize, ask and tell functions 
batch_init = jax.vmap(strategy.init, in_axes=(None, map_dict))
batch_ask = jax.vmap(strategy.ask, in_axes=(None, 0, map_dict))
batch_tell = jax.vmap(strategy.tell, in_axes=(0, 0, 0, map_dict))
  • Scan Through Evolution Rollouts: You can also lax.scan through entire init, ask, eval, tell loops for fast compilation of ES loops:
@partial(jax.jit, static_argnums=(1,))
def run_es_loop(rng, num_steps):
    """Run evolution ask-eval-tell loop."""
    es_params = strategy.default_params
    state = strategy.initialize(rng, es_params)

    def es_step(state_input, tmp):
        """Helper es step to lax.scan through."""
        rng, state = state_input
        rng, rng_iter = jax.random.split(rng)
        x, state = strategy.ask(rng_iter, state, es_params)
        fitness = ...
        state = strategy.tell(y, fitness, state, es_params)
        return [rng, state], fitness[jnp.argmin(fitness)]

    _, scan_out = jax.lax.scan(es_step,
                               [rng, state],
                               [jnp.zeros(num_steps)])
    return jnp.min(scan_out)
  • Population Parameter Reshaping: We provide a ParamaterReshaper wrapper to reshape flat parameter vectors into PyTrees. The wrapper is compatible with JAX neural network libraries such as Flax/Haiku and makes it easier to afterwards evaluate network populations.
from flax import linen as nn
from evosax import ParameterReshaper

class MLP(nn.Module):
    num_hidden_units: int
    ...

    @nn.compact
    def __call__(self, obs):
        ...
        return ...

network = MLP(64)
net_params = network.init(rng, jnp.zeros(4,), rng)

# Initialize reshaper based on placeholder network shapes
param_reshaper = ParameterReshaper(net_params)

# Get population candidates & reshape into stacked pytrees
x = strategy.ask(...)
x_shaped = param_reshaper.reshape(x)
  • Flexible Fitness Shaping: By default evosax assumes that the fitness objective is to be minimized. If you would like to maximize instead, perform rank centering, z-scoring or add weight regularization you can use the FitnessShaper:
from evosax import FitnessShaper

# Instantiate jittable fitness shaper (e.g. for Open ES)
fit_shaper = FitnessShaper(centered_rank=True,
                           z_score=False,
                           weight_decay=0.01,
                           maximize=True)

# Shape the evaluated fitness scores
fit_shaped = fit_shaper.apply(x, fitness) 
Additonal Work-In-Progress **Strategy Restart Wrappers**: *Work-in-progress*. You can also choose from a set of different restart mechanisms, which will relaunch a strategy (with e.g. new population size) based on termination criteria. Note: For all restart strategies which alter the population size the ask and tell methods will have to be re-compiled at the time of change. Note that all strategies can also be executed without explicitly providing `es_params`. In this case the default parameters will be used.
```Python
from evosax import CMA_ES
from evosax.restarts import BIPOP_Restarter

# Define a termination criterion (kwargs - fitness, state, params)
def std_criterion(fitness, state, params):
    """Restart strategy if fitness std across population is small."""
    return fitness.std() < 0.001

# Instantiate Base CMA-ES & wrap with BIPOP restarts
# Pass strategy-specific kwargs separately (e.g. elite_ration or opt_name)
strategy = CMA_ES(num_dims, popsize, elite_ratio)
re_strategy = BIPOP_Restarter(
                strategy,
                stop_criteria=[std_criterion],
                strategy_kwargs={"elite_ratio": elite_ratio}
            )
state = re_strategy.initialize(rng)

# ask/tell loop - restarts are automatically handled 
rng, rng_gen, rng_eval = jax.random.split(rng, 3)
x, state = re_strategy.ask(rng_gen, state)
fitness = ...  # Your population evaluation fct 
state = re_strategy.tell(x, fitness, state)
```

- **Batch Strategy Rollouts**: *Work-in-progress*. We are currently also working on different ways of incorporating multiple subpopulations with different communication protocols.

```Python
from evosax.experimental.subpops import BatchStrategy

# Instantiates 5 CMA-ES subpops of 20 members
strategy = BatchStrategy(
        strategy_name="CMA_ES",
        num_dims=4096,
        popsize=100,
        num_subpops=5,
        strategy_kwargs={"elite_ratio": 0.5},
        communication="best_subpop",
    )

state = strategy.initialize(rng)
# Ask for evaluation candidates of different subpopulation ES
x, state = strategy.ask(rng_iter, state)
fitness = ...
state = strategy.tell(x, fitness, state)
```

- **Indirect Encodings**: *Work-in-progress*. ES can struggle with high-dimensional search spaces (e.g. due to harder estimation of covariances). One potential way to alleviate this challenge, is to use indirect parameter encodings in a lower dimensional space. So far we provide JAX-compatible encodings with random projections (Gaussian/Rademacher) and Hypernetworks for MLPs. They act as drop-in replacements for the `ParameterReshaper`:

```Python
from evosax.experimental.decodings import RandomDecoder, HyperDecoder

# For arbitrary network architectures / search spaces
num_encoding_dims = 6
param_reshaper = RandomDecoder(num_encoding_dims, net_params)
x_shaped = param_reshaper.reshape(x)

# For MLP-based models we also support a HyperNetwork en/decoding
reshaper = HyperDecoder(
        net_params,
        hypernet_config={
            "num_latent_units": 3,  # Latent units per module kernel/bias
            "num_hidden_units": 2,  # Hidden dimensionality of a_i^j embedding
        },
    )
x_shaped = param_reshaper.reshape(x)
```

Resources & Other Great JAX-ES Tools πŸ“

  • πŸ“Ί Rob's MLC Research Jam Talk: Small motivation talk at the ML Collective Research Jam.
  • πŸ“ Rob's 02/2021 Blog: Tutorial on CMA-ES & leveraging JAX's primitives.
  • πŸ’» Evojax: JAX-ES library by Google Brain with great rollout wrappers.
  • πŸ’» QDax: Quality-Diversity algorithms in JAX.

Acknowledgements & Citing evosax ✏️

If you use evosax in your research, please cite the following paper:

@article{evosax2022github,
  author = {Robert Tjarko Lange},
  title = {evosax: JAX-based Evolution Strategies},
  journal={arXiv preprint arXiv:2212.04180},
  year = {2022},
}

We acknowledge financial support by the Google TRC and the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) under Germany's Excellence Strategy - EXC 2002/1 "Science of Intelligence" - project number 390523135.

Development πŸ‘·

You can run the test suite via python -m pytest -vv --all. If you find a bug or are missing your favourite feature, feel free to create an issue and/or start contributing πŸ€—.

Disclaimer ⚠️

This repository contains an independent reimplementation of LES and DES based on the corresponding ICLR 2023 publication (Lange et al., 2023). It is unrelated to Google or DeepMind. The implementation has been tested to roughly reproduce the official results on a range of tasks.

More Repositories

1

gymnax

RL Environments in JAX 🌍
Python
630
star
2

code-and-blog

Some small scale experiments for my blog posts πŸ“
Jupyter Notebook
78
star
3

gymnax-blines

Baselines for gymnax πŸ€–
Jupyter Notebook
57
star
4

reading-notes-ml

Progress, Notes, Summaries and a lot of Questions on Machine Learning
55
star
5

spinningup-workspace

Reading notes & PyTorch experiments on OpenAI's "Spinning Up in DRL" tutorial.
Python
36
star
6

deep-rl-tutorial

A Tutorial on Deep Reinforcement Learning in PyTorch
Jupyter Notebook
29
star
7

flexible-learning-group

A curated list of papers presented in the πŸ“–"Flexible Learning Reading Group" @ TU Berlin. Join us! πŸ€—
27
star
8

es-lottery

Lottery Tickets in Evolutionary Optimization (Lange & Sprekeler, ICML 2023)
Jupyter Notebook
13
star
9

minimal-meta-rl

Minimal A2C/A3C example of an LSTM-based meta-learner.
Python
13
star
10

gym-hanoi

A Towers of Hanoi environment in OpenAI Gym Style
Python
12
star
11

SequentialBayesianLearning

Sequential Bayesian Learning Agents learning data-generating process of binary sequence.
Jupyter Notebook
11
star
12

automata-perturbation-lstm

Code accompanying GΓ³mez-Nava et al. (2023, Nature Physics)
Python
10
star
13

gym-swarm

A Swarm environment in OpenAI gym style
Jupyter Notebook
8
star
14

StochVol_HMM

Stochastic Volatility Modelling using Hidden Markov Model
Jupyter Notebook
6
star
15

algonauts-2021

Algonauts 2021 Challenge Mini-Track submission based on SimCLR-v2 features & Bayesian Optimization
Jupyter Notebook
6
star
16

action-grammars-hrl

Action Grammars for Hierarchical Reinforcement Learning
Jupyter Notebook
5
star
17

Reading_Notes_Neuro

Progress, Notes, Summaries and a lot of Questions on Neuroscience
5
star
18

evojax-benchmarks

Benchmarking Utilities for EvoJAX
Jupyter Notebook
4
star
19

Bio-Plausible-DeepLearning

Who needs backprop? - Guerguiev et al (2017) - Reproduction & Robustness Checks
Jupyter Notebook
3
star
20

RobertTLange

2
star
21

ModelsNeuralSystems

Computer Practical Coursework for BCCN Berlin course "Models of Neural Systems" (2018/2019)
Jupyter Notebook
2
star
22

Migration_and_Technology_Diffusion

Bachelor Thesis tackling the Impact of Skilled-Worker Immigration on the Diffusion of New Technologies
Stata
1
star
23

RandGLM

RandNLA methods for estimating GLMs with Big Datasets
R
1
star