gpt4 book ai didi

python - 不减少尺寸的 Tensorflow 序列掩码

转载 作者:行者123 更新时间:2023-12-03 21:24:14 25 4
gpt4 key购买 nike

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

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])

现在,我不想要这个,因为原始张量的空间信息丢失了,我想保持形状不变。由于我使用 RNN 数据,因此如何在 tensorflow 中做到这一点,所以我的张量大小是 = [batch_size, max_time, feature_length]我会把它切成这样:
indexes = tf.sequence_mask([x_1, x_2, x_3, ..., x_batch_size], max_time)
但仍想保持形状完整。如果不可能,有没有办法在这样大小的多个张量上对掩码进行排序,同时将它们连接起来,以便只保留提取的序列而不是掩码的填充?可以在串联的末尾应用填充。

最佳答案

这可能有效-

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]]

关于python - 不减少尺寸的 Tensorflow 序列掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com