以二进制形式打印整数张量 [英] Print an integer tensor in binary

查看:114
本文介绍了以二进制形式打印整数张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

示例:

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


推荐答案

您可以使用 tf.py_func

You can use tf.py_func:

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

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

但请注意 tf.py_func 在图表中创建一个节点。因此,如果您想要打印许多张量,可以 c>用 tf.py_func 包裹它们<%c $ c> tf.Print ,但在循环中执行此操作可能会导致膨胀。

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

这篇关于以二进制形式打印整数张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆