Python 我怎样才能掩盖一批张量?
我想对一批张量做一个负掩蔽 e、 g.目标张量:Python 我怎样才能掩盖一批张量?,python,tensorflow,Python,Tensorflow,我想对一批张量做一个负掩蔽 e、 g.目标张量: [[1,2,3], [4,5,6], [7,8,9]] 掩模张量: [[1,1,0], [0,1,1], [1,1,0]] 预期结果: [[1,2,0], [0,5,6], [7,8,0]] 我该怎么做? 必须生成每个3x3矩阵吗 您可以执行以下操作 import tensorflow as tf tf_a = tf.constant([[1,2,3],[4,5,6],[7,8,9]], dtype=tf.float32)
[[1,2,3],
[4,5,6],
[7,8,9]]
掩模张量:
[[1,1,0],
[0,1,1],
[1,1,0]]
预期结果:
[[1,2,0],
[0,5,6],
[7,8,0]]
我该怎么做?
必须生成每个3x3矩阵吗 您可以执行以下操作
import tensorflow as tf
tf_a = tf.constant([[1,2,3],[4,5,6],[7,8,9]], dtype=tf.float32)
mask = tf.cast(tf.constant([[1,1,0] , [0,1,1], [1,1,0]]) , tf.bool)
a_masked = tf_a * tf.cast(mask, tf.float32)
with tf.Session() as sess:
#print(sess.run(tf.math.logical_not(mask)))
print(sess.run(a_masked))
另一种方法是使用: 如果在“急切”模式下打印结果:
<tf.Tensor: id=77, shape=(3, 3), dtype=float32, numpy=
array([[1., 2., 0.],
[0., 5., 6.],
[7., 8., 0.]], dtype=float32)>
如果将矩阵1和2相乘,将得到预期结果。
<tf.Tensor: id=77, shape=(3, 3), dtype=float32, numpy=
array([[1., 2., 0.],
[0., 5., 6.],
[7., 8., 0.]], dtype=float32)>