python - Tensorflow 函数打印

标签 python tensorflow

我在文件 neural_network.py 中有一个定义损失函数的函数:

def loss(a, b):
    ...
    debug = tf.Print(a, [a], message = 'debug: ')
    debug.eval(session = ???)
    return tf.add(a, b)

解释一下,我想在这个函数的某处打印一个张量。但是,我没有在此函数中声明任何 session ;我的 session 在另一个名为 forecaster.py 的文件中声明。因此,当我尝试将 tf.Print() 放入 loss() 时,我做不到,因为我不知道要评估哪个 session 与。有没有办法通过使用 tf.Print() 或其他调试方法来解决这个问题?谢谢!

最佳答案

tf.Print用作恒等函数,它返回您作为第一个参数传递的相同张量,具有打印指定为第二个参数的张量列表的副作用。

所以你应该使用如下:

def loss(a, b):
    ...
    a = tf.Print(a, [a], message = 'debug: ')
    return tf.add(a, b)

a 将在每次评估张量 tf.add(a, b) 时打印。

关于python - Tensorflow 函数打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41616065/

相关文章:

python - python 的 fcntl.flock 函数是否提供文件访问的线程级锁定?

tensorflow - 内存利用率接近零时,Google Cloud ML Engine "out-of-memory"错误

python - Keras Estimator + tf.data API

python - 属性错误: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'

循环绘制图形时的 Python 内存

python - 填充 html 表单时,Html 输入值不显示完整变量

java - 如何将预训练的 tensorflow 模型加载并预测到Java代码中?

python - 在 AWS Sagemaker 中使用 Tensorflow Estimator 时如何在 S3 中保存 Tensorflow 模型(作为/output/model.tar.gz)

python - 无法从源代码 : recipe for target 'Programs/python.o' failed 安装 python 3.6.3

python - scikit-learn 可以处理多少文本?