1. MNSIT介绍:
MNIST( http://yann.lecun.com/exdb/mnist/)是深度学习入门必定会接触到,很多的教程都是用这个数据集做例子
MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据
MNIST数据集包含四个部分:
>Training set image
>Training set labels
>Test set image
>Test set labels
2. CNN实现MNIST网络结结构(TensorFlow)
#coding=utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 1、准备数据
data = input_data.read_data_sets(train_dir="C:/Users/Leon/Desktop/AI/data/mnist/input_data/", one_hot=True)
# 2、输入层
x = tf.placeholder(dtype=tf.float32)
y = tf.placeholder(dtype=tf.int32)
# 3、卷积层1(激活)
x_image = tf.reshape(x, shape=[-1, 28, 28, 1])
filter1 = tf.Variable(tf.random_normal(shape=[5, 5, 1, 32]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding="SAME")
bias1 = tf.Variable(tf.random_normal(shape=[32]))
relu1 = tf.nn.relu(conv1 + bias1)
# 4、池化层1
pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# [14, 14, 32]
# 5、卷积层2(激活)
filter2 = tf.Variable(tf.random_normal(shape=[5, 5, 32, 64]))
conv2 = tf.nn.conv2d(pool1, filter2, strides=[1, 1, 1, 1], padding="SAME")
bias2 = tf.Variable(tf.random_normal(shape=[64]))
relu2 = tf.nn.relu(conv2 + bias2)
# 7、池化层2
pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# [7, 7, 64]
# 8、全连接层(激活)
fc_input = tf.reshape(pool2, shape=[-1, 7 * 7 * 64])
weight1 = tf.Variable(tf.random_normal(shape=[7 * 7 * 64, 1024]))
fc_bias1 = tf.Variable(tf.random_normal(shape=[1024]))
fc_out = tf.nn.relu(tf.matmul(fc_input, weight1) + fc_bias1)
# 10、原始输出层
weight2 = tf.Variable(tf.random_normal([1024, 10]))
fc_bias2 = tf.Variable(tf.random_normal(shape=[10]))
logits = tf.matmul(fc_out, weight2) + fc_bias2
# 11、Softmax回归
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits))
# 12、梯度下降,开启会话,训练模型
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(loss)
# 13、准确率的计算
equal_list = tf.equal(tf.argmax(y, axis=1), tf.argmax(logits, axis=1))
accuracy = tf.reduce_mean(tf.cast(equal_list, dtype=tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(100):
image_batch, label_batch = data.train.next_batch(batch_size=50)
sess.run(train_op, feed_dict={x: image_batch, y: label_batch})
print(sess.run(accuracy, feed_dict={x: image_batch, y: label_batch}))