Pybrain 简明教程

PyBrain - Connections

连接类似于层;唯一的不同是,它在网络中将数据从一个节点转移到另一个节点。

在此章节,我们将学习:

  1. Understanding Connections

  2. Creating Connections

Understanding Connections

下面是一个在创建网络时使用连接的工作示例。

Example

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)

Output

C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-6
Modules:
[<LinearLayer 'LinearLayer-3'>, <SigmoidLayer 'SigmoidLayer-7'>,
   <LinearLayer 'LinearLayer-8'>]
Connections:
[<FullConnection 'FullConnection-4': 'SigmoidLayer-7' -> 'LinearLayer-8'>,
   <FullConnection 'FullConnection-5': 'LinearLayer-3' -> 'SigmoidLayer-7'>]

Creating Connections

在 Pybrain 中,我们可以使用如下所示的连接模块来创建连接:

Example

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() 在 outerr、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)

Output

C:\pybrain\pybrain\src>python testconnection.py
FeedForwardNetwork-6
Modules:
[<LinearLayer 'LinearLayer-3'>, <SigmoidLayer 'SigmoidLayer-7'>,
   <LinearLayer 'LinearLayer-8'>]
Connections:
[<YourConnection 'YourConnection-4': 'LinearLayer-3' -> 'SigmoidLayer-7'>,
   <YourConnection 'YourConnection-5': 'SigmoidLayer-7' -> 'LinearLayer-8'>]