【问题标题】:Print an integer tensor in binary以二进制形式打印整数张量
【发布时间】:2018-06-15 20:56:21
【问题描述】:

我有一个 tf.int32 类型的张量 我想使用tf.Print,但我需要结果是二进制的。 这甚至可能吗? 用于调试。

例子:

constant = tf.constant(5)
#magic
tf.Print(constant) # prints 101

【问题讨论】:

  • 您最好的选择可能是重定向来自 C 流的输出,使用 python 对其进行修改,然后将其打印为二进制文件。 See this post for details.

标签: python tensorflow binary type-conversion integer


【解决方案1】:

你可以使用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包装它们,但是在循环中这样做可能会导致膨胀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2010-11-04
    • 2010-11-20
    • 2016-12-13
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多