Skip to main content

Train keras models with genetic algorithms.

Project description

Keras Genetic

Python Tensorflow contributions welcome

Keras Genetic allows you to easily train Keras models using genetic algorithms.

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 where you need to train a specialized controller with <1000 parameters.

Some areas where genetic algorithms are applied today:

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:

  • the Individual
  • the Evaluator
  • the Breeder
  • search()

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 TwoParentMutationBreeder 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:

  • ✅ stabilize the base API
  • ✅ support a callbacks API
  • ✅ end to end MNIST example
  • ✅ end to end CartPole example
  • ✅ implement a ProgBarLogger
  • ✅ implement EarlyStopping callback (can be used in CartPole example)
  • have at least 3 distinct breeders
  • autogenerate documentation
  • thoroughly document each component
  • offer implementations of the most effective genetic algorithms
  • implement unit tests for each component
  • support random seeding
  • thoroughly review the API per Keras core API design guidelines
  • support custom initial populations (i.e. to model after a human imitation model)
  • support keep_probability schedules

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}}
}

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

keras-genetic-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keras_genetic-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file keras-genetic-0.1.0.tar.gz.

File metadata

  • Download URL: keras-genetic-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for keras-genetic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c86eb7914177d30aa01bbc7f3d78454ead63daed885ecaa6a606c2be184b6345
MD5 8a64947adc562ab46f1bfb8128d82b37
BLAKE2b-256 c1e8f9a2437782c03b0ad6898174454448099226ba42b0e917d1467f900c41cf

See more details on using hashes here.

File details

Details for the file keras_genetic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: keras_genetic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for keras_genetic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53d259ee7ff3a58d94249a82001e176494aa06611a8f34b898b8d48d27446a4d
MD5 e012d8005224c0ca8049f3f3d76ddbb5
BLAKE2b-256 958d948e9d7db69cea23d211f63f2f9b8c737ff662e29e695ac71da2a68d7f6d

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