【问题标题】:Arbitrary length of input/output for Recurrent Neural Network (LSTM)循环神经网络 (LSTM) 的任意输入/输出长度
【发布时间】: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


【解决方案1】:

今天看了我这个问题很久,发现是一个对神经网络缺乏了解的人的问题。

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

神经网络总是使用固定长度的输入和输出进行训练。这是一个非常简单的神经网络实现,它只使用 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/

【讨论】:

  • 我有两个数组,第一个数组大小是 (45,1707),第二个数组大小是 (1707,)...你知道如何训练这两个数组吗?跨度>
  • @Uzair 您需要使用numpy.ndarray.flatten 将第一个数组从 (45,1707) 展平到 (76815,)。我假设第一个数组是您的输入,第二个是您的输出/目标。 (76815,) 是一个巨大的数组大小,可能几乎不可能训练该网络。
  • 是的,我试试这个,但它的尺寸非常大,我该如何训练?有什么方法可以训练吗?
猜你喜欢
  • 2016-03-06
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
相关资源
最近更新 更多