2016-11-07 85 views
6

您好我正在爲lstm rnn cell使用以下函數。Tensor Flow - LSTM - 'Tensor'對象不可迭代

def LSTM_RNN(_X, _istate, _weights, _biases): 
    # Function returns a tensorflow LSTM (RNN) artificial neural network from given parameters. 
    # Note, some code of this notebook is inspired from an slightly different 
    # RNN architecture used on another dataset: 
    # https://tensorhub.com/aymericdamien/tensorflow-rnn 

    # (NOTE: This step could be greatly optimised by shaping the dataset once 
    # input shape: (batch_size, n_steps, n_input) 
    _X = tf.transpose(_X, [1, 0, 2]) # permute n_steps and batch_size 

    # Reshape to prepare input to hidden activation 
    _X = tf.reshape(_X, [-1, n_input]) # (n_steps*batch_size, n_input) 

    # Linear activation 
    _X = tf.matmul(_X, _weights['hidden']) + _biases['hidden'] 

    # Define a lstm cell with tensorflow 
    lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0) 


    # Split data because rnn cell needs a list of inputs for the RNN inner loop 
    _X = tf.split(0, n_steps, _X) # n_steps * (batch_size, n_hidden) 

    # Get lstm cell output 
    outputs, states = rnn.rnn(lstm_cell, _X, initial_state=_istate) 

    # Linear activation 
    # Get inner loop last output 
    return tf.matmul(outputs[-1], _weights['out']) + _biases['out'] 

函數的輸出存儲在pred變量下。

pred = LSTM_RNN(x, istate, weights, biases)

但它顯示了以下錯誤。 (其中指出,張量對象不是可迭代。)

以下是錯誤圖像鏈接 - http://imgur.com/a/NhSFK

請幫我這個,我很抱歉,如果這個問題似乎很傻,因爲我相當新的LSTM和張量流程庫。

謝謝。

+0

它的reshape命令,檢查http://stackoverflow.com/questions/33884978/build-a-graph-that-works-with-variable-batch-size-using-tensorflow –

+0

但是重塑命令是轉換爲所需形狀所必需的。 –

+0

當然,用tf.reshape(x,tf.pack(n_input,-1])試試吧) –

回答

6

發生錯誤時,它試圖解包state與語句c, h=state。根據您正在使用的張量流版本(您可以通過在python解釋器中輸入import tensorflow; tensorflow.__version__來檢查版本信息),在r0.11之前的版本中,初始化rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)state_is_tuple參數的默認設置設置爲False。請參閱此處的documentation

BasicLSTMCell documentation in r0.10

由於tensorflow版本r0.11(或主版),用於state_is_tuple默認設置被設定爲True。在這裏看到documentation

BasicLSTMCell documentation in r0.11

如果安裝r0.11或tensorflow的主版本,嘗試改變BasicLSTMCell起始線爲: lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=False)。你遇到的錯誤應該消失。雖然他們的網頁確實表示state_is_tuple=False行爲很快就會被棄用。

BasicLSTMCell state_is_tuple argument documentation

3

我碰巧遇到了在同一時間對同一問題。 我只是描述我的情況可能在V

它的狀態確實有助於爲後續

c1_ex, T1_ex = tf. ones(10,tf. int 32) 
raise Type Error ... 

我發現「=」事先已經設置載體的兩名

而左側對方只返回一個載體

對不起我的英語

的低效

您的問題實際上出現在線路146不行193