【发布时间】:2016-02-27 21:05:32
【问题描述】:
这是an example 使用Elman Recurrent Neural Network 来自Neurolab Python Library:
import neurolab as nl
import numpy as np
# Create train samples
i1 = np.sin(np.arange(0, 20))
i2 = np.sin(np.arange(0, 20)) * 2
t1 = np.ones([1, 20])
t2 = np.ones([1, 20]) * 2
input = np.array([i1, i2, i1, i2]).reshape(20 * 4, 1)
target = np.array([t1, t2, t1, t2]).reshape(20 * 4, 1)
# Create network with 2 layers
net = nl.net.newelm([[-2, 2]], [10, 1], [nl.trans.TanSig(), nl.trans.PureLin()])
# Set initialized functions and init
net.layers[0].initf = nl.init.InitRand([-0.1, 0.1], 'wb')
net.layers[1].initf= nl.init.InitRand([-0.1, 0.1], 'wb')
net.init()
# Train network
error = net.train(input, target, epochs=500, show=100, goal=0.01)
# Simulate network
output = net.sim(input)
# Plot result
import pylab as pl
pl.subplot(211)
pl.plot(error)
pl.xlabel('Epoch number')
pl.ylabel('Train error (default MSE)')
pl.subplot(212)
pl.plot(target.reshape(80))
pl.plot(output.reshape(80))
pl.legend(['train target', 'net output'])
pl.show()
在本例中,它是合并 2 单位长度的输入,同时它也在合并2 单位长度的输出。之后,它使用这些合并数组训练网络。
首先,它看起来不像我从here 得到的架构:
我的主要问题是;
我必须用任意长度的 输入和输出来训练网络,如下所示:
- 任意长度输入到固定长度输出
- 固定长度输入到任意长度输出
- 任意长度输入到任意长度输出
此时你会想到:“你的答案是Long short-term memory networks。”
我知道,但Neurolab 易于使用,因为它是good features。特别是,它非常Pythonic。所以我坚持使用 Neurolab Library 来解决我的问题。但是,如果您向我推荐另一个库,例如具有更好 LSTM 功能的 Neurolab,我会接受它。
最后,我怎样才能为任意长度的输入和输出重新排列这个示例?
我对 RNN 和 LSTM 的了解不是很透彻,所以请解释一下。
【问题讨论】:
-
解决了问题还是我还能回答?
-
@RajarsheeMitra 你仍然可以回答。
-
这个答案是什么?使用任意长度的输入和输出?
-
@Uzair 我回答了我自己的问题。请查看:stackoverflow.com/a/43688984/2104879
标签: python neural-network lstm recurrent-neural-network