wangsy
wangsy

Reputation: 226

How to remove a vector which is specific value from tensor in tensorflow?

I want to implement the following operation. Given a tensor,

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

How to implement to remove the vector with value [2, 2, 2] from m?

Upvotes: 2

Views: 292

Answers (1)

javidcf
javidcf

Reputation: 59711

You can do that like this:

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

Upvotes: 1

Related Questions