Search code examples
tensorflowbinaryint

Tensorflow: How to convert an integer tensor to the corresponding binary tensor


I would like to convert an integer tensor, a_dec=tf.constant([2,7], dtype=tf.int32) to a binary rank-2 tensor such as: a_bin=tf.constant([[0,1,0],[1,1,1]], dtype=tf.int32).

Is there any efficient way to do it?


Solution

  • If you know the size n of your vector, what about:

    a_bin = tf.mod(tf.bitwise.right_shift(tf.expand_dims(a_dec,1), tf.range(n)), 2)