Diffusion Evolutionary Algorithm
Project description
Diffusion Evolution
This repo is for our preprint Diffusion models are evolutionary algorithms, which anayatically proves that diffusion models are a type of evolutionary algorithm. This equivalence allows us to leverage advancements in diffusion models for evolutionary algorithm tasks, including accelerated sampling and latent space diffusion.
The Diffusion Evolution framework treats inversed diffusion as evolutionary algorithm, where the population estimates its added noise (or their noise-free states) based on its neighbors' fitness then evolves via denoising. The following figure shows the process on optimizing a two-peak density function. The Diffusion Evolution initially has large neighbor range (shown as blue disk), calculating $x_0$ based on the fitness of its neighbors then move toward estimated $x_0$.
Install
You can install the package via pip:
pip install diffevo
or manually install:
clone https://github.com/Zhangyanbo/diffusion-evolution
cd diffevo/
pip install .
Some benchmark codes requires dependencies, can be installed via:
pip install cma gym pygame tqdm matplotlib numpy==1.26.4
Also Pytorch version 2.5 or above is required
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
The benchmark fitness functions can be found here: https://github.com/bhartl/foobench
Typical Usage
In most cases, tuning hyperparameters or adding custom operations is necessary to achieve higher performance. We recommend using the following form for the best balance between conciseness and versatility.
from diffevo import DDIMScheduler, BayesianGenerator
from diffevo.examples import two_peak_density
scheduler = DDIMScheduler(num_step=100)
x = torch.randn(512, 2)
for t, alpha in scheduler:
fitness = two_peak_density(x, std=0.25)
generator = BayesianGenerator(x, fitness, alpha)
x = generator(noise=0)
The following are two evolution trajectories of different fitness functions.
Advanced Usage
We also offer multiple choices for each component to accommodate more advanced use cases:
- In addition to the
DDIMScheduler, we provide theDDIMSchedulerCosine, which features a different $\alpha$ scheduler. - We offer multiple fitness mapping functions that map the original fitness to a different value. These can be found in
diffevo.fitnessmapping. - Currently, we have only one version of the generator.
Below is an example of how to change the diffusion process and conduct advanced experiments:
import torch
from diffevo import DDIMScheduler, BayesianGenerator, DDIMSchedulerCosine
from diffevo.examples import two_peak_density
from diffevo.fitnessmapping import Power, Energy, Identity
scheduler = DDIMSchedulerCosine(num_step=100) # use a different scheduler
x = torch.randn(512, 2)
trace = [] # store the trace of the population
mapping_fn = Power(3) # setup the power mapping function
for t, alpha in scheduler:
fitness = two_peak_density(x, std=0.25)
# apply the power mapping function
generator = BayesianGenerator(x, mapping_fn(fitness), alpha)
x = generator(noise=0.1)
trace.append(x)
trace = torch.stack(trace)
Cite our work
@misc{zhang2024diffusionmodelsevolutionaryalgorithms,
title={Diffusion Models are Evolutionary Algorithms},
author={Yanbo Zhang and Benedikt Hartl and Hananel Hazan and Michael Levin},
year={2024},
eprint={2410.02543},
archivePrefix={arXiv},
primaryClass={cs.NE},
url={https://arxiv.org/abs/2410.02543},
}
License
Our software is relased under modified Apache 2.0 License. We allow non-commercial usage for research, study, learning, etc., while limiting the commercial usage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file diffevo-1.1.0.tar.gz.
File metadata
- Download URL: diffevo-1.1.0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5313d6121e787daf31bbf53550a465b0fc0256da78d8fa4615d2bb84d1798d09
|
|
| MD5 |
42ea226cee259b9ef907d9210e2a9601
|
|
| BLAKE2b-256 |
3342581a6c053ff5f6c9df87a0e2d0345cb5eeb5887afbdccd29bd114e8dd423
|
File details
Details for the file diffevo-1.1.0-py3-none-any.whl.
File metadata
- Download URL: diffevo-1.1.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38e233909ab6ed09c84ac4ebb440482f9af345aa8977a864b7d90d01bfcbe2a4
|
|
| MD5 |
64e7b845fdc092dc07aee21e4c8672eb
|
|
| BLAKE2b-256 |
e406dcbc802c1592d2cd59f455de0972ff0315d6c6f611ac855e3347c28ac68e
|