gpt4 book ai didi

python - TensorFlow 的 Print 不打印

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:42 31 4
gpt4 key购买 nike

我正在尝试理解强化学习算法中的一些代码。为此,我尝试打印张量的值。

我做了一段简单的代码来说明我的意思。

import tensorflow as tf
from keras import backend as K

x = K.abs(-2.0)
tf.Print(x,[x], 'x')

目标是打印值“2”(-2 的绝对值)。但我只得到以下信息:

Using TensorFlow backend.

Process finished with exit code 0

没有,我怎样才能像 print('...') 语句那样打印值 '2'?

最佳答案

如果您使用的是 Jupyter Notebook,那么 tf.Print() 目前是不兼容的,并且会将输出打印到 Notebook 的服务器输出,如 docs 中所述。

在 tensorflow 文档中,张量是这样描述的:

When writing a TensorFlow program, the main object you manipulate and pass around is the tf.Tensor. A tf.Tensor object represents a partially defined computation that will eventually produce a value.

因此,您必须使用 tf.Session() 初始化它们才能获取它们的值。要打印该值,您 eval()

这是你想要的代码:

import tensorflow as tf
from keras import backend as K

x= K.abs(-2.0)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print(x.eval())

初始化器对于实际初始化 x 很重要。

关于python - TensorFlow 的 Print 不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52734928/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com