0

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

这是我的代码,是否有问题:

from __future__ import print_function
import tensorflow as tf

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

2 回答 2

1

从 - 的文档中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)

因此,如果您启用了 Eager 模式,您的代码将按预期工作,如果您想继续使用您必须使用的静态图模式sess.run

于 2018-12-29T15:34:52.157 回答
0
import tensorflow as tf

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

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(a))

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

于 2018-12-29T03:19:38.633 回答