tensorflow,如何在mnist中为ml初学者打印tf

tensorflow,如何在mnist中为ml初学者打印tf,tensorflow,mnist,Tensorflow,Mnist,我正在尝试tensorflow的入门示例。我想打印一些关于交叉熵的东西,但什么也没有。 这里是代码,也可以参考 我不知道为什么tf.Print,它必然会交叉熵,在每个循环中不打印任何内容 我想我已经绑定了tf.Print->cross\u entropy->train\u step并运行这个train\u step。我的问题是什么?你说得对,tf.Print是(引用文档): 在评估时,具有打印数据副作用的标识op 因此,正确地说,每次有东西流过交叉熵节点时,您都希望看到交叉熵的值 问题是你在最小

我正在尝试tensorflow的入门示例。我想打印一些关于交叉熵的东西,但什么也没有。 这里是代码,也可以参考

我不知道为什么
tf.Print
,它必然会交叉熵,在每个循环中不打印任何内容


我想我已经绑定了tf.Print->cross\u entropy->train\u step并运行这个train\u step。我的问题是什么?

你说得对,
tf.Print
是(引用文档):

在评估时,具有打印数据副作用的标识op

因此,正确地说,每次有东西流过
交叉熵
节点时,您都希望看到
交叉熵
的值

问题是你在最小化真正的交叉熵,而不是身份节点。因此,在实践中,
交叉熵
变量是“指向”另一个有效评估的变量的标识节点

要解决此问题,可以强制图中节点的求值顺序

您可以约束仅在记录值后执行的最小化步骤。为此,您可以通过以下方式使用
tf.control\u dependencies

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_))

with tf.control_dependencies([tf.Print(cross_entropy, [cross_entropy], "###")]):
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(
    sess.run(accuracy, feed_dict={x: mnist.test.images,
                                  y_: mnist.test.labels}))

因此,原因是tensorflow计算过程由于变量绑定错误而没有正确执行?我不能完全确定计算是否没有正确执行。我建议您在GitHub上打开一个链接此线程的问题:也许TF开发人员可以理解并向我们解释为什么没有
TF.control\u dependencies
TF.Print无法工作。我的猜测是,
cross\u entropy
变成了一个引用节点,而不是一个真正的节点,因此没有任何东西流入引用,但这只是我对tensorflow如何工作的理解。打开这个问题,然后链接到这里,我也很感兴趣(可能是个bug!)。
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_))

with tf.control_dependencies([tf.Print(cross_entropy, [cross_entropy], "###")]):
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(
    sess.run(accuracy, feed_dict={x: mnist.test.images,
                                  y_: mnist.test.labels}))