给定一个训练有素的LSTM模型,我想对单个时间步进执行推理,即下例中的seq_length = 1
。在每个时间步后,内部LSTM(内存和隐藏)状态需要记住下一个'批'。对于推论的开始,内部LSTM状态init_c, init_h
根据输入进行计算。然后将它们存储在传递给LSTM的对象LSTMStateTuple
中。在训练期间,这个状态每次更新都会更新。然而,为了推断,我希望state
可以在批次之间保存,即只需要在开始时计算初始状态,然后在每个“批量”(n = 1)之后应该保存LSTM状态。TensorFlow:记住下一批次的LSTM状态(有状态的LSTM)
我发现这个相关的StackOverflow问题:Tensorflow, best way to save state in RNNs?。然而,这只适用于state_is_tuple=False
,但这种行为很快将由TensorFlow弃用(请参阅rnn_cell.py)。 Keras似乎有一个很好的包装,使有状态 LSTMs可能,但我不知道在TensorFlow中实现这一点的最佳方法。 TensorFlow GitHub上的这个问题也与我的问题有关:https://github.com/tensorflow/tensorflow/issues/2838
任何有关构建有状态LSTM模型的好建议?
inputs = tf.placeholder(tf.float32, shape=[None, seq_length, 84, 84], name="inputs")
targets = tf.placeholder(tf.float32, shape=[None, seq_length], name="targets")
num_lstm_layers = 2
with tf.variable_scope("LSTM") as scope:
lstm_cell = tf.nn.rnn_cell.LSTMCell(512, initializer=initializer, state_is_tuple=True)
self.lstm = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * num_lstm_layers, state_is_tuple=True)
init_c = # compute initial LSTM memory state using contents in placeholder 'inputs'
init_h = # compute initial LSTM hidden state using contents in placeholder 'inputs'
self.state = [tf.nn.rnn_cell.LSTMStateTuple(init_c, init_h)] * num_lstm_layers
outputs = []
for step in range(seq_length):
if step != 0:
scope.reuse_variables()
# CNN features, as input for LSTM
x_t = # ...
# LSTM step through time
output, self.state = self.lstm(x_t, self.state)
outputs.append(output)
[Tensorflow,在RNN中保存状态的最佳方式是?]的可能重复(http://stackoverflow.com/questions/37969065/tensorflow-best-way-to-save-state-in-rnns) –