对如何运行tensorflow LSTM感到困惑

时间:2016-06-03 18:03:48

标签: tensorflow lstm

我已经看到了两种在张量流上调用lstm的不同方法,我对一种方法与另一种方法的尊重感到困惑。在哪种情况下使用一个或另一个

第一个是创建一个lstm然后立即调用它,就像下面的代码一样

lstm = rnn_cell.BasicLSTMCell(lstm_size)
# Initial state of the LSTM memory.
initial_state = state = tf.zeros([batch_size, lstm.state_size])

for i in range(num_steps):
    # The value of state is updated after processing each batch of words.
    output, state = lstm(words[:, i], state)

第二个是通过rnn.rnn()调用lstm cell,如下所示。

# Define a lstm cell with tensorflow
lstm = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
# Split data because rnn cell needs a list of inputs for the RNN inner loop
inputToLstmSplited = tf.split(0, n_steps, inputToLstm) # n_steps * (batch_size, n_hidden)

inputToLstmSplitedFiltered = tf.matmul(inputToLstmSplited, weights['hidden']) + biases['hidden']

# Get lstm cell out
outputs, states = rnn.rnn(lstm, inputToLstmSplited, initial_state=istate)

1 个答案:

答案 0 :(得分:1)

第二个有效地与第一个循环相同,返回循环和最终状态中收集的所有输出的列表。它通过一些安全检查来提高效率。它还支持有用的功能,如可变序列长度。第一个选项在Tensorflow教程中提供,以便您了解如何解开RNN,但第二个选项是“生产”代码的首选。