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

前言

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

一、交叉熵代价函数简介

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

二、交叉熵代价函数使用

     与第22篇唯一不同的是这边使用交叉熵代价函数,它的使用格式一般有两种:loss = ‘categorical_crossentropy’, # 或者使用loss = keras.losses.categorical_crossentropy,具体用法如下:

import numpy as np
from keras.datasets import mnist
# keras提供的numpy工具包
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import keras

# 载入数据
(x_train,y_train), (x_test,y_test) = mnist.load_data()

# (6000,28,28)
print('x_shape:', x_train.shape)
# (6000)
print('y_shape:', y_train.shape)

# 进行数据转换,并归一化
# (60000,28,28) -> (60000, 784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
# 换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 创建模型:输入784个神经元,输出10个神经元 784-10
model = Sequential([
        Dense(units=10, input_dim=784, bias_initializer='one', activation='softmax')
    ])

# 定义优化器,loss function, 训练过程中的准确率
model.compile(
    optimizer = sgd,
    loss = 'categorical_crossentropy', #  或者使用loss = keras.losses.categorical_crossentropy,
    metrics = ['accuracy']
)

# 进行模型训练
model.fit(x_train, y_train, batch_size=32, epochs=10)

# 评估模型
loss, accuracy = model.evaluate(x_test, y_test)

print('\ntest loss:', loss)
print('accuracy:', accuracy)

# 输出结果:
# x_shape: (60000, 28, 28)
# y_shape: (60000,)
# Epoch 1/10
# 60000/60000 [==============================] - 2s 41us/step - loss: 0.3778 - acc: 0.8930
# Epoch 2/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.3033 - acc: 0.9148
# Epoch 3/10
# 60000/60000 [==============================] - 2s 38us/step - loss: 0.2895 - acc: 0.9184
# Epoch 4/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.2830 - acc: 0.9209
# Epoch 5/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.2783 - acc: 0.9218
# Epoch 6/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.2744 - acc: 0.9234
# Epoch 7/10
# 60000/60000 [==============================] - 2s 41us/step - loss: 0.2713 - acc: 0.9243
# Epoch 8/10
# 60000/60000 [==============================] - 2s 38us/step - loss: 0.2691 - acc: 0.9256
# Epoch 9/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.2665 - acc: 0.9254
# Epoch 10/10
# 60000/60000 [==============================] - 2s 38us/step - loss: 0.2652 - acc: 0.9253
# 10000/10000 [==============================] - 0s 19us/step

# test loss: 0.2759707436442375
# accuracy: 0.9201

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

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


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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值