Coder Social home page Coder Social logo

ddehueck / pytorch-neat Goto Github PK

View Code? Open in Web Editor NEW
101.0 7.0 31.0 86 KB

A pytorch implementation of the NEAT (NeuroEvolution of Augmenting Topologies) algorithm

License: MIT License

Python 100.00%
neuroevolution pytorch neat neural-networks genetic-algorithms

pytorch-neat's Introduction

PyTorch-NEAT

A PyTorch implementation of the NEAT (NeuroEvolution of Augmenting Topologies) method which was originally created by Kenneth O. Stanley as a principled approach to evolving neural networks. Read the paper here.

Experiments

PyTorch-NEAT currently contains three built-in experiments: XOR, Single-Pole Balancing, and Car Mountain Climbing.

XOR Experiment

Run with the command: python xor_run.py Will run up-to 150 generations with an initial population of 150 genomes. When/If a solution is found the solution network will be displayed along with statistics about the trial. Feel free to run for more than one trial - just increase the range in the outer for loop in the xor_run.py file.

Single Pole Balancing

Run with the command: python pole_run.py Will run up-to 150 generations with an initial population of 150 genomes. Runs in the OpenAI gym enviornment. When/If a solution is found the solution network will be displayed along with a rendered evalution in the OpenAI gym.

Car Mountain Climbing Experiment

Run with the command: python mountain_climb_run.py Will run up-to 150 generations with an initial population of 150 genomes. Runs in the OpenAI gym enviornment. When/If a solution is found the solution network will be displayed along with a rendered evalution in the OpenAI gym.

An Experiment's Configuration File

Each experiment requries a configuration file. The XOR experiment config file is broken down here:

Import necessary items.

import torch
import torch.nn as nn
from torch import autograd
from v1.phenotype.feed_forward import FeedForwardNet

A config file consists of a Python class with certain requirnments (detailed in comments below).

class XORConfig:
    # Where to evaluate tensors
    DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    # Boolean - print generation stats throughout trial
    VERBOSE = False

    # Number of inputs/outputs each genome should contain
    NUM_INPUTS = 2
    NUM_OUTPUTS = 1
    # Boolean - use a bias node in each genome
    USE_BIAS = True
    
    # String - which activation function each node will use
    # Note: currently only sigmoid and tanh are available - see v1/activations.py for functions
    ACTIVATION = 'sigmoid'
    # Float - what value to scale the activation function's input by
    # This default value is taken directly from the paper
    SCALE_ACTIVATION = 4.9
    
    # Float - a solution is defined as having a fitness >= this fitness threshold
    FITNESS_THRESHOLD = 3.9

    # Integer - size of population
    POPULATION_SIZE = 150
    # Integer - max number of generations to be run for
    NUMBER_OF_GENERATIONS = 150
    # Float - an organism is said to be in a species if the genome distance to the model genome of a species is <= this speciation threshold
    SPECIATION_THRESHOLD = 3.0

    # Float between 0.0 and 1.0 - rate at which a connection gene will be mutated
    CONNECTION_MUTATION_RATE = 0.80
    # Float between 0.0 and 1.0 - rate at which a connections weight is perturbed (if connection is to be mutated) 
    CONNECTION_PERTURBATION_RATE = 0.90
    # Float between 0.0 and 1.0 - rate at which a node will randomly be added to a genome
    ADD_NODE_MUTATION_RATE = 0.03
    # Float between 0.0 and 1.0 - rate at which a connection will randomly be added to a genome
    ADD_CONNECTION_MUTATION_RATE = 0.5
    
    # Float between 0.0 and 1.0 - rate at which a connection, if disabled, will be re-enabled
    CROSSOVER_REENABLE_CONNECTION_GENE_RATE = 0.25

    # Float between 0.0 and 1.0 - Top percentage of species to be saved before mating
    PERCENTAGE_TO_SAVE = 0.30
    
    # XOR's input and output values
    # Note: it is not always necessary to explicity include these values. Depends on the fitness evaluation.
    # See an OpenAI gym experiment config file for a different fitness evaluation example.
    inputs = list(map(lambda s: autograd.Variable(torch.Tensor([s])), [
        [0, 0],
        [0, 1],
        [1, 0],
        [1, 1]
    ]))

    targets = list(map(lambda s: autograd.Variable(torch.Tensor([s])), [
        [0],
        [1],
        [1],
        [0]
    ]))

It is required for an experiment's configuration class to contain a fitness_fn() method. It takes just one argument - a genome.

    def fitness_fn(self, genome):
        fitness = 4.0  # Max fitness for XOR

        phenotype = FeedForwardNet(genome, self)
        phenotype.to(self.DEVICE)
        criterion = nn.MSELoss()

        for input, target in zip(self.inputs, self.targets):  # 4 training examples
            input, target = input.to(self.DEVICE), target.to(self.DEVICE)

            pred = phenotype(input)
            loss = (float(pred) - float(target)) ** 2
            loss = float(loss)

            fitness -= loss

        return fitness

Feel free to add additional methods for experiment-specific uses.

    def get_preds_and_labels(self, genome):
        phenotype = FeedForwardNet(genome, self)
        phenotype.to(self.DEVICE)

        predictions = []
        labels = []
        for input, target in zip(self.inputs, self.targets):  # 4 training examples
            input, target = input.to(self.DEVICE), target.to(self.DEVICE)

            predictions.append(float(phenotype(input)))
            labels.append(float(target))

Contributors

License: MIT

Copyright (c) 2018 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pytorch-neat's People

Contributors

ccneko avatar ddehueck avatar dmitriyvaletov avatar zappaboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-neat's Issues

my question is: this NEAT algorithm works well on XOR-experiment(2 input and 2 output), but I use it on a 3-input function, it works less efficient than XOR-experiment.

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

  • OS: [e.g. iOS]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

PyTorch native implementation

Is your feature request related to a problem? Please describe.
I have updated the code to use the latest PyTorch and I tried to make it more native, closer to GPU. I wouldn't say I succeeded in making it more performant but more stable (fewer context switches?).

Describe the solution you'd like
If you are interested in merging this code please review it.

Gist source code

If you request I can submit a PR.

RuntimeError

Describe the bug
RuntimeError: _th_normal is not implemented for type torch.LongTensor

To Reproduce
Steps to reproduce the behavior:
python xor_run.py

Expected behavior
no error

Screenshots
If applicable, add screenshots to help explain your problem.

  • OS: CentOS
  • Version 7

Additional context
anaconda 4.7.5
pytorch 1.0.1

Evolving Activation function choices

Is your feature request related to a problem? Please describe.
I would like to see if we can evolve activation function choices like in the original Python NEAT

Describe the solution you'd like
A setting in the config for activation takes in the value 'random' and then evolves the activation function from a set of activations available.

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.