2

The equivalent numpy operation can be done using np.delete as specified here. Since there's no tf.delete, I am not sure how to do this in tensorflow.

alpaca
  • 1,211
  • 13
  • 23

2 Answers2

1

I think you might want to use tf.boolean_mask. For example,

labels = tf.Variable([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
a = tf.Variable([1, 0, 0])
a1 = tf.cast(a, dtype=tf.bool)
print(a1)    
mask = tf.math.logical_not(a1)
print(mask)
print(tf.boolean_mask(labels, mask))

The output is,

tf.Tensor([ True False False], shape=(3,), dtype=bool)
tf.Tensor([False  True  True], shape=(3,), dtype=bool)
tf.Tensor(
[[0 1 0]
 [0 0 1]], shape=(2, 3), dtype=int32)

So, you can define a mask to delete a specific vector of you tensors in first dimensionality.

wangsy
  • 159
  • 11
0

This is one way to do that:

import tensorflow as tf

def delete_tf(a, idx, axis=0):
    n = tf.shape(a)[axis]
    t = tf.ones_like(idx, dtype=tf.bool)
    m = ~tf.scatter_nd(tf.expand_dims(idx, 1), t, [n])
    return tf.boolean_mask(a, m, axis=axis)

with tf.Graph().as_default(), tf.Session() as sess:
    data = tf.reshape(tf.range(12), [3, 4])
    print(sess.run(delete_tf(data, [1], 0)))
    # [[ 0  1  2  3]
    #  [ 8  9 10 11]]
    print(sess.run(delete_tf(data, [0, 2], 1)))
    # [[ 1  3]
    #  [ 5  7]
    #  [ 9 11]]
jdehesa
  • 58,456
  • 7
  • 77
  • 121