1
我正在尝试对序列的实际长度进行均值运算。 (掩蔽零向量)使用动态长度的TensorFlow平均值
我的输入sequence_outpus
是(的batch_size,MAX_LEN,尺寸)
我有存储在批次中的每个序列的实际长度的张量。我从https://danijar.com/variable-sequence-lengths-in-tensorflow/
def length(sequence):
used = tf.sign(tf.reduce_max(tf.abs(sequence), reduction_indices=2))
length = tf.reduce_sum(used, reduction_indices=1)
length = tf.cast(length, tf.int64)
return length
使用该函数我这样做:
lengths = length(sequence_outputs)
lengths = tf.cast(length, tf.float32)
lengths = tf.expand_dims(lengths,1)
sentence_outputs = tf.reduce_sum(sentence_outputs,1)/lengths
图表编译但我得到NaN的损耗值。此外,当使用eval()进行调试时,我的长度变为负值。
这似乎是一个简单的问题,但我一直坚持这一段时间,并会感谢一些帮助!
谢谢!