Tensorflow MNIST初学者需要一些理解评估步骤
Tensorflow MNIST beginners need some understanding evaluation step
我浏览了 Tensorflow 中评估训练模型的基本示例。内容如下:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
我没有遵循这个代码,训练好的'model'在哪里?还是 tf.reduce_mean(....) ?检查训练好的模型。
正如 "Guy Coder" 所说,也许您应该在开始使用 tensorflow 之前查看其他在线资源或 MOOC。
但无论如何,也许你会得到一个更清晰的画面......
在 tensorflow 中训练模型有两个部分。
- 首先声明模型的结构,包含不同的层和变量。 Tensorflow 会从中制作一个图表,但还没有计算发生。
- 然后你让tensorflow"run"优化模型。你在这里所做的是告诉 tensorflow 你想要减少交叉熵,或者你定义的任何损失函数,所以你提供输入数据和标签,图形需要计算它。
在这之后你想出了一个训练有素的模型。也许您会想要保存模型并在以后重新使用它,但那是另一回事了。
因此,在训练期间,或训练结束后,您可以调用
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
。
这是告诉 tensorflow 使用具有变量当前值的图来计算准确度(也许您正在训练中)。你正在为这个准确度函数提供图像和标签。 Tensorflow 将取 x 值并尝试预测 y_,准确度将取决于他做得有多好。
与训练模型的连接来自 correct_prediction
函数,该函数应将正确的输出与模型的预测进行比较,即 y_ vs y
希望对您有所帮助
编辑
我会根据您的评论回答,但请注意,您的问题解释得非常糟糕......正如 S_kar
所指出的
要保存模型,您可以这样做:
# model declared before this line
with tf.Session() as sess:
# Merge all the summaries and write them out to /tmp/tf
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/tf", sess.graph_def)
tf.initialize_all_variables().run()
saver = tf.train.Saver()
"""
train the model...
"""
print "Model succesfuly trained"
# now save the model in a subdirectory called "model"
checkpoint_path = os.getcwd() + "/model/model.ckpt"
saver.save(sess, checkpoint_path)
print "Model saved"
要恢复模型查看
我浏览了 Tensorflow 中评估训练模型的基本示例。内容如下:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
我没有遵循这个代码,训练好的'model'在哪里?还是 tf.reduce_mean(....) ?检查训练好的模型。
正如 "Guy Coder" 所说,也许您应该在开始使用 tensorflow 之前查看其他在线资源或 MOOC。
但无论如何,也许你会得到一个更清晰的画面......
在 tensorflow 中训练模型有两个部分。
- 首先声明模型的结构,包含不同的层和变量。 Tensorflow 会从中制作一个图表,但还没有计算发生。
- 然后你让tensorflow"run"优化模型。你在这里所做的是告诉 tensorflow 你想要减少交叉熵,或者你定义的任何损失函数,所以你提供输入数据和标签,图形需要计算它。
在这之后你想出了一个训练有素的模型。也许您会想要保存模型并在以后重新使用它,但那是另一回事了。
因此,在训练期间,或训练结束后,您可以调用print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
。
这是告诉 tensorflow 使用具有变量当前值的图来计算准确度(也许您正在训练中)。你正在为这个准确度函数提供图像和标签。 Tensorflow 将取 x 值并尝试预测 y_,准确度将取决于他做得有多好。
与训练模型的连接来自 correct_prediction
函数,该函数应将正确的输出与模型的预测进行比较,即 y_ vs y
希望对您有所帮助
编辑
我会根据您的评论回答,但请注意,您的问题解释得非常糟糕......正如 S_kar
所指出的要保存模型,您可以这样做:
# model declared before this line
with tf.Session() as sess:
# Merge all the summaries and write them out to /tmp/tf
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/tf", sess.graph_def)
tf.initialize_all_variables().run()
saver = tf.train.Saver()
"""
train the model...
"""
print "Model succesfuly trained"
# now save the model in a subdirectory called "model"
checkpoint_path = os.getcwd() + "/model/model.ckpt"
saver.save(sess, checkpoint_path)
print "Model saved"
要恢复模型查看