Python 基于输入值创建tf.sequence_掩码

Python 基于输入值创建tf.sequence_掩码,python,tensorflow,Python,Tensorflow,tensorflow给出的示例表明,可以创建遮罩: tf.sequence_mask([1, 3, 2], 5) # [[True, False, False, False, False], # [True, True, True, False, False], # [True, True, False, False, False]] 如果我想根据批次的值创建一

tensorflow给出的示例表明,可以创建遮罩:

tf.sequence_mask([1, 3, 2], 5)  # [[True, False, False, False, False],
                                #  [True, True, True, False, False],
                                #  [True, True, False, False, False]]
如果我想根据批次的值创建一个动态掩码,该怎么办?假设我的输入是[[1,0,2,3,4],[2,3,4,4,4],[2,3,4,5,4]],我想屏蔽前4个之前的所有内容为真,前4个之后的所有内容为假,生成的屏蔽应该是:

[[True, True, True, True, True],
[True, True, True, False, False],
[True, True, True, False, False]]
我试图将其作为权重应用于我的序列_losstensor

导入tensorflow作为tf
x=tf.常数([[1,0,2,3,4],[2,3,4,4],[2,3,4,5,4]]
​
cond=tf.cast(tf.equal(x,4),tf.int8)
idx4_u=tf.reformate(tf.argmax(cond,axis=1,output_utype=tf.int32),(-1,1))
​
如果所有行至少有一个值等于4,则为可选:

idx4=tf.where(
tf.equal(tf.reduce_max(cond,axis=1,keep_dims=True),1),
idx4_3;,
tf.常数(-1,形状=idx4_uu2;形状)
)
通过比较前4个索引与1d范围索引来创建掩码:

mask = idx4 >= tf.range(x.shape[1])
​
with tf.Session() as sess:
    print(sess.run(mask))
#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]

或者使用
序列掩码

import tensorflow as tf
x = tf.constant([[1, 0, 2, 3, 4], [2, 3, 4, 4, 4], [2, 3, 4, 5, 4]])
​
cond = tf.cast(tf.equal(x, 4), tf.int8)
idx4_ = tf.argmax(cond, axis=1, output_type=tf.int32)

idx4 = tf.where(
    tf.equal(tf.reduce_max(cond, axis=1), 1), 
    idx4_, 
    tf.constant(-1, shape=idx4_.shape)
)

with tf.Session() as sess:
    print(sess.run(tf.sequence_mask(idx4+1, x.shape[1])))

#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]

如果x是一个占位符,其形状未知:

import tensorflow as tf
​
x = tf.placeholder(tf.int32, shape=[None,None])
cond = tf.cast(tf.equal(x, 4), tf.int8)
idx4_ = tf.argmax(cond, axis=1, output_type=tf.int32)

idx4 = tf.where(
    tf.equal(tf.reduce_max(cond, axis=1), 1), 
    idx4_, 
    tf.fill(tf.shape(idx4_), -1)
)

mask = tf.sequence_mask(idx4+1, tf.shape(x)[-1])
with tf.Session() as sess:
    print(sess.run(mask, {x: [[1, 0, 2, 3, 4], [2, 3, 4, 4, 4], [2, 3, 4, 5, 4]]}))

#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]
将tensorflow导入为tf
x=tf.常数([[1,0,2,3,4],[2,3,4,4],[2,3,4,5,4]]
​
cond=tf.cast(tf.equal(x,4),tf.int8)
idx4_u=tf.reformate(tf.argmax(cond,axis=1,output_utype=tf.int32),(-1,1))
​
如果所有行至少有一个值等于4,则为可选:

idx4=tf.where(
tf.equal(tf.reduce_max(cond,axis=1,keep_dims=True),1),
idx4_3;,
tf.常数(-1,形状=idx4_uu2;形状)
)
通过比较前4个索引与1d范围索引来创建掩码:

mask = idx4 >= tf.range(x.shape[1])
​
with tf.Session() as sess:
    print(sess.run(mask))
#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]

或者使用
序列掩码

import tensorflow as tf
x = tf.constant([[1, 0, 2, 3, 4], [2, 3, 4, 4, 4], [2, 3, 4, 5, 4]])
​
cond = tf.cast(tf.equal(x, 4), tf.int8)
idx4_ = tf.argmax(cond, axis=1, output_type=tf.int32)

idx4 = tf.where(
    tf.equal(tf.reduce_max(cond, axis=1), 1), 
    idx4_, 
    tf.constant(-1, shape=idx4_.shape)
)

with tf.Session() as sess:
    print(sess.run(tf.sequence_mask(idx4+1, x.shape[1])))

#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]

如果x是一个占位符,其形状未知:

import tensorflow as tf
​
x = tf.placeholder(tf.int32, shape=[None,None])
cond = tf.cast(tf.equal(x, 4), tf.int8)
idx4_ = tf.argmax(cond, axis=1, output_type=tf.int32)

idx4 = tf.where(
    tf.equal(tf.reduce_max(cond, axis=1), 1), 
    idx4_, 
    tf.fill(tf.shape(idx4_), -1)
)

mask = tf.sequence_mask(idx4+1, tf.shape(x)[-1])
with tf.Session() as sess:
    print(sess.run(mask, {x: [[1, 0, 2, 3, 4], [2, 3, 4, 4, 4], [2, 3, 4, 5, 4]]}))

#[[ True  True  True  True  True]
# [ True  True  True False False]
# [ True  True  True False False]]

那么你的一个批次形状总是[batch_SIZE,3,5]吗?你说的前4个是什么意思?我在你的结果图中看不到这一点?批量大小是固定的,就像我给出的示例一样。在前4个(包括前4个)之前看到的所有内容都应该伪装为真实。4是批次中的元素值,因为我的批次包含编号,所以我们的一个批次形状始终是[batch_SIZE,3,5]?你说的前4个是什么意思?我在你的结果图中看不到这一点?批量大小是固定的,就像我给出的示例一样。在前4个(包括前4个)之前看到的所有内容都应该伪装为真实。4是批次中的元素值,因为我的批次包含数字如果x不是常数怎么办?我正在运行批处理,我想根据批处理的值更改掩码。另外,我可以使用序列掩码实现它吗?如果
x
是一个变量,它应该仍然有效,当
x
是一个占位符时,用
sequence\u mask
方法更新了一个解决方案。如果x不是常数怎么办?我正在运行批处理,我想根据批处理的值更改掩码。另外,我可以使用sequence_mask实现它吗?如果
x
是一个变量,它应该仍然有效,当
x
是一个带有
sequence_mask
方法的占位符时,它更新了一个解决方案。