Python 计算tensorflow 2.0中句子填充嵌入查找中的原始序列长度

Python 计算tensorflow 2.0中句子填充嵌入查找中的原始序列长度,python,tensorflow,tensorflow2.0,Python,Tensorflow,Tensorflow2.0,text\u tensor是形状为[None,sequence\u max\u length,embedding\u dim]的张量,包含一批序列的嵌入查找。序列用零填充。我需要在shape[None]中获得一个名为text\u length的列表(None是批次大小),其中包含每个序列的长度(不带填充)。我试过几个剧本 我得到的最接近的代码如下: text_lens = tf.math.reduce_sum(tf.cast(tf.math.not_equal(text_tensor, tf.

text\u tensor
是形状为
[None,sequence\u max\u length,embedding\u dim]
的张量,包含一批序列的嵌入查找。序列用零填充。我需要在shape
[None]
中获得一个名为
text\u length
的列表(None是批次大小),其中包含每个序列的长度(不带填充)。我试过几个剧本

我得到的最接近的代码如下:

 text_lens = tf.math.reduce_sum(tf.cast(tf.math.not_equal(text_tensor, tf.as_tensor(numpy.zeros([embedding_dim]))), dtype=tf.int32), axis=-1)

但是仍然错误地计算了长度。有人能帮我吗?

如果我理解正确,在序列的原始长度之后,对于第一个轴的剩余索引,您可以得到0的大小
嵌入\u dim

import tensorflow as tf

# batch_size = 2, first sequence length = 1, second sequence length = 3
data = [[[1, 1, 1, 1],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [0, 0, 0, 0]]]

with tf.compat.v1.Session() as sess:
    tensor = tf.constant(data, dtype=tf.int32)
    check = tf.reduce_all(tf.not_equal(tensor, 0), axis=-1)
    lengths = tf.reduce_sum(tf.cast(check, tf.int32), axis=-1)
    print(sess.run(lengths))
输出

[1 3]

零是否仅作为填充的一部分出现,或者它们也可能是序列原始长度内计算的结果?
但仍然计算不正确。
这在实践中意味着什么?你有一个例子吗@‌只有填充张量是完全零。