Giter VIP home page Giter VIP logo

differential-evolution's Introduction

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 [email protected]: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

differential-evolution's People

Contributors

jannisko avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.