【发布时间】:2018-03-22 10:47:55
【问题描述】:
我目前正在使用 tensorflow 和 python 处理多层 LSTM。 我正在使用 tf.nn.dynamic_rnn 中的 initial_state 将单元的先前状态传递给下一步。
像这样创建图层:
cells = []
for c in range(0, num_layers):
cells.append(tf.nn.rnn_cell.BasicLSTMCell(num_units = num_units, forget_bias = 1.0, activation = tf.nn.tanh))
basic_cell = tf.nn.rnn_cell.MultiRNNCell(cells)
state_series, current_state = tf.nn.dynamic_rnn(basic_cell, x, dtype=tf.float32, initial_state = rnn_tuple_state)
像这样的类型和尺寸:
rnn_tuple_state: <class 'tuple'> len:num_layers
Layer 0 : <class 'tensorflow.python.ops.rnn_cell_impl.LSTMStateTuple'> len:2
cell: <class 'tensorflow.python.framework.ops.Tensor'> dimensions:(outputs, truncated_backprop_len)
hidden: <class 'tensorflow.python.framework.ops.Tensor'> dimensions:(outputs, truncated_backprop_len)
Layer 1 : ...
...
Layer num_layers : ...
state_series: <class 'tensorflow.python.framework.ops.Tensor'> dimension:(outputs, truncated_backprop_len, num_units)
current_state: <class 'tuple'> len:num_layers
Layer 0 : <class 'tensorflow.python.ops.rnn_cell_impl.LSTMStateTuple'> len:2
cell_state: <class 'tensorflow.python.framework.ops.Tensor'> (7, 360) dimensions:(outputs, truncated_backprop_len)
hidden_state: <class 'tensorflow.python.framework.ops.Tensor'> (7, 360) dimensions:(outputs, truncated_backprop_len)
Layer 1 : ...
...
Layer num_layers: ...
使用这个我能够实现状态空间 LSTM 看起来像这样:
图中蓝色是 zero_state,绿色是 2 个 LSTM 层,列都是相同的单元格,应该只显示重复,箭头显示状态从一步到下一步的传递。
现在我想使用更复杂的 initial_state,不仅将状态从一个步骤传递到下一步,而且还从一层传递到另一层:
这就是我现在卡住的地方。 我只是在initial_state 中添加了额外的元组,但这导致我出现ValueError 之类的错误:要解包的值太多(预期为2)。 我也在查看其他类型的 LSTM 单元,但无法确定需要的。
所以我的问题是,我怎样才能在 tensorflow 中实现这个更复杂的 initial_state,例如使用哪些细胞类型或如何塑造 initial_state?
提前谢谢你。
【问题讨论】:
标签: python-3.x tensorflow lstm