PyBrain neuron manipulation
Asked Answered
M

1

6

Is there a good way to add/remove a neuron and its associated connections into/from a fully connected PyBrain network? Say I start with:

from pybrain.tools.shortcuts import buildNetwork
net = buildNetwork(2,3,1)

How would I go about making it a (2,4,1) or a (2,2,1) network WHILE maintaining all the old weights (and initializing any new ones to be random as is done when initializing the network)? The reason I want to do this is because I am attempting to use an evolutionary learning strategy to determine the best architecture and the 'mutation' step involves adding/removing nodes with some probability. (The input and output modules should always remain the same.)

edit: I found NeuronDecomposableNetwork which should make this easier, but it still seems that I have to keep track of neurons and connections separately.

Madaih answered 25/7, 2012 at 23:10 Comment(0)
T
4

I assume you're doing along the lines of the NEAT algorithm? There are two different answers to your question:

  1. Open ended evolution of the network topology: in this case, I recommend encapsulating every neuron in its own "layer"/module, and add/remove them and their connections to the network iteratively, a bit like in this tutorial, except that there will be many more (single-neuron) layers. Don't forget to call the sortModules() method after each topological change.

  2. Finding the best topology within a predefined framework (say a maximum of 1000 neurons). In that case it's easier and more efficient to build the full network in the beginning, and just mask some of the connections (e.g. using the MaskedParameters module). Among others, memetic algorithms (used like this) are designed to search such topology spaces.

An alternative, as you say, is manually managing all the weights (by tracking what is where, or using NeuronDecomposableNetwork) but I don't recommend that.


A general comment: for more advanced uses of pybrain such as yours, relying on the `buildNetwork' shortcut is really too limited, and you will want to use the Network/Module/Connection API directly.

Thunderpeal answered 7/8, 2012 at 3:59 Comment(1)
Awesome, this helps me out a ton, thanks! I really like the first idea. Never would have thought about using layers as individual neurons. And yes after trying for about three days now I would not recommend manual management either lol (the other stackoverflow post was actually where I got the idea). I'll come back after some experimenting (and researching NEAT as this is the first I've found out about it, yet being almost exactly what I wanted to get to) and update my post with the results.Madaih

© 2022 - 2024 — McMap. All rights reserved.