4

I have a tensor of type tf.int32 I would like to use tf.Print but I need the result to be in binary. Is this even possible? It is for debugging.

Example:

constant = tf.constant(5)
#magic
tf.Print(constant) # prints 101
Maxim
  • 52,561
  • 27
  • 155
  • 209
dtracers
  • 1,534
  • 3
  • 17
  • 37
  • Your best bet may be to redirect the output from the C stream, modify it using python, and the print it as binary. [See this post for details.](https://stackoverflow.com/questions/35304920/how-to-redirect-c-level-streams-in-python-in-windows) – James Jan 06 '18 at 04:28

1 Answers1

3

You can use tf.py_function:

x = tf.placeholder(tf.int32)
bin_op = tf.py_function(lambda dec: bin(int(dec))[2:], [x], tf.string)

bin_op.eval(feed_dict={x: 5})   # '101'

But note that tf.py_function creates a node in the graph. So if you want to print many tensors, you can wrap them with tf.py_function before tf.Print, but doing this in a loop may cause bloating.

NucS
  • 619
  • 8
  • 21
Maxim
  • 52,561
  • 27
  • 155
  • 209