LukeWood / keras-genetic

18 stars 1 forks source link

Keras Genetic

Python Tensorflow contributions welcome

Keras Genetic allows you to easily train Keras models using genetic algorithms. Genetic algorithms can be used to train small controllers to perform specialized tasks. Below, a genetic algorithm was used to flawlessly solve the CartPole environment. This is an animated GIF not a static image.

Quick Links:

Background

KerasGenetic allows you to leverage the elegent modeling API Keras while performing training with genetic algorithms. Typically, Keras neural network weights are optimized by minimizing a loss function through the process of gradient descent.

Keras Genetic takes a different approach to weight optimization by leveraging genetic algorithms. Genetic algorithms allow you to optimize a neural network without in scenarios where there is no information about the loss landscape.

Genetic algorithms can be used to train neural networks in niche cases to train specialized controllers with <1000 parameters based on a fitness function.

Some areas where genetic algorithms are applied today:

Installation

You can install keras-genetic from PyPi:

pip install keras-genetic

Or directly from GitHub:

pip install git+https://github.com/lukewood/keras-genetic

Overview

The Keras genetic API is quick to get started with, but flexible enough to fit any use case you may come up with.

There are three core components of the API that must be used to get started:

Individual

The Individual class represents an individual in the population.

The most important method on the Individual class is load_model(). load_model() yields a Keras model with the weights stored on the individual class loaded:

model = individual.load_model()
model.predict(some_data)

Evaluator

Next, lets go over the Evaluator. The Evaluator is responsible for determining the strength of an Individual. Perhaps the simplest evaluator is an accuracy evaluator for a classification task:

def evaluate_accuracy(individual: keras_genetic.Individual):
    model = individual.load_model()
    result = model.evaluate(x_train[:100], y_train[:100], return_dict=True, verbose=0)
    return result["accuracy"]

The evaluate_accuracy() function defined above maps from an Individual to an accuracy score. This score can be used to select the individuals that will be used in the next generation.

Breeder

The Breeder is responsible with producing new individuals from a set of parent individuals. The details as to how each Breeder produces new individuals are unique to the breeder, but as a general rule some attributes of the parent are preserved while some new attributes are randomly sampled.

For most users, the MutationBreeder is sufficiently effective.

search()

search() is akin to model.fit() in the core Keras framework. The search() API supports a wide variety of parameters. For an in depth view, browse the API docs.

Here is a sample usage of the search() function:

results = keras_genetic.search(
    model=model,
    # computational cost is evaluate*generations*population_size
    evaluator=evaluate_accuracy,
    generations=10,
    population_size=50,
    n_parents_from_population=5,
    breeder=keras_genetic.breeder.MutationBreeder(),
    return_best=1,
)

Further Reading

Check out the examples and guides (Coming Soon!).

Quickstart

For now, the Cartpole Example serves as the Quickstart guide.

Roadmap

I'd like to accomplish the following tasks:

Feel free to contribute any of these.

Citation

@misc{wood2022kerasgenetic,
    title        = {Keras Genetic},
    author       = {Luke Wood},
    year         = 2022,
    howpublished = {\url{https://github.com/lukewood/keras-genetic}}
}