我想在张量流中的张量3D上放置一个二进制掩码向量。
例如,shape=(None, 3,4,1)
[[1,2,3,4][5,6,7,8][9,10,11,12]]
我想放置一个像
这样的掩蔽向量[[1,1,1,1] [1,1,1,1] [1,1,0,0]]
=>我的理想结果是
[[1,2,3,4][5,6,7,8][9,10,0,0]]
我尝试如下。
output = tf.layers.conv2d(output, 1, [5, 5], strides=(9, 6), padding='valid')
output = tf.tanh(output)
aa = [1.0] * 10 + [0.0] *2
aa = aa * batch_size
bb = tf.constant(aa , shape =(batch_size , 3,4,1))
output = output * bb
我想在输出后面放置一个掩码向量。
output = tf.layers.conv2d(output, 1, [5, 5], strides=(9, 6), padding='valid')
output = tf.tanh(output)
## output shape is (batch size , 3,4,1)
## and then mask vector [[1,1,1,1] [1,1,1,1] [1,1,0,0]] 3x4
## (batch size , mask vector , 1)
## (batch size, 3, 4, 1)
# I want to put a mask vector behind the output.
# Include any research you've conducted
我喜欢这个
masking = tf.sequence_mask([27]*19 + [17] , maxlen=27, dtype=tf.float32)
masking2 = tf.expand_dims(masking,axis = 0)
masking2 = tf.expand_dims(masking2,axis = -1)
mask = tf.tile(masking2, [batch_size, 1 , 1,1])
G = G * mask
答案 0 :(得分:1)
tf.sequence_mask应该可以满足您的需求。
在您的特定示例中,该名称为tf.sequence_mask([4, 4, 2], maxlen=4, dtype=tf.float32)
。