3

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?

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I am using TF version 1.4 and it doesn't support tf.bitwise.right_shift() yet. But it is good idea.
It is available from TF 1.5, and the current release is TF 1.8. Perhaps consider upgrading.
0

I tried the solution from guik, but failed when converting 1 and 4, like

a = tf.constant([1,7], dtype=tf.int32)

b = tf.mod(tf.bitwise.right_shift(tf.expand_dims(a,1), tf.range(3)), 2)

Actually, the bits are reversed here, so:

b = tf.reverse(b, axis = [-1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.