如何删除3D张量的不同行

时间:2019-02-25 00:16:47

标签: python tensorflow delete-row tensor

例如,有一个这样的3D张量:

a = tf.constant([[[1,2,3],
                  [4,5,6],
                  [7,8,9]],
                 [[9,8,7],
                  [6,5,4],
                  [3,2,1]],
                 [[0,8,0],
                  [1,5,4],
                  [3,1,1]]])

我想从索引为以下三个元素中删除不同的行:

idx = [[1], 
       [0], 
       [2]]

结果将是这样的:

re = [[[1,2,3],
       [7,8,9]],
      [[6,5,4],
       [3,2,1]],
      [[0,8,0],
       [1,5,4]]]

如何做到?

2 个答案:

答案 0 :(得分:0)

您可以使用numpy(假设aidxnumpy.ndarray

import numpy as np

columns_to_delete = idx.flatten()
mask = np.ones_like(a, dtype=np.bool)
mask[np.arange(a.shape[0]), columns_to_delete, :] = False
re = a[mask].reshape(a.shape[0], a.shape[1] - 1, a.shape[2])

然后使用retf.convert_to_tensor转换为张量

答案 1 :(得分:0)

第一种方法:使用tf.one_hottf.boolean_mask

# shape = (?,1,3)
mask_idx = 1- tf.one_hot(idx,a.shape[1])
# shape = (?,3)
result = tf.boolean_mask(a,mask_idx[:,0,:])
# shape = (?,2,3)
result = tf.reshape(result,shape=(-1,a.shape[1]-1,a.shape[2]))

第二种方法:使用tf.map_fn

result = tf.map_fn(lambda x: tf.boolean_mask(x[0],1 - tf.one_hot(tf.squeeze(x[1]),a.shape[1]))
                   , [a,idx]
                   , dtype=tf.int32)

一个例子:

import tensorflow as tf

a = tf.constant([[[1,2,3],[4,5,6],[7,8,9]],
                    [[9,8,7],[6,5,4],[3,2,1]],
                    [[0,8,0],[1,5,4],[3,1,1]]],dtype=tf.int32)
idx = tf.constant([[1],[0],[2]],dtype=tf.int32)

# First approach:
# shape = (?,1,3)
mask_idx = 1- tf.one_hot(idx,a.shape[1])
# shape = (?,3)
result = tf.boolean_mask(a,mask_idx[:,0,:])
# shape = (?,2,3)
result = tf.reshape(result,shape=(-1,a.shape[1]-1,a.shape[2]))

# Second approach:
result = tf.map_fn(lambda x: tf.boolean_mask(x[0],1 - tf.one_hot(tf.squeeze(x[1]),a.shape[1]))
                   , [a,idx]
                   , dtype=tf.int32)

with tf.Session() as sess:
    print(sess.run(result))

# print
[[[1 2 3]
  [7 8 9]]

 [[6 5 4]
  [3 2 1]]

 [[0 8 0]
  [1 5 4]]]