tf.print没有结果

时间:2018-12-29 03:01:52

标签: python tensorflow

我想使用tf.print来显示张量值,但是没有结果吗?

这是我的代码,这有什么问题吗

from __future__ import print_function
import tensorflow as tf

sess = tf.InteractiveSession()  
a = tf.constant([1.0, 3.0])
tf.print(a)

2 个答案:

答案 0 :(得分:1)

documentation of tf.Print中-已过时,建议使用tf.print

请注意,tf.print返回一个无输出运算符,该运算符直接打印输出。 在defuns或eager模式之外,除非在session.run中直接指定该运算符或将其用作其他运算符的控件依赖项,否则它将不会执行。é

这仅是图形模式下的问题。以下是如何确保tf.print在图形模式下执行的示例:

sess = tf.Session()
with sess.as_default():
    tensor = tf.range(10)
    print_op = tf.print(tensor)
    with tf.control_dependencies([print_op]):
      out = tf.add(tensor, tensor)
    sess.run(out)

因此,如果启用急切模式,则代码将按预期工作,如果要继续使用静态图形模式,则必须使用sess.run

答案 1 :(得分:0)

transform()

是我要做的。导入tensorflow,设置变量,为其设置并运行初始化程序,然后打印评估该常数的会话

相关问题