ukuleleplayer / pureples

Pure Python Library for ES-HyperNEAT. Contains implementations of HyperNEAT and ES-HyperNEAT.
MIT License
111 stars 36 forks source link

Config always finds 5 inputs. [RuntimeError: Expected 840 inputs, got 5] #10

Closed SilverDash closed 2 years ago

SilverDash commented 4 years ago
 ****** Running generation 0 ******

Traceback (most recent call last):
  File "c:\Users\Silver\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\ptvsd_launcher.py", line 48, in <module>
    main(ptvsdArgs)
  File "c:\Users\Silver\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\Silver\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "g:\Emulators\ML AI open AI\env2.py", line 51, in <module>
    winner = run(200, env)[0]
  File "g:\Emulators\ML AI open AI\env2.py", line 37, in run
    winner, stats = run_es(gens, env, 200, config, params, sub, max_trials=200)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\shared\gym_runner.py", line 50, in run_es
    pop.run(eval_fitness, gens)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\neat\population.py", line 89, in run
    fitness_function(list(iteritems(self.population)), self.config)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\shared\gym_runner.py", line 25, in eval_fitness
    net = network.create_phenotype_network()
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\es_hyperneat\es_hyperneat.py", line 46, in create_phenotype_network
    hidden_nodes, connections = self.es_hyperneat()
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\es_hyperneat\es_hyperneat.py", line 151, in es_hyperneat
    root = self.division_initialization((x, y), True)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\es_hyperneat\es_hyperneat.py", line 110, in division_initialization
    c.w = query_cppn(coord, (c.x, c.y), outgoing, self.cppn, self.max_weight)
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\pureples\hyperneat\hyperneat.py", line 84, in query_cppn
    w = cppn.activate(i)[0]
  File "C:\Users\Silver\AppData\Local\Programs\Python\Python37\lib\site-packages\neat\nn\feed_forward.py", line 14, in activate
    raise RuntimeError("Expected {0:n} inputs, got {1:n}".format(len(self.input_nodes), len(inputs)))
RuntimeError: Expected 840 inputs, got 5

I ran this through the Debugger and found that at some point some random float values replace the existing number of inputs that initially gets set.

I could even see that at some point during execution the correct number of inputs was actually used.

I've been fighting to find the cause and I've come to the conclusion that something has to be wrong in the module.

for some context, I took one of the examples and attempted to configure it to run a gym retro env.

As you can see though the only thing stopping me is the inputs being messed up somehow.

If you need more information please let me know.

ukuleleplayer commented 4 years ago

Thanks for opening this issue - I’ve a lot of stuff to do at the moment, but I’ll dig into this when I eventually get the time.

SilverDash commented 4 years ago

Thank you. I want to add that other agents resulted in the same error

123mitnik commented 4 years ago

Hi. I can confirm this issue. It happens in all environments

PlayerCFM commented 3 years ago

Hello. As mentioned by the ticket's author, the issue still persists. For instance, I'm trying to create a phenotype with 130 inputs and the exception that it arises is the following:

multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "trainingBehavior.py", line 88, in eval_genome
    net = create_phenotype_network(cppn, sub)
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/pureples-0.0-py3.8.egg/pureples/hyperneat/hyperneat.py", line 29, in create_phenotype_network
    im = find_neurons(cppn, oc, layer, hidden_nodes[idx], False)
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/pureples-0.0-py3.8.egg/pureples/hyperneat/hyperneat.py", line 68, in find_neurons
    w = query_cppn(coord, node, outgoing, cppn, max_weight)
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/pureples-0.0-py3.8.egg/pureples/hyperneat/hyperneat.py", line 84, in query_cppn
    w = cppn.activate(i)[0]
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/neat_python-0.92-py3.8.egg/neat/nn/feed_forward.py", line 13, in activate
    raise RuntimeError("Expected {0:n} inputs, got {1:n}".format(len(self.input_nodes), len(inputs)))
RuntimeError: Expected 130 inputs, got 5
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "trainingBehavior.py", line 152, in <module>
    winner = run(300)[0]
  File "trainingBehavior.py", line 143, in run
    winner = pop.run(pe.evaluate, 150)
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/neat_python-0.92-py3.8.egg/neat/population.py", line 88, in run
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/site-packages/neat_python-0.92-py3.8.egg/neat/parallel.py", line 31, in evaluate
  File "/home/USER/miniconda3/envs/HyperNEAT-conda/lib/python3.8/multiprocessing/pool.py", line 771, in get
    raise self._value
RuntimeError: Expected 130 inputs, got 5
aacoimbra commented 2 years ago

Hi. I'm facing the same issue as listed above.

So the problem seems to be in is the ESNetwork class.

First, I've created an ESNetwork object from pureples.es_hyperneat.es_hyperneat and called the function self.create_phenotype_network() which called self.es_hyperneat().

The thing is that self.es_hyperneat() then calls two functions: self.division_initialization() and self.pruning_extraction() and both functions call query_cppn() from pureples.hyperneat.hyperneat which calls .activate() from the neat-python package.

And this last function expects a list with one value for each input node, but query_cppn() always feeds 4 coordinates + 1 (directly from code: [coord1[0], coord1[1], coord2[0], coord2[1], 1.0]).

Could it be the case that it's hardcoded for only 5 inputs?

ukuleleplayer commented 2 years ago

Hi all, Could any of you above provide me with a minimal "working" example with config file and an experiment specification with input/output, fitness function etc.? Then I'll have a look ASAP.

ukuleleplayer commented 2 years ago

@aacoimbra: What you describe with the query_cppn() is at least on purpose; this function should query the CPPN from one point to another, hence coord1 to coord2 in the code.

aacoimbra commented 2 years ago

@ukuleleplayer Thanks for the reply. Here is a quick minimal example raising the following error: RuntimeError: Expected 20 inputs, got 5.

This behavior could very well be a misusage of the library.

Minimal Pureples.zip

ukuleleplayer commented 2 years ago

@aacoimbra: Your example is working for me, if I use the config_cppn_xor provided. Try to get the latest code, put your training.py inside the experiments/xor folder. Change the path of the config to point to the existing CPPN config.

The input stuff seems to be mismatch between the number of input coordinates and the number of input values. Try to hardcode it first to something you know - like 20 input coordinates and 20 input values (while keeping output coordinates and values to 1). If this doesn't take you further than your described error with input, then please provide one more example 👍

I just played around with XOR, trying to add 10 input parameters and 10 input values and that worked (even though XOR doesn't make sense with 10 values...).

aacoimbra commented 2 years ago

Thank you @ukuleleplayer, I was mixing some concepts.

For those who face this problem:

If you are uncertain about placing your input nodes here's a quick tip from this link:

Why do I still have to decide on the placement of Input and Output nodes? While ES-HyperNEAT frees the user from deciding on the position of the hidden nodes, placing the inputs and outputs allows the user to insert knowledge about the problem geometry into the evolutionary search, which is invisible to traditional encodings. For example, the sensors of an autonomous robot can be placed from left to right on the substrate in the same order that they exist on the robot. Outputs for moving left or right can also be placed in the same order, allowing ES-HyperNEAT to understand from the outset the correlation of sensors to effectors. Therefore, allowing to user to inject such knowledge from the start is a potential advantage.

ASJB commented 1 year ago

Hello, Sorry to add to a closed issue but I have a question regarding this problem. I am using the XOR template for a problem with around 150 inputs, but with the config file inputs set at 5 the output cppn only has 5 inputs. I would like to see the cppn with my inputs, is this possible?

ukuleleplayer commented 1 year ago

@ASJB - hey! Not sure exactly what you mean. Try to have a look at the examples, some nets are drawn there in the bottom of the files. Also, have a look at the visualize.py which contains various drawing magic. It should be able to draw your CPPN. So.. save your CPPN after running your experiment and then feed it to some visualisation.