A NEAT library in Python
Project description
neatpy
neatpy is a library that implements the NEAT algorithm designed by Kenneth O. Stanley which is documented in this paper. This method evolves neural network topologies along with weights on the foundation of a genetic algorithm.
Why neatpy?
Some reasons to use neatpy:
- Easy to use
- Code is easier to understand
- Can be translated to other languages without much difficulty
- Little to no knowledge of configuration is required to get started
Installation
pip install neatpy
Basic XOR example
from neatpy.options import Options # import options class for configuration
from neatpy.population import Population # import population class
# XOR inputs and outputs
xor_inp = [(0,0), (0,1), (1,0), (1,1)]
xor_out = [0, 1, 1, 0]
# evaluate function
def evaluate(brains):
for nn in brains:
nn.fitness = 4
for xi, xo in zip(xor_inp, xor_out):
output = nn.predict(xi)[0]
nn.fitness -= (output - xo) ** 2
# number of inputs - number of outputs - population size - maximum fitness needed
Options.set_options(2, 1, 150, 3.9)
p = Population()
best, solved = p.evaluate(evaluate, 400) # evaluating population for 400 generations or till maximum fitness is reached
# testing the best neural network
evaluate([best])
print(best.fitness)
Notes
- For every environment an
evaluate
function needs to be created that takes every generation's population - To customize the algorithm even further, optional arguments in
Options.set_options
can be tweaked - When the evaluation ends the method returns the best brain and whether the environment was solved (maximum fitness reached) or not
XOR Loose example
Imitating the evaluate method in Population() we can write the above code as:
from neatpy.options import Options
from neatpy.population import Population
Options.set_options(2, 1, 150)
p = Population()
xor_inp = [(0,0), (0,1), (1,0), (1,1)]
xor_out = [0, 1, 1, 0]
max_fitness = 3.9 # maximum fitness
# while the maximum fitness hasn't been reached
while p.best.fitness < max_fitness:
for nn in p.pool:
nn.fitness = 4
for xi, xo in zip(xor_inp, xor_out):
output = nn.predict(xi)[0]
nn.fitness -= (output - xo) ** 2
p.epoch() # create a new pool
print(p) # __str__ method gives the statistics
best = p.best # best brain
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
neatpy-0.5.0.tar.gz
(11.9 kB
view details)
Built Distribution
neatpy-0.5.0-py3-none-any.whl
(13.7 kB
view details)
File details
Details for the file neatpy-0.5.0.tar.gz
.
File metadata
- Download URL: neatpy-0.5.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 984d5c83274f523d5610ece5b94227bb501d7eeb8c3247073eafe672aa75f0fd |
|
MD5 | 3b32368ad0db1a3a74ad3f4d042755ae |
|
BLAKE2b-256 | 329263b946ddd742d0526b5764c522fcd5b2a0a450150499bbbd5a1d74962f68 |
File details
Details for the file neatpy-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: neatpy-0.5.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c8a5537cf8c10d87c49818beec1a689dde957174ee1d9b2e06812fb3d502ddd |
|
MD5 | cd3cfde9d490f7ed1d2f6bd2c673d005 |
|
BLAKE2b-256 | 9120aa14699c94f87d33f4a5e21f1eccc12f249022fa893247e4d01a3b2dc750 |