Tensorflow 张量流中的LSTM反向传播

Tensorflow 张量流中的LSTM反向传播,tensorflow,lstm,Tensorflow,Lstm,从部门截断反向传播的官方PTB google教程中,有一个实现使用BasicLSTMCell,通过创建一个for循环来展开num_步骤的图形 # Placeholder for the inputs in a given iteration. words = tf.placeholder(tf.int32, [batch_size, num_steps]) lstm = rnn_cell.BasicLSTMCell(lstm_size) # Initial state of the LSTM

从部门截断反向传播的官方PTB google教程中,有一个实现使用BasicLSTMCell,通过创建一个for循环来展开num_步骤的图形

# Placeholder for the inputs in a given iteration.
words = tf.placeholder(tf.int32, [batch_size, num_steps])

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)

# The rest of the code.
# ...

final_state = state
我已经使用BasicLSTMCell实现了一个预测时间序列的方法,不同的是我没有在图中使用任何循环,而是在程序执行循环中更新lstmCells的状态。代码如下:

input_layer = tf.placeholder(tf.float32, [input_width, input_dim * 1])
lstm_cell1 = tf.nn.rnn_cell.BasicLSTMCell(input_dim * input_width)
lstm_state1 = tf.Variable(tf.zeros([input_width,lstm_cell1.state_size]))
lstm_output1, lstm_state_output1 = lstm_cell1(input_layer, lstm_state1, scope='LSTM1')
lstm_update_op1 = lstm_state1.assign(lstm_state_output1)

for i in range(39000):
    input_v, output_v = get_new_input_output(i, A)
    _, _, network_output = sess.run([lstm_update_op1, train_step, final_output],
                                feed_dict={input_layer: input_v, correct_output: output_v})

第二个实现是如何通过时间实现tha反向传播的,这是否是tensorflow对lstmCell的正确使用。就个人而言,我更喜欢第二种实现,因为我发现它更清晰,而且还能够支持数据流。但谷歌展示第一个实现的事实让我怀疑我做错了什么。

为了在训练期间通过时间进行反向传播,图形需要在向前传递期间存储所有张量的值,以便在反向传递期间使用它们来计算梯度。在您的代码中,正向传递应该是wordfine(尽管我没有测试它),但是反向传递无法工作,因为图形无法在正向传递期间保留张量值(因为
assign()
op)

我建议你看看Danijar Hafner写的。它解释了如何使用
dynamic\u rnn()
函数来执行您想要的操作