Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能掩盖一批张量?_Python_Tensorflow - Fatal编程技术网

Python 我怎样才能掩盖一批张量?

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)

我想对一批张量做一个负掩蔽

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