Remove zero vectors from matrix in TensorFlow

As in the question, I am trying to remove all vectors of zeros (ie [0, 0, 0, 0]) from the tensor.

Considering:

array([[ 0.        ,  0.        ,  0.        ,  0.        ],
   [ 0.19999981,  0.5       ,  0.        ,  0.        ],
   [ 0.4000001 ,  0.29999995,  0.10000002,  0.        ],
   ..., 
   [-0.5999999 ,  0.        , -0.0999999 , -0.20000005],
   [-0.29999971, -0.4000001 , -0.30000019, -0.5       ],
   [ 0.        ,  0.        ,  0.        ,  0.        ]], dtype=float32)

I tried the following code (inspired by this ):

x = tf.placeholder(tf.float32, shape=(10000, 4))

zeros_vector = tf.zeros(shape=(1, 4), dtype=tf.float32)
bool_mask = tf.not_equal(x, zero_vector)

omit_zeros = tf.boolean_mask(x, bool_mask)

But it bool_maskalso has the form (10000, 4), as if he were comparing each element of the tensor xwith zero, not a row.

I was thinking about using tf.reduce_sumwhere the whole string is zero, but this also omits the type strings [1, -1, 0, 0], and I don't want this.

Ideas?

0
source share
1 answer

, , , [1, -1, 0, 0], . - :

intermediate_tensor = reduce_sum(tf.abs(x), 1)
zero_vector = tf.zeros(shape=(1,1), dtype=tf.float32)
bool_mask = tf.not_equal(intermediate_tensor, zero_vector)
omit_zeros = tf.boolean_mask(x, bool_mask)
+2

Source: https://habr.com/ru/post/1686183/


All Articles