PyBrain-使用递归网络

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)

To create recurrent network, we will use RecurrentNetwork class as shown below −

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

py (rn.py)


from pybrain.structure import RecurrentNetwork
recurrentn = RecurrentNetwork()
print(recurrentn)

python rn.py (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.

这是带有模块和连接的循环网络的代码。

py (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 (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.

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

Let us now activate the network using activate method as shown below −

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

py (rn.py)

Add below code to the one created earlier −

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


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

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

python rn.py (python rn.py)


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

翻译自: https://www.tutorialspoint.com/pybrain/pybrain_working_with_recurrent_networks.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值