我有一个 tf.int32
类型的张量
我想使用 tf.Print
但我需要结果是二进制的。
这可能吗?
用于调试。
例子:
constant = tf.constant(5)
#magic
tf.Print(constant) # prints 101
最佳答案
您可以使用 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'
但请注意,tf.py_function
在图中创建了一个节点。所以如果你想打印很多张量,你可以在tf.Print
之前用tf.py_function
包裹它们,但是在循环中这样做可能引起腹胀。
关于python - 以二进制打印整数张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123877/