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.
I assume you're doing along the lines of the NEAT algorithm? There are two different answers to your question:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With