[关闭]
@linux1s1s 2017-08-29T13:23:00.000000Z 字数 2538 阅读 1610

TensorFlow 搭建mnist项目

Machine-Learning 2017-08


每当学习一种编程语言,总是从HelloWord开始,TensorFlow的HelloWord就是经典的MNIST项目。

项目准备

THE MNIST DATABASE of handwritten digits 这个官网上面给出了训练数据和测试数据集,请分别把下面的四个文件下载到本地。

  1. train-images-idx3-ubyte.gz: training set images (9912422 bytes)
  2. train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
  3. t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
  4. t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

tensorflow/mnist 下载源代码,并直接用Pycharm打开该项目

调试项目

项目本身包含自动下载训练数据和测试数据,但是可能因为网络原因有可能导致无法加载,所以为了方便起见,我们直接使用上面已经下载好的本地数据即可。

将上面下载的文件放入下图所示的目录中
此处输入图片的描述

修改如下所示python文件
此处输入图片的描述

运行结果

此处输入图片的描述

deep-learning的训练对比

我们可以选择深度学习,来对比一下训练出来的可信度。

修改mnist-deep.py文件,将载入数据部分修改为上图的本地加载。为了节省时间,我们将迭代训练次数由原先的2w次降低为2k次(实际工作中切不可这么粗鲁),学习率还是100,然后运行mnist-deep.py。

此处输入图片的描述

一段时间以后,观察一下运行结果。

此处输入图片的描述

对比softmax回归的0.91,现在的0.97效果要好很多。

附softmax源码

  1. """A very simple MNIST classifier.
  2. See extensive documentation at
  3. https://www.tensorflow.org/get_started/mnist/beginners
  4. """
  5. from __future__ import absolute_import
  6. from __future__ import division
  7. from __future__ import print_function
  8. import argparse
  9. import sys
  10. from tensorflow.examples.tutorials.mnist import input_data
  11. import tensorflow as tf
  12. FLAGS = None
  13. def main(_):
  14. # Import data
  15. # mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
  16. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
  17. # Create the model
  18. x = tf.placeholder(tf.float32, [None, 784])
  19. W = tf.Variable(tf.zeros([784, 10]))
  20. b = tf.Variable(tf.zeros([10]))
  21. y = tf.matmul(x, W) + b
  22. # Define loss and optimizer
  23. y_ = tf.placeholder(tf.float32, [None, 10])
  24. # The raw formulation of cross-entropy,
  25. #
  26. # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
  27. # reduction_indices=[1]))
  28. #
  29. # can be numerically unstable.
  30. #
  31. # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
  32. # outputs of 'y', and then average across the batch.
  33. cross_entropy = tf.reduce_mean(
  34. tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
  35. train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
  36. sess = tf.InteractiveSession()
  37. tf.global_variables_initializer().run()
  38. # Train
  39. for _ in range(1000):
  40. batch_xs, batch_ys = mnist.train.next_batch(100)
  41. sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  42. # Test trained model
  43. correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  44. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  45. print(sess.run(accuracy, feed_dict={x: mnist.test.images,
  46. y_: mnist.test.labels}))
  47. if __name__ == '__main__':
  48. parser = argparse.ArgumentParser()
  49. parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
  50. help='Directory for storing input data')
  51. FLAGS, unparsed = parser.parse_known_args()
  52. tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注