我正在尝试在PyBrain中创建一个神经网络,它利用共享权重连接,但是我遇到了一些问题。我没有找到太多使用这些类型连接的例子,但我认为从我找到的例子和源代码中已经掌握了使用它们的方法。但显然我没有那么幸运。
作为一个简单的例子,我正在尝试创建以下共享权重的神经网络:
矩形中显示的连接应该是共享的,每条路径上的权重相同(将输入向量[x,y]与[y,x]交换应该产生相同的输出)。
我尝试使用以下代码构建此体系结构:
在上述代码中,我创建了两个MotherConnections,mc和mc2,其想法是这两个对象将分别保存我的第一个和第二个矩形中共享的权重,如图所示。然后我使用ModuleSlice将输入模块和隐藏模块分成两组。接着我尝试使用mc和mc2容器添加连接来连接这些路径。
运行上述代码时不会出现错误。但是如果我取消注释结尾处的net.activate语句来测试网络,就会出现以下错误:
作为一个简单的例子,我正在尝试创建以下共享权重的神经网络:
![a simple SWNN](https://istack.dev59.com/CzgXN.webp)
我尝试使用以下代码构建此体系结构:
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
因此,我想我一定对这个设置的方式有所误解。非常感谢能够指出我在这些命令理解方面偏离轨道的任何人!