Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yahiakr/kerasga
A simple and easy-to-use implementation of Genetic Algorithm for Keras neural network models.
https://github.com/yahiakr/kerasga
genetic-algorithm keras neural-network
Last synced: 4 months ago
JSON representation
A simple and easy-to-use implementation of Genetic Algorithm for Keras neural network models.
- Host: GitHub
- URL: https://github.com/yahiakr/kerasga
- Owner: yahiakr
- License: gpl-3.0
- Created: 2019-12-13T01:48:26.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-08T23:33:34.000Z (almost 5 years ago)
- Last Synced: 2023-11-20T16:20:17.407Z (about 1 year ago)
- Topics: genetic-algorithm, keras, neural-network
- Language: Python
- Homepage: https://pypi.org/project/KerasGA/
- Size: 24.4 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# KerasGA
A simple and easy-to-use implementation of Genetic Algorithm for Keras NN models in Python.## Features
* Instead of using backpropagation, use Genetic Algorithm to update the parameters/weights of the neural network while training the model.
* Create an initial population (of size: `population_size`) of randomly initialized chromosomes (i.e model weights).
* You can adjust the `selection_rate` & the `mutation_rate`.
* Perform the different GA operations (i.e Selection, Crossover, & Mutation).## Examples
Here are a few projects based on this package:
* [yahiakr/FlappyAI](https://github.com/yahiakr/FlappyAI)
* [yahiakr/SnakeAI](https://github.com/yahiakr/SnakeAI)## Usage
* Install `KerasGA` :
```
$ pip install KerasGA
```* import `GeneticAlgorithm` from `KerasGA` and initiate an object :
```python
from KerasGA import GeneticAlgorithmpopulation_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:
```python
population = GA.initial_population()
```
* To set the wights of a model you can use `.set_weights()` built-in function:
```python
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:
```python
# 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 :)