我正在尝试在 PyBrain 中创建一个利用共享权重连接的神经网络,但这样做时遇到了麻烦。我没有找到太多使用这些类型连接的示例,但我认为我已经从我找到的那些和源代码中收集了使用它们的方法。但显然我没有那么幸运。
我希望共享矩形中显示的连接,因为沿每条路径的权重是相同的(将输入向量 [x,y] 与 [y,x] 交换应该产生相同的输出)。
我尝试使用以下代码构建此架构:
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
所以我想我一定对这种设置方式有误解。非常感谢任何能指出我对这些命令的理解在哪里误入歧途的人!