input/output 循环神经网络 (LSTM) 的任意长度

Arbitrary length of input/output for Recurrent Neural Network (LSTM)

这是an example of use Elman Recurrent Neural Network from 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:

得到的架构

我的主要问题是;

我必须用 任意长度的 输入 输出 来训练网络,就像这样:

此时你会想到:"Your answer is Long short-term memory networks."

我知道,但是 Neurolab is easy to use because of it's good features. Particularly, it is exceptionally Pythonic. So I'm insisting on using Neurolab Library for my problem. But if you suggest me another library like Neurolab with better LSTM 功能,我会接受它

最后,我如何为任意长度的输入和输出重新排列此示例?

我对 RNN 和 LSTM 不是很了解,所以请解释一下。

今天看了很久的这道题,才知道这是一个对神经网络缺乏了解的人的题。

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

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