Skip to main content

Diffusion Evolutionary Algorithm

Project description

Diffusion Evolution

Diffusion models are evolutionary algorithms.

Install

clone https://github.com/Zhangyanbo/diffusion-evolution
cd diffevo/
pip install .

Quick Start

For simple experiments, parameter changes may not be necessary. In such cases, we can use the DiffEvo class to simplify the code.

from diffevo import DiffEvo
import torch
from diffevo.examples import two_peak_density, two_peak_density_step

optimizer = DiffEvo(noise=0.1)

xT = torch.randn(512, 2)
x, population_trace, fitness_count = optimizer.optimize(two_peak_density, xT, trace=True)

Output:

100%|██████████| 99/99 [00:00<00:00, 175.58it/s]

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 the DDIMSchedulerCosine, 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffevo-1.0.1.tar.gz (12.0 kB view details)

Uploaded Source

File details

Details for the file diffevo-1.0.1.tar.gz.

File metadata

  • Download URL: diffevo-1.0.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for diffevo-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b26340bfe979c7da4d330eb98bc75c772fd310e5bf6bdec6404ff9f11fd5b5a0
MD5 49f316e617c1a01fcbaa722f9d2910fc
BLAKE2b-256 87f2d3ffd40732fb9f357413a5600381092a9cc8de88b8c7fb985448684f4414

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page