如何从张量流中的张量中删除特定值的向量?
How to remove a vector which is specific value from tensor in tensorflow?
我想实现以下操作。
给定一个张量,
m = ([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
如何实现从m中删除值为[2, 2, 2]的向量?
你可以这样做:
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]]
我想实现以下操作。 给定一个张量,
m = ([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
如何实现从m中删除值为[2, 2, 2]的向量?
你可以这样做:
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]]