Jor*_*Dik 5 python keras keras-rl
我试图从强化学习算法中理解一些代码。为了做到这一点,我试图打印张量的值。
我做了一段简单的代码来说明我的意思。
import tensorflow as tf
from keras import backend as K
x = K.abs(-2.0)
tf.Print(x,[x], 'x')
Run Code Online (Sandbox Code Playgroud)
目标是打印值“2”(-2 的绝对值)。但我只得到以下信息:
Using TensorFlow backend.
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
什么都没有,我怎么能像 print('...') 语句那样打印值 '2' 呢?
如果您使用的是 Jupyter Notebook,那么tf.Print()到目前为止不兼容,并且会按照文档中的描述将输出打印到 Notebook 的服务器输出
在 tensorflow 文档中,以下是对 Tensor 的描述:
在编写 TensorFlow 程序时,您操作和传递的主要对象是 tf.Tensor。一个 tf.Tensor 对象代表一个部分定义的计算,最终会产生一个值。
因此,您必须用 a 初始化它们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())
Run Code Online (Sandbox Code Playgroud)
初始化器对于实际初始化 x 很重要。