Tensorflow:从具有可变序列长度的双向 rnn 获取输出

标签 tensorflow lstm

我将 tf.nn.bidirectional_rnnsequence_length 参数一起用于可变输入大小,但我无法弄清楚如何获得每个的最终输出小批量样本:

output, _, _ = tf.nn.bidirectional_rnn(forward1,backward1,input,dtype=tf.float32,sequence_length=input_lengths)

现在,如果我有恒定的序列长度,我会简单地使用 output[-1] 并获得最终输出。在我的例子中,我有可变序列(它们的长度是已知的)。 另外,这个输出是前向和后向 LSTM 的输出吗?

谢谢。

最佳答案

这个问题看源码就可以回答rnn.py .

对于具有动态长度的序列,源代码说:

If the sequence_length vector is provided, dynamic calculation is performed. This method of calculation does not compute the RNN steps past the maximum sequence length of the minibatch (thus saving computational time), and properly propagates the state at an example's sequence length to the final state output.

因此,为了获得实际的最后输出,您应该对结果输出进行切片。

对于bidirectional_rnn,源码说:

A tuple (outputs, output_state_fw, output_state_bw) where: outputs is a length T list of outputs (one for each input), which are depth-concatenated forward and backward outputs. output_state_fw is the final state of the forward rnn. output_state_bw is the final state of the backward rnn.

因此,输出是元组而不是张量。 如果您愿意,可以将此元组连接成一个向量。

关于Tensorflow:从具有可变序列长度的双向 rnn 获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821069/

相关文章:

python - 极其缓慢构建双向 TF Lstm 单元

tensorflow - 使用 LSTM 从单元素序列进行预测

python - 在训练期间将非张量参数传递给 Keras 模型/使用张量进行索引

python-3.x - 如何在 tensorflow 中使用 python3 预测 LSTM 模型中的情绪?

python - 错误 : Could not find a version that satisfies the requirement tensorflow (from versions: none) ERROR: No matching distribution found for tensorflow)

python - tensorflow 2.0.0 中 tf.one_hot 的等价物是什么

python - TensorFlow:卡住模型似乎只存储输出节点?

machine-learning - LSTM网络学习

python - 训练 LSTM 模型