tensorflow,如何在 mnist 中为 ml 初学者做 tf.Print

tensorflow, how to do tf.Print in mnist for ml beginners

我正在尝试在此 page 上使用 tensorflow 的入门示例。我想打印关于 cross_entropy 的某事,但什么也没得到。 这是代码,也可以参考here

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_))
cross_entropy = 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}))

我无法找出为什么 tf.Print,它绑定到 cross_entropy,在每个循环中什么都不打印。

我想我已经绑定了 tf.Print -> cross_entropy -> train_step 和 运行 这个 train_step。我的问题是什么?

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

an identity op with the side effect of printing data when evaluating.

因此,正确的是,每当有东西流过 cross_entropy 节点时,您都​​希望看到 cross_entropy 的值。

问题是您正在最小化真实的交叉熵,而不是身份节点。因此,在实践中,cross_entropy 变量是一个身份节点,"points" 到另一个变量,它被有效地评估。

要解决此问题,您可以强制计算图中节点的顺序。

您可以限制仅在记录值后执行的最小化步骤。为此,您可以这样使用 tf.control_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}))