简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM