A simple and easy-to-use implementation of Genetic Algorithm for Keras NN models in Python.
Project description
KerasGA
A simple and easy-to-use implementation of Genetic Algorithm for Keras NN models in Python.
Features
- create an initial population (of size:
population_size) of randomly initialized chromosomes (i.e model weights). - You can adjust the
selection_rate& themutation_rate. - Perform the different GA operations (i.e Selection, Crossover, & Mutation).
Examples
Here are a few projects based on this package:
Usage
- Install
KerasGA:
$ pip install KerasGA
- import
GeneticAlgorithmfromKerasGAand initiate an object :
from KerasGA import GeneticAlgorithm
population_size = 10
GA = GeneticAlgorithm(model, population_size = population_size, selection_rate = 0.1, mutation_rate = 0.2)
PS: model is a Keras model.
- Generate the initial population:
population = GA.initial_population()
- To set the wights of a model you can use
.set_weights()built-in function:
for chromosome in population:
model.set_weights(chromosome)
# then evaluate the chromosome (i.e assign its final score)
- After calculating the scores for each chromosome, it's time to select the top-performers:
# Selection:
# 'scores' is a list of length = population_size
# 'top_performers' is a list of tuples: (chromosome, it's score)
top_performers = GA.strongest_parents(population,scores)
# Make pairs:
# 'GA.pair' return a tuple of type: (chromosome, it's score)
pairs = []
while len(pairs) != GA.population_size:
pairs.append( GA.pair(top_performers) )
# Crossover:
base_offsprings = []
for pair in pairs:
offsprings = GA.crossover(pair[0][0], pair[1][0])
# 'offsprings' contains two chromosomes
base_offsprings.append(offsprings[-1])
# Mutation:
new_population = GA.mutation(base_offsprings)
And that's it :)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
KerasGA-1.0.0.tar.gz
(3.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
KerasGA-1.0.0-py3-none-any.whl
(16.5 kB
view details)
File details
Details for the file KerasGA-1.0.0.tar.gz.
File metadata
- Download URL: KerasGA-1.0.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6389a07626e7758ad4cccd57dd63c00c52b0e7ae6584e32446937bd3e8ddd364
|
|
| MD5 |
9b5cf9ecac22f89fa5bdb65af3de6c51
|
|
| BLAKE2b-256 |
8ca6d2e64b8a9347d5dda712b617da5c72eb6e106d9be9cd5b2dca916e15e7f0
|
File details
Details for the file KerasGA-1.0.0-py3-none-any.whl.
File metadata
- Download URL: KerasGA-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23328ecd82987ade9a32ec297358b6385a90fe48baa44ffcb262f05852164894
|
|
| MD5 |
75f5e11adaa2d80ffbdfc5f70a982da7
|
|
| BLAKE2b-256 |
ad98c306410360532b70d968e9ca3d9974a14221be2a9ca1f179a33f2787f0be
|