【问题标题】:Concatenate Two RNN States in Tensorflow在 Tensorflow 中连接两个 RNN 状态
【发布时间】:2017-10-24 05:57:39
【问题描述】:

我试图在张量流中将两个 LSTM 状态连接在一起。以前,这是使用 tf.concat 完成的,但这对我不起作用,因为我需要计算结果状态的 logits,并且自 'Tensor' object is not iterable 以来我无法遍历结果状态。

这是我想要做的:

outputs, fstate = tf.nn.dynamic_rnn(cell=lstm, inputs=rnn_inputs,
                                      sequence_length=lengths, 
                                      dtype=tf.float32, time_major=False)
outputs2, fstate2 = tf.nn.dynamic_rnn(cell=lstm2, inputs=rnn_inputs2,
                                      sequence_length=lengths2, 
                                      dtype=tf.float32, time_major=False)

newRnnState = tf.concat([fstate, fstate2], 1)
logits = tf.matmul(tf.concat([f.h for f in newRnnState], 1), output_layer[0]) + output_bias[0]

这会返回错误:TypeError: 'Tensor' object is not iterable.

有什么方法可以将两个 RNN 状态连接在一起并像这样使用它们?

谢谢

【问题讨论】:

  • 问题是 newRnnState 是张量而不是数组,这就是你有这个错误的原因。根据你想做的,你可以试试这个:logits = tf.matmul(tf.concat([fstate.h, fstate2.h], 1), output_layer[0]) + output_bias[0]
  • @AnthonyD'amato 这不起作用,因为 fstate 是 LSTMStateTuple。但是,如果我这样做logits[0] = tf.matmul(tf.concat([fstate[0].h, fstate2[0].h], 1), output_layer[0]) + output_bias[0],它会成功连接,但后来抱怨输出形状不正确。形状是(9, 512),它应该是(9, 256),因为每个LSTM单元的大小为256。因此,它不能与output_layer[0]相乘,因为它的大小为(256, output)。我不认为正确的解决方案是改变output_layer 的大小,或者是吗?
  • 哈,是的,对不起,我忘了它是一个元组,你是对的。但是由于每个 LSTM 的单元格大小为 256,那么如果您沿轴 1 连接两个输出,您将得到 512。我想说您必须将 output_layer 大小更改为 (cell_size*2, output)
  • 好的,谢谢!如果您将其发布为答案,我可以将其标记为正确。

标签: python tensorflow nlp deep-learning


【解决方案1】:

根据我们在 cmets 中所说的,您应该将输出层的大小设置为(2*cell_size, output_size),因为您正在沿轴 1 连接 2 个 cell_size 大小的状态。然后您可以使用: tf.matmul(tf.concat([fstate[0].h, fstate2[0].h], 1), output_layer[0]) + output_bias[0]

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多