Home > Enterprise >  How to reduce (m, 50) tensor to (m, 25) tensor based on tensor value in tensorflow 1.10
How to reduce (m, 50) tensor to (m, 25) tensor based on tensor value in tensorflow 1.10

Time:10-28

For example,

# (m, 50) tensor a
print(a)

<tf.Tensor: id=11543, shape=(3, 50), dtype=int32, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 15, 32, 1, 1, 7, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 11, 1, 1, 1, 11, 1, 1, 1, 1, 1, 8, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 421, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1,
        1, 1, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0]], dtype=int32)>

# (m, 50) tensor b
print(b)
<tf.Tensor: id=20624, shape=(3, 50), dtype=int32, numpy=
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 11, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 142, 1]], dtype=int32)

The tensor shape is (m, 50) and half of it is all 0, so I want to reduce tensor into (m, 25) which drops all 0 half part.
I am wondering how to implement it in tf 1.10.

CodePudding user response:

I'll assume that it is known that the number of zeros in each row represent exactly half the size of the column. If it doesn't, then removing the zeros will result in a ragged tensor (a tensor with different dimension sizes) which is not supported in tf v1.10.0.

You can create a mask where the elements in the tensor are greater than 0 and then mask them out using tf.boolean_mask(and also we will have to reshape the result because tensorflow 1.10 cannot infer the new shape).

import tensorflow as tf


# using the `a` defined in your question
mask = tf.cast(a > 0, dtype=tf.int32)
result = tf.boolean_mask(a, mask)
result = tf.reshape(result, (3, 25))


with tf.Session() as sess:
    print(result.eval())

# [[  1   1   1   1   1   1   1   1   1   1  15  32   1   1   7   1   1   1
#     1   1   1   1   1   1   1]
#  [  1   1   1   1   1  11   1   1   1  11   1   1   1   1   1   8   1   1
#     1   1   1   1   1   1   1]
#  [  1   1   1   1   1 421   1   1   1   1   1   1   1   1   1  12   1   1
#     1   1   1   1   1   1  42]]
  • Related