TensorFlow教程中测试数据的计算

我在学习TensorFlow的教程-https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))    #weightsb = tf.Variable(tf.zeros([10]))         #bias   y = tf.nn.softmax(tf.matmul(x, W) + b)y_ = tf.placeholder(tf.float32, [None, 10])cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)for i 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}))

在代码的最后,我们将测试数据传递给占位符。y_ 是包含真实值的矩阵,而 y 是包含预测值的矩阵。我的问题是,对于测试数据,y 是在什么时候计算的。W 矩阵已经通过反向传播进行了训练。但这个训练后的矩阵必须与新的输入 x(测试数据)相乘才能得到预测值 y。这是在哪里发生的?

通常我看到的代码是顺序执行的,而在最后几行中,y 并没有被显式调用。


回答:

accuracy 依赖于 correct_prediction,而 correct_prediction 依赖于 y

所以当你调用 sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 时,y 会在 accuracy 计算之前被计算。所有这些都在TensorFlow图中发生。


TensorFlow图对于训练和测试是相同的。唯一的区别是你 feeding 给占位符 xy_ 的数据。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注