tf.print() vs Python 打印 vs tensor.eval() [英] tf.print() vs Python print vs tensor.eval()

查看:52
本文介绍了tf.print() vs Python 打印 vs tensor.eval()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

貌似在Tensorflow中,至少有三种方法可以打印出一个张量的值.我一直在阅读这里那里,但仍然很困惑.

It seems that in Tensorflow, there are at least three methods to print out the value of a tensor. I've been reading here and there, yet still very confused.

这些作者似乎将不同的用法总结为:

These authors seem to summarize the different usage as:

  • Python print:只能打印出张量的某些属性,例如它的形状,因为在计算图之外它只是一个操作.
  • tensor.eval() :不确定它与 tf.print()
  • 的区别
  • tf.print():可以输出张量的实际值,但必须插入图中的某处以及被其他操作使用,否则会悬空打印出来.
  • Python print: can only print out certain attributes of a tensor, e.g. its shape, because outside the computation graph it's merely an operation.
  • tensor.eval() : Not sure its difference with tf.print()
  • tf.print(): can output the actual value of a tensor, but must be inserted somewhere in the graph as well as being used by some other operation otherwise it will be dangling and still not printed.

我的困惑可能还在于我不确定我们可以在 tensorflow 计算图中访问多少 Python 功能,或者计算图结束"和 Python 代码开始"的位置.例如

My confusion probably also lies in I'm not sure how much Python functionalities we can access in a tensorflow computation graph, or where the computation graph "ends" and Python code "begins". e.g.

  1. 如果我在构建计算图的两行之间插入 Python 打印,当我稍后调用 sess.run() 时,会调用该行吗?
  2. 如果我想在一个计算图中打印出多个张量值,我应该把这些语句放在哪里?
  3. tensor.eval()tf.print() 有什么区别?我应该如何以不同的方式使用它们?
  1. If I insert a Python print between two lines where I construct a computation graph, when I call sess.run() later, will this line be called?
  2. If I want to print out multiple tensor values in a computation graph, where should I put these statements?
  3. What's the difference between tensor.eval() and tf.print()? How should I use them differently?

推荐答案

原生 Python print() 语句将在第一次构建图时调用.看看这个:

The native Python print() statement will be called when the graph is build the first time. Check this out:

a = tf.placeholder(shape=None, dtype=tf.int32)
b = tf.placeholder(shape=None, dtype=tf.int32)
print("a is ",a," while b is ",b)
c = tf.add(a, b)
with tf.Session() as sess:
    print(sess.run(c, feed_dict={a: 1, b: 2}))
    print(sess.run(c, feed_dict={a: 3, b: 1}))

通过执行此代码块,输出为:

By exectuing this code block, the output is:

# a is  Tensor("Placeholder:0", dtype=int32)  while b is  Tensor("Placeholder_1:0", dtype=int32)
# 3
# 4

另一方面,让我们看看tf.print():

On the other hand, let us see tf.print():

a = tf.placeholder(shape=None, dtype=tf.int32)
b = tf.placeholder(shape=None, dtype=tf.int32)
print_op = tf.print("a is ",a," while b is ",b)
with tf.control_dependencies([print_op]):
    c = tf.add(a, b)

with tf.Session() as sess:
    print(sess.run(c, feed_dict={a: 1, b: 2}))
    print(sess.run(c, feed_dict={a: 3, b: 1}))

所以,根据下面的输出,我们可以看到,如果我们添加了 tf.print 操作必须在 c 运行时运行的依赖项,我们得到查看我们想要的输出:

So, according to the output below, we can see that if we add the dependency that the tf.print op must be run whenever c is run, we get to see the output we want to:

# a is  1  while b is  2
# 3
# a is  3  while b is  1
# 4

最后,tensor.eval()sess.run(tensor) 相同.但是,tensor.eval() 的局限性在于您可以运行它来评估单个张量,而 tf.Session 可用于评估多个张量 sess.run([tensor1, tensor2]).如果你问我,我会一直使用 sess.run(list_of_tensors) 来评估任意数量的张量,并打印出它们的值.

Finally, tensor.eval() is identical to sess.run(tensor). However, the limitation of tensor.eval() is that you can run it to evaluate a single tensor, while the tf.Session can be used to evaluate multiple tensors sess.run([tensor1, tensor2]). If you ask me, I would always use sess.run(list_of_tensors), to evaluate as many tensors as I want to, and print out their values.

这篇关于tf.print() vs Python 打印 vs tensor.eval()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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