zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

使用TensorFlow实现MNIST数据集分类

数据 实现 分类 Tensorflow mnist 使用
2023-09-11 14:20:52 时间

1 MNIST数据集

MNIST数据集由70000张28x28像素的黑白图片组成,每一张图片都写有0~9中的一个数字,每个像素点的灰度值在0 ~ 255(0是黑色,255是白色)之间。
在这里插入图片描述
MINST数据集是由Yann LeCun教授提供的手写数字数据库文件,其官方下载地址THE MNIST DATABASE of handwritten digits
在这里插入图片描述
下载好MNIST数据集后,将其放在Spyder工作目录下(若使用Jupyter编程,则放在Jupyter工作目录下),如图:
在这里插入图片描述
G:\Anaconda\Spyder为笔者Spyder工作目录,MNIST_data为新建文件夹,读者也可以自行命名。

2 实验

为方便设计神经网络输入层,将每张28x28像素图片的像素值按行排成一行,故输入层设计28x28=784个神经元,隐藏层设计600个神经元,输出层设计10个神经元。使用read_data_sets()函数载入数据集,并返回一个类,这个类将MNIST数据集划分为train、validation、test 3个数据集,对应图片数分别为55000、5000、10000。本文采用交叉熵损失函数,并且为防止过拟合问题产生,引入正则化方法。
mnist.py

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)

#每批次的大小
batch_size=100
#总批次数
batch_num=mnist.train.num_examples//batch_size
#训练轮数
training_step = tf.Variable(0,trainable=False)

#定义两个placeholder
x=tf.placeholder(tf.float32, [None,784])
y=tf.placeholder(tf.float32, [None,10])

#神经网络layer_1
w1=tf.Variable(tf.random_normal([784,600]))
b1=tf.Variable(tf.constant(0.1,shape=[600]))
z1=tf.matmul(x,w1)+b1
a1=tf.nn.tanh(z1)

#神经网络layer_2
w2=tf.Variable(tf.random_normal([600,10]))
b2=tf.Variable(tf.constant(0.1,shape=[10]))
z2=tf.matmul(a1,w2)+b2

#交叉熵代价函数
cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y,1),logits=z2)
#cross_entropy=tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=z2) 
#L2正则化函数
regularizer=tf.contrib.layers.l2_regularizer(0.0001)
#总损失
loss=tf.reduce_mean(cross_entropy)+regularizer(w1)+regularizer(w2)
#学习率(指数衰减法)
laerning_rate = tf.train.exponential_decay(0.8,training_step,batch_num,0.999)
#梯度下降法优化器
train=tf.train.GradientDescentOptimizer(laerning_rate).minimize(loss,global_step=training_step)

#预测精度
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(z2,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#初始化变量
init=tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    test_feed={x:mnist.test.images,y:mnist.test.labels}
    for epoch in range(51):
        for batch in range(batch_num):
            x_,y_=mnist.train.next_batch(batch_size)
            sess.run(train,feed_dict={x:x_,y:y_})
        acc=sess.run(accuracy,feed_dict=test_feed)
        if epoch%10==0:
            print("epoch:",epoch,"accuracy:",acc)  

在这里插入图片描述
迭代50次后,精度达到97.68%。