Pybrain 简明教程

PyBrain - Working with Recurrent Networks

循环网络和前馈网络相同,其唯一的区别在于您需要记住在每个步骤中的数据。每个步骤的历史记录都必须保存。

我们将学习如何:

  1. Create a Recurrent Network

  2. Adding Modules and Connection

Creating a Recurrent Network

创建循环网络,我们将使用RecurrentNetwork类,如下所示:

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:
[]

我们可以看到循环网络的新连接称为循环连接。目前没有可用数据。

现在,让我们创建图层并添加到模块并创建连接。

Adding Modules and Connection

我们将创建图层,即输入、隐藏和输出。这些图层将添加到输入和输出模块。接下来,我们将创建输入到隐藏、隐藏到输出和隐藏到隐藏之间的循环连接。

以下是具有模块和连接的循环网络的代码。

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'>]

在以上输出中,我们可以看到模块、连接和循环连接。

现在让我们使用activate方法激活该网络,如下所示:

rn.py

将以下代码添加到之前创建的代码:

#activate network using activate() method
act1 = recurrentn.activate((2, 2))
print(act1)

act2 = recurrentn.activate((2, 2))
print(act2)

python rn.py

C:\pybrain\pybrain\src>python rn.py
[-1.24317586]
[-0.54117783]