Python 无降维的Tensorflow序列掩码

Python 无降维的Tensorflow序列掩码,python,tensorflow,Python,Tensorflow,我有一个要运行的示例代码: import numpy a np import tensorflow as tf import tensorflow.contrib.eager as tfe tfe.enable_eager_execution() x = np.random.randint(100,size=(4,4)) indexes =tf.sequence_mask([1,2,2,4],4) """ indexes = [ [True,False,False,False],

我有一个要运行的示例代码:

import numpy a np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()

x = np.random.randint(100,size=(4,4))
indexes =tf.sequence_mask([1,2,2,4],4)
"""
indexes = [
    [True,False,False,False],
    [True,True,False,False],
    [True,True,False,False],
    [True,True,True,True],
]
"""

y = tf.boolean_mask(x,indexes)
# y = array([43, 78, 68, 54, 46, 28, 15, 52,  3])
现在,我不想这样,因为原始张量的空间信息丢失了,我想保持形状完整。在tensorflow中如何做到这一点,因为我使用的是RNN数据,所以我的tensor大小是=
[批大小、最大时间、特征长度]
,在这里我将对其进行切片,以便:

index=tf.sequence\u mask([x\u 1,x\u 2,x\u 3,…,x\u批量大小],最大时间)

但仍然希望保持形状完整。如果不可能的话,有没有一种方法可以对如此大小的多个张量进行序列屏蔽,同时将它们连接起来,以便只保留提取的序列,而不保留被屏蔽的填充?可以在连接结束时应用填充物。

这可能有效-

x = tf.constant([[1, 2, 3, 4],
             [5, 6, 7, 8],
             [9, 10, 11, 12],
             [13, 14, 15, 16]])

indexes = tf.sequence_mask([1, 2, 2, 4], 4)
y = tf.multiply(x, tf.cast(indexes, x.dtype))

-> y is [[ 1  0  0  0]
         [ 5  6  0  0]
         [ 9 10  0  0]
         [13 14 15 16]]
可能会对您有所帮助,但这取决于您的结果如何使用(我在下面使用了“j”而不是“x”):

j=np.random.randint(100,大小=(4,4)) >>>索引=tf.序列屏蔽([1,2,2,4],4) >>>tf.ragged.boolean_掩码(j,索引)
好问题,我遇到了与
tf.boolean\u mask()
完全相同的问题。我认为这是当前实现的一个限制,就像某些其他tensorflow库函数一样,
sequence\u mask()
boolean\u mask()
都可以使用一个额外的
keepdims=True
选项。@datwelk我已经用一个简单的解决方案解决了这个问题。你想要一个答案或帮助吗?我很想看看,也许你可以把它贴在下面作为你问题的答案?我在tensorflow github页面上看到了您的(已关闭)问题,我为boolean_mask创建了一个新的问题,并明确提到这是一个功能请求,以避免重定向到Stackoverflow:
>>>j = np.random.randint(100,size=(4,4))
>>>indexes =tf.sequence_mask([1,2,2,4],4)

>>>tf.ragged.boolean_mask(j, indexes)
<tf.RaggedTensor [[75], [11, 8], [20, 40], [24, 30, 75, 77]]>