首页 > 解决方案 > 如何从张量流中的张量中删除特定值的向量?

问题描述

我想实现以下操作。给定一个张量,

m = ([[1, 1, 1], [2, 2, 2], [3, 3, 3]])

如何实现从 m 中删除值为 [2, 2, 2] 的向量?

标签: pythontensorflowtensorflow2.0

解决方案


你可以这样做:

import tensorflow as tf

def remove_row(m, q):
    # Assumes m is 2D
    mask = tf.math.reduce_any(tf.not_equal(m, q), axis=-1)
    return tf.boolean_mask(m, mask)

# Test
m = tf.constant([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
q = tf.constant([2, 2, 2])
tf.print(remove_row(m, q))
# [[1 1 1]
#  [3 3 3]]

推荐阅读