給定一個訓練有素的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) –