Git Product home page Git Product logo

pytorch-neat's Introduction

PyTorch NEAT

Background

NEAT (NeuroEvolution of Augmenting Topologies) is a popular neuroevolution algorithm, one of the few such algorithms that evolves the architectures of its networks in addition to the weights. For more information, see this research paper: http://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf.

HyperNEAT is an extension to NEAT that indirectly encodes the weights of the network (called the substrate) with a separate network (called a CPPN, for compositional pattern-producing network). For more information on HyperNEAT, see this website: http://eplex.cs.ucf.edu/hyperNEATpage/.

Adaptive HyperNEAT is an extension to HyperNEAT which indirectly encodes both the initial weights and an update rule for the weights such that some learning can occur during a network's "lifetime." For more information, see this research paper: http://eplex.cs.ucf.edu/papers/risi_sab10.pdf.

About

PyTorch NEAT builds upon NEAT-Python by providing some functions which can turn a NEAT-Python genome into either a recurrent PyTorch network or a PyTorch CPPN for use in HyperNEAT or Adaptive HyperNEAT. We also provide some environments in which to test NEAT and Adaptive HyperNEAT, and a more involved example using the CPPN infrastructure with Adaptive HyperNEAT on a T-maze.

Examples

The following snippet turns a NEAT-Python genome into a recurrent PyTorch network:

from pytorch_neat.recurrent_net import RecurrentNet

net = RecurrentNet.create(genome, config, bs)
outputs = net.activate(some_array)

You can also turn a NEAT-Python genome into a CPPN:

from pytorch_neat.cppn import create_cppn

cppn_nodes = create_cppn(genome, config)

A CPPN is represented as a graph structure. For easy evaluation, a CPPN's input and output nodes may be named:

from pytorch_neat.cppn import create_cppn

[delta_w_node] = create_cppn(
    genome,
    config,
    ["x_in", "y_in", "x_out", "y_out", "pre", "post", "w"],
    ["delta_w"],
)

delta_w = delta_w_node(x_in=some_array, y_in=other_array, ...)

We also provide some infrastructure for running networks in Gym environments:

from pytorch_neat.multi_env_eval import MultiEnvEvaluator
from pytorch_neat.recurrent_net import RecurrentNet

def make_net(genome, config, batch_size):
    return RecurrentNet.create(genome, config, batch_size)


def activate_net(net, states):
    outputs = net.activate(states).numpy()
    return outputs[:, 0] > 0.5

def make_env():
    return gym.make("CartPole-v0")

evaluator = MultiEnvEvaluator(
    make_net, activate_net, make_env=make_env, max_env_steps=max_env_steps, batch_size=batch_size,
)

fitness = evaluator.eval_genome(genome)

This allows multiple environments to run in parallel for efficiency.

A simple example using NEAT to solve the Cartpole can be run like this:

python3 -m examples.simple.main

And a simple example using Adaptive HyperNEAT to partially solve a T-maze can be run like this:

python3 -m examples.adaptive.main

Author / Support

PyTorch NEAT is extended from Python NEAT by Alex Gajewsky.

Questions can be directed to [email protected].

pytorch-neat's People

Contributors

jal278 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-neat's Issues

AssertionError: assert len(leaf_names) == len(genome_config.input_keys)

No matter how I attempt to start one of the examples (adaptive)
I get this error. Full log here.

  File "g:/Emulators/ML AI open AI/env2.py", line 130, in <module>
    run()  # pylint: disable=no-value-for-parameter
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "g:/Emulators/ML AI open AI/env2.py", line 120, in run
    winner = pop.run(eval_genomes, n_generations)
  File "g:\emulators\ml ai open ai\src\neat-python\neat\population.py", line 88, in run
    fitness_function(list(self.population.items()), self.config)
  File "g:/Emulators/ML AI open AI/env2.py", line 110, in eval_genomes
    raise e
  File "g:/Emulators/ML AI open AI/env2.py", line 106, in eval_genomes
    genome, config, debug=DEBUG and i % 100 == 0
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\tf_neat\multi_env_eval.py", line 30, in eval_genome
    net = self.make_net(genome, config, self.batch_size)
  File "g:/Emulators/ML AI open AI/env2.py", line 52, in make_net
    device="gpu",
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\tf_neat\adaptive_linear_net.py", line 149, in create
    output_activation=output_activation,
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\tf_neat\cppn.py", line 199, in create_cppn
    assert len(leaf_names) == len(genome_config.input_keys)
AssertionError`

To add to this, I'm confused about the input coordinates and output coordinates. Is there no documentation on what this is supposed to mean? I thought possibly my NEAT config is conflicting with that but I'm seriously unsure of how to resolve it.

Model checkpoint issue

I tried to add this line, and there are problems with pickling it seems

pop.add_reporter(neat.Checkpointer(1))

Error:

Saving checkpoint to neat-checkpoint-0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-0f6fae74326a> in <module>()
     54 
     55 if __name__ == "__main__":
---> 56     run(100)

4 frames
<ipython-input-53-0f6fae74326a> in run(n_generations)
     50     pop.add_reporter(neat.Checkpointer(1))
     51 
---> 52     pop.run(eval_genomes, n_generations)
     53 
     54 

/usr/local/lib/python3.6/dist-packages/neat/population.py in run(self, fitness_function, n)
    127             self.species.speciate(self.config, self.population, self.generation)
    128 
--> 129             self.reporters.end_generation(self.config, self.population, self.species)
    130 
    131             self.generation += 1

/usr/local/lib/python3.6/dist-packages/neat/reporting.py in end_generation(self, config, population, species_set)
     34     def end_generation(self, config, population, species_set):
     35         for r in self.reporters:
---> 36             r.end_generation(config, population, species_set)
     37 
     38     def post_evaluate(self, config, population, species, best_genome):

/usr/local/lib/python3.6/dist-packages/neat/checkpoint.py in end_generation(self, config, population, species_set)
     57 
     58         if checkpoint_due:
---> 59             self.save_checkpoint(config, population, species_set, self.current_generation)
     60             self.last_generation_checkpoint = self.current_generation
     61             self.last_time_checkpoint = time.time()

/usr/local/lib/python3.6/dist-packages/neat/checkpoint.py in save_checkpoint(self, config, population, species_set, generation)
     68         with gzip.open(filename, 'w', compresslevel=5) as f:
     69             data = (generation, config, population, species_set, random.getstate())
---> 70             pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
     71 
     72     @staticmethod

TypeError: cannot serialize '_io.TextIOWrapper' object

Problem with torch

When running pip install -r requirements.txt, I got the error :
ERROR: No matching distribution found for torch==0.4.0 (from -r requirements.txt (line 5))

Exception: Failed to activate node delta_w

Currently running the adaptive HyperNEAT example using device = "cuda:0"

Population size: 100
Elitism: 10%
No multiprocessing

It runs okay for several generations, up until the following exception is thrown from the cppn.py
Any help to sort out this issue is appreciated.

 File "/pytorch_neat/cppn.py", line 108, in __call__
    return self.get_activs(shape)
  File "/pytorch_neat/cppn.py", line 93, in get_activs
    self.activs = self.activate(xs, shape)
  File "/pytorch_neat/cppn.py", line 87, in activate
    raise Exception("Failed to activate node {}".format(self.name))
Exception: Failed to activate node delta_w

RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_bmm

Is anyone aware of this issue with regards to the use of `device="cuda:0" when trying to run the CUDA version of the adaptive HyperNEAT example (T-Maze) I get the subject error.

Below a full Trace.

Has anyone encountered such a problem? seems to be originated after every generation ends as if the argument is of device=('cpu') instead of being CUDA.

Any help with this?

RuntimeError                              Traceback (most recent call last)
<ipython-input-2-621c87ec8634> in <module>
    113 
    114 if __name__ == "__main__":
--> 115     run()  # pylint: disable=no-value-for-parameter

<ipython-input-2-621c87ec8634> in run(n_generations, n_processes)
    103     pop.add_reporter(logger)
    104 
--> 105     winner = pop.run(eval_genomes, n_generations)
    106 
    107     print(f'Best Genome: {winner}')

/usr/local/lib/python3.7/dist-packages/neat/population.py in run(self, fitness_function, n)
     87 
     88             # Evaluate all genomes using the user-provided function.
---> 89             fitness_function(list(iteritems(self.population)), self.config)
     90 
     91             # Gather and report statistics.

<ipython-input-2-621c87ec8634> in eval_genomes(genomes, config)
     93                     print(genome)
     94 
---> 95                     raise e
     96 
     97     pop = neat.Population(config)

<ipython-input-2-621c87ec8634> in eval_genomes(genomes, config)
     88                 try:
     89                     genome.fitness = evaluator.eval_genome(
---> 90                         genome, config, debug=DEBUG and i % 100 == 0
     91                     )
     92                 except Exception as e:

~/AHN_experiment_Tmaze/pytorch_neat/multi_env_eval.py in eval_genome(self, genome, config, debug)
     43                     net, states, debug=True, step_num=step_num)
     44             else:
---> 45                 actions = self.activate_net(net, states)
     46             assert len(actions) == len(self.envs)
     47             for i, (env, action, done) in enumerate(zip(self.envs, actions, dones)):

<ipython-input-2-621c87ec8634> in activate_net(net, states, debug, step_num)
     41         print(net.delta_w_node)
     42         print("W init: ", net.input_to_output[0])
---> 43     outputs = net.activate(states)#.to('cpu').numpy()
     44     if debug and (step_num - 1) % 100 == 0:
     45         print(f"\nStep: {step_num - 1}")

~/AHN_experiment_Tmaze/pytorch_neat/adaptive_linear_net.py in activate(self, inputs)
    105             ).unsqueeze(2)
    106 
--> 107             outputs = self.activation(self.input_to_output.matmul(inputs))
    108 
    109             input_activs = inputs.transpose(1, 2).expand(

RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_bmm



RuntimeError: tensors used as indices must be long or byte tensors

I am trying to run the examples.simple.main.py and I encounter the following exception:

RuntimeError: tensors used as indices must be long or byte tensors

I have the following packages installed with pip3 in python3.6:

neat-python==0.92
numpy==1.15.2+mkl
gym==0.10.5
click==6.7
torch==0.4.0
torchvision==0.2.1

This error occurs within pytorch_neat.recurrent_net in the dense_from_coo function:

def dense_from_coo(shape, conns, dtype=torch.float64):
    mat = torch.zeros(shape, dtype=dtype)
    idxs, weights = conns
    if len(idxs) == 0:
        return mat
    rows, cols = np.array(idxs).transpose()
    mat[torch.tensor(rows), torch.tensor(cols)] = torch.tensor(
        weights, dtype=dtype)
    return mat

The problem is that np.array is assuming int32 for the indexes, but torch wants int64.
Simple solution:

def dense_from_coo(shape, conns, dtype=torch.float64):
    mat = torch.zeros(shape, dtype=dtype)
    idxs, weights = conns
    if len(idxs) == 0:
        return mat
    rows, cols = np.array(idxs, dtype=np.int64).transpose()
    mat[torch.tensor(rows), torch.tensor(cols)] = torch.tensor(
        weights, dtype=dtype)
    return mat

The difference may be from the differing numpy versions, but I think this change makes sense regardless for torch tensor indexing.

I cannot get this to run.

I'm doing some research in continual learning, and a HyperNEAT implementation would come in handy. I have tried many things, and for the life of me I cannot get this implementation to run.

When I use a modern version of torch, I get IndexError: tensors used as indices must be long, byte or bool tensors after running the simple example.

I'm not sure if I can install pytorch 0.4.0, and even if I did I don't think this version supports CUDA.

Does anyone know if there is a way I can use this implementation, otherwise are there any other implementations of HyperNEAT I can use out there?

ValueError: expected sequence of length 4 at dim 2 (got 0)

def dense_from_coo(shape, conns, dtype=torch.float64):
mat = torch.zeros(shape, dtype=dtype)
idxs, weights = conns
if len(idxs) == 0:
return mat
rows, cols = np.array(idxs).transpose()
mat[torch.LongTensor(rows), torch.LongTensor(cols)] = torch.tensor(
weights, dtype=dtype)
return mat

after changing torch.tensor to torch.longTensor. This error pops up.

Torch version 1.17

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.