# MNIST手写识别

Demo侠可能是我等小白进阶的必经之路了，如今在AI领域，我也是个研究Demo的小白。用了两三天装好环境，跑通Demo，自学Python语法，进而研究这个Demo。当然过程中查了很多资料，充分发挥了小白的主观能动性，总算有一些收获需要总结下。

``` 1 import os
2 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
3
4 #获得数据集
5 from tensorflow.examples.tutorials.mnist import input_data
7
8 import tensorflow as tf
9
10 #输入图像数据占位符
11 x = tf.placeholder(tf.float32, [None, 784])
12
13 #权值和偏差
14 W = tf.Variable(tf.zeros([784, 10]))
15 b = tf.Variable(tf.zeros([10]))
16
17 #使用softmax模型
18 y = tf.nn.softmax(tf.matmul(x, W) + b)
19
20 #代价函数占位符
21 y_ = tf.placeholder(tf.float32, [None, 10])
22
23 #交叉熵评估代价
24 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
25
26 #使用梯度下降算法优化：学习速率为0.5
28
29 #Session(交互方式)
30 sess = tf.InteractiveSession()
31
32 #初始化变量
33 tf.global_variables_initializer().run()
34
35 #训练模型，训练1000次
36 for _ in range(1000):
37   batch_xs, batch_ys = mnist.train.next_batch(100)
38   sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
39
40 #计算正确率
41 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
42
43 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
44 print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
```

“TF_CPP_MIN_LOG_LEVEL”]=
‘2’
# 只显示 warning 和 Error，等于1是显示所有信息。不加这两行会有个提示（Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2，具体可以看 这里

``` 1 from __future__ import absolute_import
2 from __future__ import division
3 from __future__ import print_function
4
5 import gzip
6 import os
7 import tempfile
8
9 import numpy
10 from six.moves import urllib
11 from six.moves import xrange  # pylint: disable=redefined-builtin
12 import tensorflow as tf
```

``` 1 def read_data_sets(train_dir,
2                    fake_data=False,
3                    one_hot=False,
4                    dtype=dtypes.float32,
5                    reshape=True,
6                    validation_size=5000,
7                    seed=None,
8                    source_url=DEFAULT_SOURCE_URL):
9   if fake_data:
10   ...
11
12   if not source_url:  # empty string check
13   ...
14
16                                    source_url + TRAIN_IMAGES)
17   with gfile.Open(local_file, 'rb') as f:
18     train_images = extract_images(f)
19
20   ...
21
22   if not 0 <= validation_size <= len(train_images):
23     raise ValueError('Validation size should be between 0 and {}. Received: {}.'
24                      .format(len(train_images), validation_size))
25
26   validation_images = train_images[:validation_size]
27   validation_labels = train_labels[:validation_size]
28   train_images = train_images[validation_size:]
29   train_labels = train_labels[validation_size:]
30
31   options = dict(dtype=dtype, reshape=reshape, seed=seed)
32
33   train = DataSet(train_images, train_labels, **options)
34   validation = DataSet(validation_images, validation_labels, **options)
35   test = DataSet(test_images, test_labels, **options)
36
37   return base.Datasets(train=train, validation=validation, test=test)
```

mnist最终得到的是base.Datasets，完成了数据读取。这里边的细节还需要完了再仔细研究下。

``` 1 def Mycollect(My , thing):
2
3     try:
4         count = My[thing]
5     except KeyError:
6         count = 0
7
8     return count
9
10 from TestFunction import Mycollect
11 My = {'a':10, 'b':15, 'c':5}
12 thing = 'a'
13 print(Mycollect(My , thing));
```

21行是什么，看完就知道是实际的输出，然后在24行做交叉熵。终于又碰到熵这个老朋友了。交叉熵简单理解为概率分布的距离，在这里作为一个loss_function。第27行使用了梯度下降来优化这个loss_function，最终是想找到最优时候的一个模型，这里的最优指的是通过这个模型，得到的结果和实际值最接近。