2017-05-18 89 views
0

我想建立情緒分析模型中TensorflowBasicRNNCell對象不是可迭代

def rnn_lstm(weights, biases, data_x, sequence_length, vocab_size, embedding_size): 
    # Use Tensor Flow embedding lookup and convert the input data set 
    with tf.device("/cpu:0"): 
     embedding = tf.get_variable("embedding43", [vocab_size, embedding_size]) 
     embedded_data = tf.nn.embedding_lookup(embedding, data_x) 
     embedded_data_dropout = tf.nn.dropout(embedded_data, rnn_dropout_keep_prob) 

    #add LSTM cell and dropout nodes 
    rnn_lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(rnn_cell_size, forget_bias = rnn_lstm_forget_bias) 
    rnn_lstm_cell = tf.contrib.rnn.core_rnn_cell.DropoutWrapper(rnn_lstm_cell, output_keep_prob = rnn_dropout_keep_prob) 



    rnn_data_X = embedded_data_dropout 
    # Permuting batch_size and sequence_length 
    rnn_data_X = tf.transpose(rnn_data_X, [1, 0, 2]) 
    #print ("RNN After transpose rnn_data_X: ", rnn_data_X) 
    # Reshaping to (sequence_length * batch_size, rnn_data_vec_size) 
    rnn_data_X = tf.reshape(rnn_data_X, [-1, rnn_data_vec_size]) 
    #print ("RNN After reshape rnn_data_X: ", rnn_data_X) 
    # Split to get a list of 'sequence_length' tensors of shape (batch_size, rnn_data_vec_size) 
    rnn_data_X = tf.split(rnn_data_X,sequence_length,0) 
    #print ("RNN After split len(rnn_data_X): ", len(rnn_data_X), rnn_data_X[0]) 

    # Get lstm cell output 
    outputs, states = tf.contrib.rnn.core_rnn_cell.BasicRNNCell(rnn_lstm_cell, rnn_data_X) 


    output = tf.matmul(outputs[-1], weights) + biases 
    return output 

但扔給我一個錯誤,這是BasicRNNCell對象不是可迭代。任何想法請

回答

0

的問題是該行:

# Get lstm cell output 
outputs, states = tf.contrib.rnn.core_rnn_cell.BasicRNNCell(rnn_lstm_cell, rnn_data_X) 

那不是如何你應該使用復發的細胞。 rnn_lstm_cell已經是一種(類型)複發性細胞;使用它,你需要調用tf.nn.dynamic_rnn

# Get lstm cell output 
outputs, states = tf.nn.dynamic_rnn(rnn_lstm_cell, rnn_data_X) 

您可以瞭解更多關於復發模型TensorFlow here

+0

謝謝你的回答,但是現在當我改變爲動態ValueError後,我得到了這個錯誤:對於'transpose_6'(op:'Transpose'),尺寸必須爲2,但輸入形狀爲[?,128], [3]。 – angel