25

I want to create an artificial neural network (in PyBrain) that follows the following layout:

layout

However, I cannot find the proper way to achieve this. The only option that I see in the documentation is the way to create fully connected layers, which is not what I want: I want some of my input nodes to be connected to the second hidden layer and not to the first one.

Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170

2 Answers2

22

The solution is to use the connection type of your choice, but with slicing parameters: inSliceFrom, inSliceTo, outSliceFrom and outSliceTo. I agree the documentation should mention this, so far it's only in the Connection class' comments.

Here is example code for your case:

#create network and modules
net = FeedForwardNetwork()
inp = LinearLayer(9)
h1 = SigmoidLayer(2)
h2 = TanhLayer(2)
outp = LinearLayer(1)
# add modules
net.addOutputModule(outp)
net.addInputModule(inp)
net.addModule(h1)
net.addModule(h2)
# create connections
net.addConnection(FullConnection(inp, h1, inSliceTo=6))
net.addConnection(FullConnection(inp, h2, inSliceFrom=6))
net.addConnection(FullConnection(h1, h2))
net.addConnection(FullConnection(h2, outp))
# finish up
net.sortModules()
schaul
  • 1,021
  • 9
  • 21
  • Hmm, I'm actually learning PyBrain from the source code itself, except that I use to kick it in with the tutorial, but then checking in depth what its code does with the lines of the tutorial. Turns out it is a good idea? :) Read code, not docs - python has docstrings anyway! :) – n611x007 May 19 '13 at 15:56
  • Hi, just curious. Let's say we want to create a cascade feed forward neural network (http://www.mathworks.com/help/nnet/ref/cascadeforwardnet.html), so we will just need to create connections between any 2 layers? – meta_warrior Jan 14 '15 at 16:49
0

An alternative way to the one suggested by schaul is to use multiple input layers.

#create network
net = FeedForwardNetwork()

# create and add modules
input_1 = LinearLayer(6)
net.addInputModule(input_1)
input_2 = LinearLayer(3)
net.addInputModule(input_2)
h1 = SigmoidLayer(2)
net.addModule(h1)
h2 = SigmoidLayer(2)
net.addModule(h2)
outp = SigmoidLayer(1)
net.addOutputModule(outp)

# create connections
net.addConnection(FullConnection(input_1, h1))
net.addConnection(FullConnection(input_2, h2))
net.addConnection(FullConnection(h1, h2))
net.addConnection(FullConnection(h2, outp))

net.sortModules()
Staza
  • 3,145
  • 1
  • 11
  • 12