在Pybrain中创建共享权重连接

时间:2013-01-09 15:16:50

标签: python machine-learning neural-network pybrain

我正在尝试在PyBrain中创建一个利用共享权重连接的神经网络,但我很难这样做。我没有找到太多使用这种类型的连接的例子,但我认为我已经收集了从我找到的那些和源代码中使用它们的方法。但显然我没那么幸运。

作为一个简单的例子,我正在尝试创建以下共享权重神经网络: a simple SWNN http://i49.tinypic.com/ztuc2g.jpg

矩形中显示的连接我希望共享,因为沿每条路径的权重是相同的(用[y,x]交换输入向量[x,y]应该产生相同的输出)。

我尝试使用以下代码构建此体系结构:

from pybrain.structure.modules.linearlayer import LinearLayer
from pybrain.structure.modules.sigmoidlayer import SigmoidLayer
from pybrain.structure.moduleslice import ModuleSlice
from pybrain.structure.networks.feedforward import FeedForwardNetwork
from pybrain.structure.connections.shared import MotherConnection,SharedFullConnection

net=FeedForwardNetwork()

# make modules
inp=LinearLayer(2,name='input')
h1=SigmoidLayer(2,name='hidden')
outp=LinearLayer(1,name='output')

# now add modules
net.addOutputModule(outp)
net.addInputModule(inp)
net.addModule(h1)

# now we need to create the connections
mc=MotherConnection(2,name='mother') 
mc2=MotherConnection(2,name='mother2')
topInput=ModuleSlice(inp,outSliceFrom=0,outSliceTo=1)
bottomInput=ModuleSlice(inp,outSliceFrom=1,outSliceTo=2)
topHidden=ModuleSlice(h1,inSliceFrom=0,inSliceTo=1)
bottomHidden=ModuleSlice(h1,inSliceFrom=1,inSliceTo=2)
net.addConnection(SharedFullConnection(mc,topInput,topHidden))
net.addConnection(SharedFullConnection(mc,bottomInput,bottomHidden))
net.addConnection(SharedFullConnection(mc2,topHidden,outp))
net.addConnection(SharedFullConnection(mc2,bottomHidden,outp))

# finish up
net.sortModules()

#print net.activate([2,1])

在上面的代码中,我创建了两个MotherConnections,mc和mc2,这个想法是这两个对象分别在我的第一个和第二个矩形中保存共享权重,如图所示。然后我使用ModuleSlice将输入模块和隐藏模块分成两组。然后我尝试使用mc和mc2容器添加连接以连接这些路径。

运行上面的代码我没有收到错误。但是如果我尝试通过在最后取消注释net.activate语句来测试网络,我会收到以下错误:

Traceback (most recent call last):
  File "test.py", line 38, in <module>
    print net.activate([2,1])
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-py2.7.egg/pybrain/structure/networks/feedforward.py", line 19, in activate
    return super(FeedForwardNetworkComponent, self).activate(inpt)
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-   py2.7.egg/pybrain/structure/modules/module.py", line 123, in activate
    self.forward()
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-py2.7.egg/pybrain/structure/modules/module.py", line 75, in forward
    self.outputbuffer[self.offset])
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-  py2.7.egg/pybrain/structure/networks/feedforward.py", line 32, in _forwardImplementation
    c.forward()
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-py2.7.egg/pybrain/structure/connections/connection.py", line 77, in forward
    self.outmod.inputbuffer[outmodOffset, self.outSliceFrom:self.outSliceTo])
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-py2.7.egg/pybrain/structure/connections/shared.py", line 64, in _forwardImplementation
    FullConnection._forwardImplementation(self, inbuf, outbuf)
  File "/usr/local/lib/python2.7/dist-packages/PyBrain-0.3.1-py2.7.egg/pybrain/structure/connections/full.py", line 19, in _forwardImplementation
    outbuf += dot(reshape(self.params, (self.outdim, self.indim)), inbuf)
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 171, in reshape
    return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged

所以我想我一定是对这个设置的方式有所误解。非常感谢任何能够指出我对这些命令的理解我误入歧途的人!

1 个答案:

答案 0 :(得分:2)

我可能已经找到了问题所在。我想我应该在topHidden和bottomHidden的隐藏图层定义中包含outSlices,例如

topHidden=ModuleSlice(h1,inSliceFrom=0,inSliceTo=1,outSliceFrom=0,outSliceTo=1)
bottomHidden=ModuleSlice(h1,inSliceFrom=1,inSliceTo=2,outSliceFrom=1,outSliceTo=2)

天真我想我认为这是不必要的,因为隐藏层中所有内容的输出都连接到输出层。但是,如果没有这样做,那么隐藏层似乎没有正确的outdim(ension)并导致上述错误。
另外,MotherConnections应该这样定义:

mc=MotherConnection(1,name='mother')
mc2=MotherConnection(1,name='mother2')

我还没有广泛测试过这个网络,但它似乎不再有上述问题了。