Pybrain 简明教程

PyBrain - Working with Feed-Forward Networks

前馈网络是一种神经网络,其中节点之间的信息向前移动,并且永远不会向后传播。前馈网络是人工神经网络中可用网络中第一个也是最简单的网络。信息从输入节点传递到隐藏节点,然后传递到输出节点。

A feed-forward network is a neural network, where the information between nodes moves in the forward direction and will never travel backward. Feed Forward network is the first and the simplest one among the networks available in the artificial neural network. The information is passed from the input nodes, next to the hidden nodes and later to the output node.

在本章中,我们将讨论如何 −

In this chapter we are going to discuss how to −

  1. Create Feed-Forward Networks

  2. Add Connection and Modules to FFN

Creating a Feed Forward Network

您可以使用您选择的 python IDE,即 PyCharm。在此,我们使用 Visual Studio Code 编写代码,并将在终端中执行相同的代码。

You can use the python IDE of your choice, i.e., PyCharm. In this, we are using Visual Studio Code to write the code and will execute the same in terminal.

要创建一个前馈网络,我们需要从 pybrain.structure 导入它,如下所示 −

To create a feedforward network, we need to import it from pybrain.structure as shown below −

ffn.py

from pybrain.structure import FeedForwardNetwork
network = FeedForwardNetwork()
print(network)

按如下所示执行 ffn.py −

Execute ffn.py as shown below −

C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-0
Modules:
[]
Connections:
[]

我们尚未向前馈网络添加任何模块和连接。因此,网络显示模块和连接的空数组。

We have not added any modules and connections to the feedforward network. Hence the network shows empty arrays for Modules and Connections.

Adding Modules and Connections

我们将首先创建输入、隐藏、输出层,并将它们添加到模块中,如下所示 −

First we will create input, hidden, output layers and add the same to the modules as shown below −

ffy.py

from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
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)

print(network)

Output

C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-3
Modules:
[]
Connections:
[]

模块和连接仍然为空。我们需要为创建的模块提供连接,如下所示 −

We are still getting the modules and connections as empty. We need to provide a connection to the modules created as shown below −

以下代码创建了输入、隐藏和输出层之间的连接,并将连接添加到网络中。

Here is the code where we have created a connection between input, hidden and output layers and add the connection to the network.

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)

print(network)

Output

C:\pybrain\pybrain\src>python ffn.py
FeedForwardNetwork-3
Modules:
[]
Connections:
[]

我们仍然无法获得模块和连接。现在让我们添加最后一步,即我们需要添加 sortModules() 方法,如下所示 −

We are still not able to get the modules and connections. Let us now add the final step, i.e., we need to add the sortModules() method as shown below −

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'gt;, <SigmoidLayer 'SigmoidLayer-7'>,
   <LinearLayer 'LinearLayer-8'>]
Connections:
[<FullConnection 'FullConnection-4': 'SigmoidLayer-7' -> 'LinearLayer-8'>,
   <FullConnection 'FullConnection-5': 'LinearLayer-3' -> 'SigmoidLayer-7'>]

我们现在可以看到feedforwardnetwork的模块和连接详细信息。

We are now able to see the modules and the connections details for feedforwardnetwork.