Skip to content

A (very slow) implementation of the differential evolution algorithm in Python using Tensorflow.

License

Notifications You must be signed in to change notification settings

jannisko/differential-evolution

Repository files navigation

Differential Evolution

A (very slow) implementation of the differential evolution algorithm in Python using low level Tensorflow functions.

gif of a quadratic model being optimized gif of the ackley loss being optimized

Higher quality versions of these animations can be found under Releases.

Motivation

I started this project as an assignment for my Tensorflow class. I am sharing this to help anyone with a similar assignment.

At the moment my code is not using Tensorflow to its strenghts, so execution times are very long.

Getting Started

git clone git@github.com:jannisko/differential-evolution.git
cd differential-evolution
pip install -r requirements.txt

Usage

import tensorflow as tf

from differential_evolution import DifferentialEvolution
from differential_evolution.losses import create_huber
from differential_evolution.models import create_quadratic

# define original data
x = [1,2,3,4,5,6,7,8]
y = [1,4,9,16,25,36,49,64]

# define initial variables for the model
a = tf.Variable(0.0)
b = tf.Variable(0.0)
c = tf.Variable(0.0)

model = create_quadratic(x, a, b, c)

loss = create_huber(model, y)

opt = DifferentialEvolution(loss, [a,b,c])

for _ in range(100):
    opt.next_generation()

point, loss = opt.get_best_point()

Using your own model

x = np.linspace(0,9,10)
y = np.linspace(0,9,10)**2

a = tf.Variable(0.0)
b = tf.Variable(0.0)
c = tf.Variable(0.0)

def logistic_model():
    return c / (1 + a * tf.math.exp(-b * x))

loss = create_huber(logistic_model, y)

Creating animations

create_animation.py contains a few examples of how to animate your model or loss function. To output as .mp4 you will need ffmpeg. For .gif you will need imagemagik.

  • animate_models() was used for the 2d animation of a quadratic function.
  • animate_ackley() was used for the 3d animation of the ackley function.

Troubleshooting

License

This project is licensed under the MIT License - see the LICENSE file for details

About

A (very slow) implementation of the differential evolution algorithm in Python using Tensorflow.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages