2016-02-27 184 views
2

這是從Neurolab Python Library使用Elman Recurrent Neural Networkan example爲迴歸神經網絡(LSTM)輸入/輸出的任意長度的

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得到了架構:

enter image description here

我的主要問題是;

我具有到網絡的輸入輸出喜歡這些任意長度培養:

  • 任意長度的輸入,以固定長度的輸出
  • 固定長度輸入到任意長度輸出
  • 任意長度輸入到任意長度輸出

在這一點上,你會想到:「你的回答是Long short-term memory networks。」

我知道它但Neurolab易於使用,因爲它是good features。特別是,它特別是Pythonic。所以我堅持使用Neurolab圖書館爲我的問題。但如果你建議我另一個庫像Neurolab更好的LSTM功能,我會接受它

最後,如何重新排列這個例子的任意長度的輸入和輸出?

我對RNN和LSTM沒有最好的理解,所以請說明一下。

+0

解決問題,或者我還可以回答? –

+0

@RajarsheeMitra你仍然可以回答。 – mertyildiran

+0

這是什麼答案?使用任意長度的輸入和輸出? – Uzair

回答

0

經過很長一段時間,當我看到今天的這個問題時,我可以看到這是一個對神經網絡缺乏瞭解的人的問題。

矩陣乘法是神經網絡核心的基本數學。您不能簡單地改變輸入矩陣的形狀,因爲它會改變產品的形狀並破壞數據集之間的一致性。

神經網絡總是與輸入和輸出的固定長度的訓練。這裏是一個非常簡單的神經網絡實現,使用只是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/

+0

我有兩個nd數組,第一個數組大小是(45,1707),第二個數組大小是(1707,)...你有沒有想法如何訓練這兩個陣列? – Uzair

+0

@Uzair你需要使用[numpy.ndarray.flatten](https://docs.scipy.org/doc/numpy/reference/generated/numpy)將第一個數組從(45,1707)平鋪到(76815)。 ndarray.flatten.html)。我假設第一個數組是你的輸入,第二個數組是你的輸出/目標。 (76815,)是一個巨大的數組大小,並且可能實際上不可能訓練該網絡。 – mertyildiran

+0

是的,我嘗試這個,但它的大小非常大我該如何訓練?有什麼方法可以訓練? – Uzair