【问题标题】:How to realize a more complex initial_state for LSTM in tensorflow如何在tensorflow中为LSTM实现更复杂的initial_state
【发布时间】: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 看起来像这样:

simple_initial-state

图中蓝色是 zero_state,绿色是 2 个 LSTM 层,列都是相同的单元格,应该只显示重复,箭头显示状态从一步到下一步的传递。

现在我想使用更复杂的 initial_state,不仅将状态从一个步骤传递到下一步,而且还从一层传递到另一层:

wanted complex initial_state

这就是我现在卡住的地方。 我只是在initial_state 中添加了额外的元组,但这导致我出现ValueError 之类的错误:要解包的值太多(预期为2)。 我也在查看其他类型的 LSTM 单元,但无法确定需要的。

所以我的问题是,我怎样才能在 tensorflow 中实现这个更复杂的 initial_state,例如使用哪些细胞类型或如何塑造 initial_state?

提前谢谢你。

【问题讨论】:

    标签: python-3.x tensorflow lstm


    【解决方案1】:

    您的单元状态必须是单个数字向量,其大小必须在 LSTM 单元中定义。 LSTM 单元将输出与预期输入相同的大小。当您有多个层时,您通常将信息从一层传递到下一层,通过第一层的输出到第二层的输入。您通常不会传递状态。但是,如果您想以这种方式进行实验,我想这样做的方法是将两种状态加在一起,因为您无法真正改变形状。您可能会考虑将残差网络的优先级作为这种想法的理由(尽管我不知道这是否是一个好想法)。

    如果您想输出与输入不同的单元状态大小,以便您可以在输入单元状态内实际连接之前层的状态,您将不得不手动编码 RNN,而不是 LSTM 单元wrapper 将允许这种配置。

    为了更好地了解 LSTM 单元的内部结构,我在此阅读:

    http://colah.github.io/posts/2015-08-Understanding-LSTMs/

    【讨论】:

    • 非常感谢。将各州合并在一起是个好主意!
    猜你喜欢
    • 2017-07-18
    • 2016-11-01
    • 2019-06-13
    • 2018-04-05
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多