循环神经网络(LSTM)的输入/输出长度任意。

3

以下是使用Elman循环神经网络Neurolab Python库一个示例:

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()

在这个例子中,它正在合并两个单位长度的输入,并且也在合并两个单位长度的输出。之后,它使用这些合并的数组来训练网络。
首先,它似乎不像我从这里得到的模式。

enter image description here

我的主要问题是:
我必须训练具有任意长度的输入和输出的网络,例如:
  • 任意长度的输入到固定长度的输出
  • 固定长度的输入到任意长度的输出
  • 任意长度的输入到任意长度的输出
此时,你可能会想到:“你的答案是 长短期记忆网络。”
我知道 Neurolab 很容易使用,因为它有很多 优秀的特性。尤其是它非常符合 Pythonic 的风格。因此,我坚持使用 Neurolab Library 来解决我的问题。但是如果你建议我使用另一个像 Neurolab 一样拥有更好 LSTM 功能的库,我会接受的
最终,我怎么能够为任意长度的输入和输出重新排列这个例子呢?
我对 RNN 和 LSTM 没有最好的理解,请解释一下。

问题已解决,还需要我回答吗? - Rajarshee Mitra
@RajarsheeMitra,你仍然可以回答。 - mertyildiran
这个问题的答案是什么?使用任意长度的输入和输出? - Uzair
@Uzair,我回答了自己的问题,请查看:https://dev59.com/f5Tfa4cB1Zd3GeqPSIqB#43688984 - mertyildiran
1个回答

2

当我今天再看这个问题时,我发现它是一个缺乏神经网络理解的人提出的问题。

矩阵乘法是神经网络核心的基础数学。你不能简单地改变输入矩阵的形状,因为它会改变乘积的形状并破坏数据集之间的一致性。

神经网络始终使用固定长度的输入和输出进行训练。这是一个非常简单的神经网络实现,仅使用numpy的点积进行前馈:

import numpy as np

# sigmoid function
def nonlin(x,deriv=False):
    if(deriv==True):
        return x*(1-x)
    return 1/(1+np.exp(-x))

# input dataset
X = np.array([  [0,0,1],
                [0,1,1],
                [1,0,1],
                [1,1,1] ])

# output dataset            
y = np.array([[0,0,1,1]]).T

# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)

# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1

for iter in xrange(10000):

    # forward propagation
    l0 = X
    l1 = nonlin(np.dot(l0,syn0))

    # how much did we miss?
    l1_error = y - l1

    # multiply how much we missed by the 
    # slope of the sigmoid at the values in l1
    l1_delta = l1_error * nonlin(l1,True)

    # update weights
    syn0 += np.dot(l0.T,l1_delta)

print "Output After Training:"
print l1

来源:http://iamtrask.github.io/2015/07/12/basic-python-network/

本文向您介绍了使用Python编写基础神经网络的过程,包括使用NumPy库进行数学计算和实现前馈(feedforward)过程、梯度下降法(Gradient Descent)训练模型等。同时,还解释了神经网络是如何学习分类任务的,并提供了一个简单的代码例子以加深理解。


我有两个nd数组,第一个数组的大小为(45,1707),第二个数组的大小为(1707,)...你有任何想法如何训练这两个数组吗? - Uzair
@Uzair,你需要使用numpy.ndarray.flatten将第一个数组从(45,1707)压缩为(76815,)。我假设第一个数组是你的输入,第二个数组是你的输出/目标。 (76815,)是一个巨大的数组大小,可能会在实际训练网络时变得不可行。 - mertyildiran
是的,我尝试过这个,但它的大小非常大,我该如何训练?有没有什么方法可以训练? - Uzair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接