Pycharm+Tensorflow的配置和安装在前面的博客已经介绍过。

Minst数据集可以再这里下载:Minst数据集下载

MNIST数据集包含了6w张图片作为训练数据,1w图片作为测试数据。在MNIST数据集中,每一张图片都代表了0~9中的一个数字,图片的大小都是28×28,且数字都会出现在图片的正中间。数据集包含了四个文件:

t10k-images-idx3-ubyte.gz     测试数据图片
t10k-labels-idx1-ubyte.gz       测试数据答案
train-images-idx3-ubyte.gz     训练数据图片
train-labels-idx1-ubyte.gz       训练数答案

每张图片的784个像素点组成长度为784的一维数组,作为输入特征

[0. 0. 0.  ......   0.38 0.45 0.64 ....    0. 0. 0. ]

图片的标签以一个一维数组的形式给出,每个元素表示对应分类出现的概率,比如数字“6”的标签

[0.  0.  0.  0.  0.  0.  1.  0.  0.  0. ] 
 

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets(r'F:\手写数字识别\新建文件夹\data', one_hot=True)

print("train data size:", mnist.train.num_examples)
print("validation data size:", mnist.validation.num_examples)
print("test data size:", mnist.test.num_examples)

t_lables0 = mnist.train.labels[1]  # 返回标签
t_iamge0 = mnist.train.images[1]  # 返回数据

t_iamge0.resize(28, 28)  # 将784个元素的标签转换为28*28
print('t_lables0: ', t_lables0)  # 查看标签

结果:

Pycharm+tensorflow+Softmax实现Mnist手写体数字识别_测试数据

搭建一个CNN网络实现识别Mnist手写数字识别:

总体步骤:

1.标注数据(训练集,测试集)

2.初始化权重矩阵W,偏置b

3.用softmax分类器进行分类,得出结果Y的预测值

4.求出Y的真实值和预测值之间的交叉熵得出残差

5.梯度下降法使得残差最小

6.计算测试数据的精确度

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 下载并加载数据
mst = input_data.read_data_sets(r'F:\手写数字识别\新建文件夹\data', one_hot=True)

# 数据与标签的占位
x = tf.placeholder(tf.float32, shape=[None, 784])
y_actual = tf.placeholder(tf.float32, shape=[None, 10])

# 初始化权重和偏置
W = tf.Variable(tf.zeros([784, 10])) # 权重矩阵
b = tf.Variable(tf.zeros([10])) # 偏置
# 使用sfm分类器进行分类
y_predict = tf.nn.softmax(tf.matmul(x, W) + b) # 得出y的预测值
# 求交叉熵得到残差
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict), reduction_indices=1))
# 梯度下降法使得残差最小
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# 测试阶段,测试准确度计算
correct_prediction = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y_actual, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))  # 多个批次的准确度均值

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    # 训练,迭代1000次
    for i in range(1000):
        batch_xs,batch_ys = mst.train.next_batch(100)  # 按批次训练,每批100行数据
        sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})  # 执行训练
        # 每训练100次,测试一次
        if i % 100 == 0:
            print("accuracy:", sess.run(accuracy, feed_dict={x: mst.test.images, y_actual: mst.test.labels}))

训练结果:

Pycharm+tensorflow+Softmax实现Mnist手写体数字识别_测试数据_02