# TensorFlow MNIST手写数字识别（神经网络极简版）

MNIST手写数字识别数据集是NIST数据集的一个子集（介绍），常用于深度学习的入门样例。

## （1） 加载数据

Tensorflow提供了一个类来处理MNIST数据，这个类会自动下载并转化MNIST数据的格式，将原始数据解析成可直接进行训练和测试的数据格式。代码如下：

mnist = input_data.read_data_sets("datasets/MNIST_data/", one_hot=True)
print("Training data size: ", mnist.train.num_examples)        #55000
print("Validating data size: ", mnist.validation.num_examples) #5000
print("Testing data size: ", mnist.test.num_examples)          #10000


## （2）设置参数

learning_rate = 0.0001  # 学习率
num_epochs = 1000       # 迭代次数
BATCH_SIZE = 100        #每轮迭代的训练数据个数


## （3）前向传播

(m,n_x) = mnist.train.images.shape #784
n_y = mnist.train.labels.shape[1] #10
n_1 = 500

X = tf.placeholder(tf.float32, shape=(None,n_x), name="X")  #(55000,784)
Y = tf.placeholder(tf.float32, shape=(None,n_y), name="Y")  #(55000,10)

W1 = tf.get_variable("w1",[n_x,n_1],initializer = tf.contrib.layers.xavier_initializer(seed = 1))   #(784,500)
b1 = tf.get_variable("b1",[1,n_1],  initializer = tf.zeros_initializer())                          #(1,500)
W2 = tf.get_variable("w2",[n_1,n_y],initializer = tf.contrib.layers.xavier_initializer(seed = 1))  #(500,10)
b2 = tf.get_variable("b2",[1,n_y],  initializer = tf.zeros_initializer())                          #(1,10)

Z1 = tf.nn.relu(tf.matmul(X,W1) + b1) #(55000,500)
Z2 = tf.matmul(Z1,W2) + b2            #(55000,10)


## （4）定义损失函数

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = Z2, labels = Y))


## （5） 优化器

optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)


## （6）模型训练

with tf.Session() as sess:
tf.initialize_all_variables().run()
for i in range(num_epochs):
x,y = mnist.train.next_batch(BATCH_SIZE)
sess.run(optimizer,feed_dict={X:x,Y:y})

if i%500 == 0:
cost_v = sess.run(cost,feed_dict={X:x,Y:y})
costs.append(cost_v)
print(i,cost_v)

# Calculate the correct accuracy
correct_prediction = tf.equal(tf.argmax(Z2,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("Train Accuracy:", accuracy.eval({X:mnist.train.images, Y: mnist.train.labels})) #Train Accuracy: 0.98807275
print ("Test Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))   #Test Accuracy: 0.9756


## （7）模型评估

plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data

learning_rate = 0.0001
num_epochs = 10000
BATCH_SIZE = 100

(m,n_x) = mnist.train.images.shape #784
n_y = mnist.train.labels.shape[1] #10
n_1 = 500
costs = []

tf.set_random_seed(1)        # to keep consistent results

ops.reset_default_graph()    # to be able to rerun the model without overwriting tf variables
X = tf.placeholder(tf.float32, shape=(None,n_x), name="X")  #(55000,784)
Y = tf.placeholder(tf.float32, shape=(None,n_y), name="Y")  #(55000,10)

W1 = tf.get_variable("w1",[n_x,n_1],initializer = tf.contrib.layers.xavier_initializer(seed = 1))   #(784,500)
b1 = tf.get_variable("b1",[1,n_1],  initializer = tf.zeros_initializer())                          #(1,500)
W2 = tf.get_variable("w2",[n_1,n_y],initializer = tf.contrib.layers.xavier_initializer(seed = 1))  #(500,10)
b2 = tf.get_variable("b2",[1,n_y],  initializer = tf.zeros_initializer())                          #(1,10)

Z1 = tf.nn.relu(tf.matmul(X,W1) + b1) #(55000,500)
Z2 = tf.matmul(Z1,W2) + b2            #(55000,10)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = Z2, labels = Y))

with tf.Session() as sess:
tf.initialize_all_variables().run()
for i in range(num_epochs):
x,y = mnist.train.next_batch(BATCH_SIZE)
sess.run(optimizer,feed_dict={X:x,Y:y})

if i%500 == 0:
cost_v = sess.run(cost,feed_dict={X:x,Y:y})
costs.append(cost_v)
print(i,cost_v)

# Calculate the correct accuracy
correct_prediction = tf.equal(tf.argmax(Z2,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("Train Accuracy:", accuracy.eval({X:mnist.train.images, Y: mnist.train.labels})) #Train Accuracy: 0.98807275
print ("Test Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))   #Test Accuracy: 0.9756

plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()


01-06 5万+

04-09 1万+

05-26 5905

05-18 8543

02-24 398

11-07 2万+

08-11 2万+

01-23 1872

09-23 1万+

10-28 1万+

03-03 4976

01-10 3万+

05-23 52

05-27 105

10-23 148

02-11 3058

06-05

01-26 5306

11-07 3万+

07-16 1万+

04-12 273

06-16 326

08-08 247

07-18 3184

01-15 408

08-16 6万+

05-06 464

11-09 808

05-27 1236

08-12 3万+

12-12 3558

10-17

12-21

05-16 106

11-16 1190

05-25 312

05-29 7883

05-26 64

05-12 62

05-15 16

04-26 165

03-05

05-21 1226

05-31 1万+

07-03 1万+

06-05

04-25 485

04-18 1597

05-10 2万+

04-22 183

05-17 2180

07-02 1万+

09-07 404

07-13 1194

07-22 1万+

03-27 5万+