1、获取MNIST数据
MNIST数据集只要一行代码就可以获取的到，非常方便。关于MNIST的基本信息可以参考我的上一篇随笔。

`mnist = input_data.read_data_sets('./data/mnist', one_hot=True)`

2、模型基本结构

```1 inputSize  = 784
2 outputSize = 10
3 hiddenSize = 50
4 batchSize  = 64
5 trainCycle = 50000```

3、输入层

`inputLayer = tf.placeholder(tf.float32, shape=[None, inputSize])`

4、隐藏层

random_normal() 不一样。这两者的作用都是生成指定形状、期望和标准差的符合正太分布随机变量。区别是 truncated_normal 函数对随机变量的范围有个限制（与期望的偏差在2个标准差之内，否则丢弃）。另外偏差项这里也使用了变量的形式，也可以采用常量来进行替代。

```1 hiddenWeight = tf.Variable(tf.truncated_normal([inputSize, hiddenSize], mean=0, stddev=0.1))
2 hiddenBias   = tf.Variable(tf.truncated_normal([hiddenSize]))
3 hiddenLayer  = tf.add(tf.matmul(inputLayer, hiddenWeight), hiddenBias)
4 hiddenLayer  = tf.nn.sigmoid(hiddenLayer)```

5、输出层

```1 outputWeight = tf.Variable(tf.truncated_normal([hiddenSize, outputSize], mean=0, stddev=0.1))
2 outputBias   = tf.Variable(tf.truncated_normal([outputSize], mean=0, stddev=0.1))
3 outputLayer  = tf.add(tf.matmul(hiddenLayer, outputWeight), outputBias)
4 outputLayer  = tf.nn.sigmoid(outputLayer)```

6、输出标签

`outputLabel = tf.placeholder(tf.float32, shape=[None, outputSize])`

7、损失函数

`loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=outputLabel, logits=outputLayer))`

8、优化器与目标函数

```optimizer = tf.train.AdamOptimizer()
target    = optimizer.minimize(loss)```

9、训练过程

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(trainCycle):
batch = mnist.train.next_batch(batchSize)
sess.run(target, feed_dict={inputLayer: batch[0], outputLabel: batch[1]})
if i % 1000 == 0:
corrected = tf.equal(tf.argmax(outputLabel, 1), tf.argmax(outputLayer, 1))
= tf.reduce_mean(tf.cast(corrected, tf.float32))
accuracyValue = sess.run(accuracy, feed_dict={inputLayer: batch[0], outputLabel: batch[1]})
print(i, ‘train set accuracy:’, accuracyValue)

10、测试训练结果

```     corrected = tf.equal(tf.argmax(outputLabel, 1), tf.argmax(outputLayer, 1))
accuracy  = tf.reduce_mean(tf.cast(corrected, tf.float32))
accuracyValue = sess.run(accuracy, feed_dict={inputLayer: mnist.test.images, outputLabel: mnist.test.labels})
print("accuracy on test set:", accuracyValue)```

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
inputSize = 784
outputSize = 10
hiddenSize = 50
batchSize = 64
trainCycle = 50000
# 输入层
inputLayer = tf.placeholder(tf.float32, shape=[None, inputSize])
# 隐藏层
hiddenWeight = tf.Variable(tf.truncated_normal([inputSize, hiddenSize], mean=0, stddev=0.1))
hiddenBias = tf.Variable(tf.truncated_normal([hiddenSize]))
hiddenLayer = tf.nn.sigmoid(hiddenLayer)
# 输出层
outputWeight = tf.Variable(tf.truncated_normal([hiddenSize, outputSize], mean=0, stddev=0.1))
outputBias = tf.Variable(tf.truncated_normal([outputSize], mean=0, stddev=0.1))
outputLayer = tf.nn.sigmoid(outputLayer)
# 标签
outputLabel = tf.placeholder(tf.float32, shape=[None, outputSize])
# 损失函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=outputLabel, logits=outputLayer))
# 优化器
# 训练目标
target = optimizer.minimize(loss)
# 训练
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(trainCycle):
batch = mnist.train.next_batch(batchSize)
sess.run(target, feed_dict={inputLayer: batch[0], outputLabel: batch[1]})
if i % 1000 == 0:
corrected = tf.equal(tf.argmax(outputLabel, 1), tf.argmax(outputLayer, 1))
accuracy = tf.reduce_mean(tf.cast(corrected, tf.float32))
accuracyValue = sess.run(accuracy, feed_dict={inputLayer: batch[0], outputLabel: batch[1]})
print(i, ‘train set accuracy:’, accuracyValue)
# 测试
corrected = tf.equal(tf.argmax(outputLabel, 1), tf.argmax(outputLayer, 1))
accuracy = tf.reduce_mean(tf.cast(corrected, tf.float32))
accuracyValue = sess.run(accuracy, feed_dict={inputLayer: mnist.test.images, outputLabel: mnist.test.labels})
print(“accuracy on test set:”, accuracyValue)
sess.close()