python3 8.tensorflow使用交叉熵代价函数进行MNIST数据集简单分类 学习笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mcyJacky/article/details/86501565

前言

     计算机视觉系列之学习笔记主要是本人进行学习人工智能(计算机视觉方向)的代码整理。本系列所有代码是用python3编写,在平台Anaconda中运行实现,在使用代码时,默认你已经安装相关的python库,这方面不做多余的说明。本系列所涉及的所有代码和资料可在我的github上下载到,gitbub地址:https://github.com/mcyJacky/DeepLearning-CV,如有问题,欢迎指出。

一、交叉熵代价函数简介

     之前第7篇对MNIST数据集进行简单分类使用代价函数是均方差代价函数(二次代价函数),这种代价函数更加适用于回归问题处理。本篇我们使用交叉熵(Cross Entropy)代价函数,它更加适用于分类问题,它的基本公式是:
E = ( t ln y + ( 1 t ) ln ( 1 y ) ) E = -(t \ln y + (1-t) \ln (1-y))
其中, y y 是预测值, t t 是正确值。当两者越是接近时, E E 会越小。

二、交叉熵代价函数使用

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 = 64
# 计算一个周期一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

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

# 创建一个简单的神经网络:784-10
W = tf.Variable(tf.truncated_normal([784,10], stddev=0.1))
b = tf.Variable(tf.zeros([10]) + 0.1)
prediction = tf.nn.softmax(tf.matmul(x,W)+b)


# 二次代价函数
# loss = tf.losses.mean_squared_error(y, prediction)
# 交叉熵代价函数
loss = tf.losses.softmax_cross_entropy(y, prediction)

# 使用梯度下降法
train = tf.train.GradientDescentOptimizer(0.3).minimize(loss)

# 结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# 创建会话
with tf.Session() as sess:
    # 变量初始化
    sess.run(tf.global_variables_initializer())
    # 周期epoch:所有数据训练一次,就是一个周期
    for epoch in range(21):
        for batch in range(n_batch):
            # 获取一个批次的数据和标签
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train,feed_dict={x:batch_xs,y:batch_ys})
        # 每训练一个周期做一次测试
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

# 打印结果:    
# Iter 0,Testing Accuracy 0.7473
# Iter 1,Testing Accuracy 0.8413
# Iter 2,Testing Accuracy 0.9066
# Iter 3,Testing Accuracy 0.9113
# Iter 4,Testing Accuracy 0.9143
# Iter 5,Testing Accuracy 0.9168
# Iter 6,Testing Accuracy 0.9199
# Iter 7,Testing Accuracy 0.9201
# Iter 8,Testing Accuracy 0.9202
# Iter 9,Testing Accuracy 0.9213
# Iter 10,Testing Accuracy 0.921
# Iter 11,Testing Accuracy 0.9205
# Iter 12,Testing Accuracy 0.9214
# Iter 13,Testing Accuracy 0.923
# Iter 14,Testing Accuracy 0.9237
# Iter 15,Testing Accuracy 0.9238
# Iter 16,Testing Accuracy 0.924
# Iter 17,Testing Accuracy 0.9231
# Iter 18,Testing Accuracy 0.9246
# Iter 19,Testing Accuracy 0.925
# Iter 20,Testing Accuracy 0.9253

     使用交叉熵代价函数后,通过训练21个周期,得到测试集的准确率约92.53%,因这个训练集比较简单,所以和之前使用均方差代价函数的计算结果相差不大。在大型复杂的项目中,我们还会通常用到DropOut正则化、使用其他优化器的方法,这个我们再下面篇幅介绍。

     
     
     
     
【参考】:
     1. 城市数据团课程《AI工程师》计算机视觉方向
     2. deeplearning.ai 吴恩达《深度学习工程师》
     3. 《机器学习》作者:周志华
     4. 《深度学习》作者:Ian Goodfellow


转载声明:
版权声明:非商用自由转载-保持署名-注明出处
署名 :mcyJacky
文章出处:https://blog.csdn.net/mcyJacky

猜你喜欢

转载自blog.csdn.net/mcyJacky/article/details/86501565