Python 移除或屏蔽I';倾斜张量的第th子元素?

Python 移除或屏蔽I';倾斜张量的第th子元素?,python,tensorflow,tiling,Python,Tensorflow,Tiling,我正在寻找一种使用Tensorflow提取所有子元素的方法,除了对应于张量索引的子元素 (例如,如果查看索引1,则仅存在子元素0和2) 非常类似于使用Numpy 下面是一些创建平铺张量和布尔掩码的示例代码: import tensorflow as tf import numpy as np _coordinates = np.array([ [1.0, 7.0, 0.0], [2.0, 7.0, 0.0], [3.0, 7.0, 0.0], ]) verts_coo

我正在寻找一种使用Tensorflow提取所有子元素的方法,除了对应于张量索引的子元素

(例如,如果查看索引1,则仅存在子元素0和2)

非常类似于使用Numpy

下面是一些创建平铺张量和布尔掩码的示例代码:

import tensorflow as tf
import numpy as np

_coordinates = np.array([
    [1.0, 7.0, 0.0],
    [2.0, 7.0, 0.0],
    [3.0, 7.0, 0.0],
])

verts_coord = _coordinates
n = verts_coord.shape[0]

mat_loc = tf.Variable(verts_coord)

tile = tf.tile(mat_loc, [n, 1])
tile = tf.reshape(tile, [n, n, n])

mask = tf.constant(~np.eye(n, dtype=bool))

result = tf.somefunc(tile, mask) #somehow extract only the elements where mask == true

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    print(sess.run(tile))
    print(sess.run(mask))
输出张量示例:

>>> print(tile)
[[[ 1.  7.  0.]
  [ 2.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 2.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 2.  7.  0.]
  [ 3.  7.  0.]]]

>>> print(mask)
[[False  True  True]
 [ True False  True]
 [ True  True False]]
期望输出:

>>> print(result)
[[[ 2.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 2.  7.  0.]]]
我也很好奇,是否有更有效的方法来做这件事,而不是创建一个大张量,然后掩盖它


谢谢

原来Tensorflow正是我想要的内置软件:)

result = tf.boolean_mask(tile, mask)
result = tf.reshape(result,  [n, n-1, -1])

>>> print(result)
[[[ 2.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 3.  7.  0.]]

 [[ 1.  7.  0.]
  [ 2.  7.  0.]]]