📅  最后修改于: 2020-12-10 05:17:57             🧑  作者: Mango
连接的工作方式类似于图层;唯一的区别是它将数据从网络中的一个节点转移到另一个节点。
在本章中,我们将学习-
这是创建网络时使用的连接的有效示例。
ffy.py
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection
network = FeedForwardNetwork()
#creating layer for input => 2 , hidden=> 3 and output=>1
inputLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outputLayer = LinearLayer(1)
#adding the layer to feedforward network
network.addInputModule(inputLayer)
network.addModule(hiddenLayer)
network.addOutputModule(outputLayer)
#Create connection between input ,hidden and output
input_to_hidden = FullConnection(inputLayer, hiddenLayer)
hidden_to_output = FullConnection(hiddenLayer, outputLayer)
#add connection to the network
network.addConnection(input_to_hidden)
network.addConnection(hidden_to_output)
network.sortModules()
print(network)
C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-6
Modules:
[, ,
]
Connections:
[ 'LinearLayer-8'>,
'SigmoidLayer-7'>]
在Pybrain中,我们可以使用连接模块创建连接,如下所示-
connect.py
from pybrain.structure.connections.connection import Connection
class YourConnection(Connection):
def __init__(self, *args, **kwargs):
Connection.__init__(self, *args, **kwargs)
def _forwardImplementation(self, inbuf, outbuf):
outbuf += inbuf
def _backwardImplementation(self, outerr, inerr, inbuf):
inerr += outer
要创建连接,有2种方法-_forwardImplementation()和_backwardImplementation() 。
调用_forwardImplementation()时,传入模块的输出缓冲区为inbuf ,传出模块的输入缓冲区称为outbuf 。将inbuf添加到传出模块outbuf中。
_backwardImplementation()用externalr , inerr和inbuf调用。将传出模块错误添加到_backwardImplementation()中的传入模块错误中。
现在让我们在网络中使用YourConnection 。
testconnection.py
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from connect import YourConnection
network = FeedForwardNetwork()
#creating layer for input => 2 , hidden=> 3 and output=>1
inputLayer = LinearLayer(2)
hiddenLayer = SigmoidLayer(3)
outputLayer = LinearLayer(1)
#adding the layer to feedforward network
network.addInputModule(inputLayer)
network.addModule(hiddenLayer)
network.addOutputModule(outputLayer)
#Create connection between input ,hidden and output
input_to_hidden = YourConnection(inputLayer, hiddenLayer)
hidden_to_output = YourConnection(hiddenLayer, outputLayer)
#add connection to the network
network.addConnection(input_to_hidden)
network.addConnection(hidden_to_output)
network.sortModules()
print(network)
C:\pybrain\pybrain\src>python testconnection.py
FeedForwardNetwork-6
Modules:
[, ,
]
Connections:
[ 'SigmoidLayer-7'>,
'LinearLayer-8'>]