Coder Social home page Coder Social logo

reddragonnm / neatpy Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 111 KB

A NEAT library in Python

Home Page: https://pypi.org/project/neatpy/

License: MIT License

Python 100.00%
neat neat-python neat-algorithm neuroevolution python3 xor-neural-network

neatpy's Introduction

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 

neatpy's People

Contributors

reddragonnm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

enacting

neatpy's Issues

Net evaluation order inconsistent with connection order

A connection is considered valid if it is from a node to a node with a higher y.
See line 280 in brain.py in _valid_conn:
node1.y <= node2.y

However in evaluation, the nodes are evaluated in the natural list order which by inspection appear to be in .id order, not .y order.
See line 302 in brain.py in predict
for node in self.nodes:

This needs to read something like
for node in sorted(net.nodes,key=lambda node : node .y):

Without this change, randomly it evaluates nodes which have a value still set to zero because the connection goes forward through the list.
I ran into this because in my code I have a forked version of predict which runs the data through the network as a numpy array for accelerated processing and the uninitialized zero value didn't play well with slicing for the relu zeroing. As is, in your version, the library will look like it is working, but there will be nets which fail the evolution not because of a bad connection, but because the signal reads as zero when it shouldn't. You should be able to prove that this is happening by setting the default value to None on line 297 and then asserting it is not None when iterating input connections on line 314.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.