tensorflow教程中测试数据的计算
computation of test data in tensorflow tutorial
我正在看tensorflow的教程-
https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10])) #weights
b = 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 图与训练和测试相同。唯一的区别是您提供给占位符 x
和 y_
.
的数据
y
在这里计算:
y = tf.nn.softmax(tf.matmul(x, W) + b) # Line 7
具体来说,您要查找的内容在该行中:
tf.matmul(x, W) + b
其输出通过softmax函数来识别class。
这是在图形的 1000 次遍历中计算的,每次变量 W
和 b
由 GradientDescent 更新并且计算 y
并与 y_
确定损失。
我正在看tensorflow的教程- https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10])) #weights
b = 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 图与训练和测试相同。唯一的区别是您提供给占位符 x
和 y_
.
y
在这里计算:
y = tf.nn.softmax(tf.matmul(x, W) + b) # Line 7
具体来说,您要查找的内容在该行中:
tf.matmul(x, W) + b
其输出通过softmax函数来识别class。
这是在图形的 1000 次遍历中计算的,每次变量 W
和 b
由 GradientDescent 更新并且计算 y
并与 y_
确定损失。