Tensorflow占位符的一个问题是,您只能使用python列表或Numpy数组来提供它(我认为)。因此,您无法在LSTMStateTuple的元组中保存两次运行之间的状态。
initial_state = np.zeros((num_layers, 2, batch_size, state_size))
在LSTM层中有两个组件, 和 ,这就是“ 2”的含义。(这篇文章很棒:https ://arxiv.org/pdf/1506.00019.pdf)
构建图时,您将解压缩并创建元组状态,如下所示:
state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size])
l = tf.unpack(state_placeholder, axis=0)
rnn_tuple_state = tuple(
[tf.nn.rnn_cell.LSTMStateTuple(l[idx][0],l[idx][1])
for idx in range(num_layers)]
)
然后您以通常的方式获得新状态
cell = tf.nn.rnn_cell.LSTMCell(state_size, state_is_tuple=True)
cell = tf.nn.rnn_cell.MultiRNNCell([cell] * num_layers, state_is_tuple=True)
outputs, state = tf.nn.dynamic_rnn(cell, series_batch_input, initial_state=rnn_tuple_state)
可能不应该这样……也许他们正在研究解决方案。