Pybrain 简明教程
PyBrain - Working with Recurrent Networks
循环网络和前馈网络相同,其唯一的区别在于您需要记住在每个步骤中的数据。每个步骤的历史记录都必须保存。
Recurrent Networks is same as feed-forward network with only difference that you need to remember the data at each step.The history of each step has to be saved.
我们将学习如何:
We will learn how to −
-
Create a Recurrent Network
-
Adding Modules and Connection
Creating a Recurrent Network
创建循环网络,我们将使用RecurrentNetwork类,如下所示:
To create recurrent network, we will use RecurrentNetwork class as shown below −
rn.py
from pybrain.structure import RecurrentNetwork
recurrentn = RecurrentNetwork()
print(recurrentn)
python rn.py
C:\pybrain\pybrain\src>python rn.py
RecurrentNetwork-0
Modules:
[]
Connections:
[]
Recurrent Connections:
[]
我们可以看到循环网络的新连接称为循环连接。目前没有可用数据。
We can see a new connection called Recurrent Connections for the recurrent network. Right now there is no data available.
现在,让我们创建图层并添加到模块并创建连接。
Let us now create the layers and add to modules and create connections.
Adding Modules and Connection
我们将创建图层,即输入、隐藏和输出。这些图层将添加到输入和输出模块。接下来,我们将创建输入到隐藏、隐藏到输出和隐藏到隐藏之间的循环连接。
We are going to create layers, i.e., input, hidden and output. The layers will be added to the input and output module. Next, we will create the connection for input to hidden, hidden to output and a recurrent connection between hidden to hidden.
以下是具有模块和连接的循环网络的代码。
Here is the code for the Recurrent network with modules and connections.
rn.py
from pybrain.structure import RecurrentNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection
recurrentn = RecurrentNetwork()
#creating layer for input => 2 , hidden=> 3 and output=>1
inputLayer = LinearLayer(2, 'rn_in')
hiddenLayer = SigmoidLayer(3, 'rn_hidden')
outputLayer = LinearLayer(1, 'rn_output')
#adding the layer to feedforward network
recurrentn.addInputModule(inputLayer)
recurrentn.addModule(hiddenLayer)
recurrentn.addOutputModule(outputLayer)
#Create connection between input ,hidden and output
input_to_hidden = FullConnection(inputLayer, hiddenLayer)
hidden_to_output = FullConnection(hiddenLayer, outputLayer)
hidden_to_hidden = FullConnection(hiddenLayer, hiddenLayer)
#add connection to the network
recurrentn.addConnection(input_to_hidden)
recurrentn.addConnection(hidden_to_output)
recurrentn.addRecurrentConnection(hidden_to_hidden)
recurrentn.sortModules()
print(recurrentn)
python rn.py
C:\pybrain\pybrain\src>python rn.py
RecurrentNetwork-6
Modules:
[<LinearLayer 'rn_in'>, <SigmoidLayer 'rn_hidden'>,
<LinearLayer 'rn_output'>]
Connections:
[<FullConnection 'FullConnection-4': 'rn_hidden' -> 'rn_output'>,
<FullConnection 'FullConnection-5': 'rn_in' -> 'rn_hidden'>]
Recurrent Connections:
[<FullConnection 'FullConnection-3': 'rn_hidden' -> 'rn_hidden'>]
在以上输出中,我们可以看到模块、连接和循环连接。
In above ouput we can see the Modules, Connections and Recurrent Connections.
现在让我们使用activate方法激活该网络,如下所示:
Let us now activate the network using activate method as shown below −