改善手写数字识别的准确率过拟合

该博客介绍了如何通过在神经网络中添加L1正则化和Dropout层来缓解过拟合问题。首先,代码展示了如何在Keras中构建一个包含正则化的两层全连接网络,接着应用Dropout层以随机关闭一部分神经元。模型经过编译和训练后,在MNIST数据集上进行了评估,结果显示了正则化和Dropout对提高模型泛化能力的效果。
摘要由CSDN通过智能技术生成

在隐藏层添加正则化,并使得部分神经元丧失功能,可以改善准确率过拟合

network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),kernel_regularizer=regularizers.l1(0.0001)))# L1范式正则
network.add(layers.Dropout(0.01)) #以百分之一的概率使得神经元丧失功能

全部代码

#第二次优化识别,改善过拟合(添加正则化,且使部分网络丧失功能)
from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist
import matplotlib.pyplot as plt
# import torch
#加载数据集
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()#归一化处理,注意必须进行归一化操作,否则准确率非常低,图片和标签
# print(train_images.shape,test_images.shape)
# print(train_images[0])
# print(train_labels[0])
# plt.figure()
# plt.imshow(train_images[0])
# plt.show()
#将图片由二维铺开成一维
train_images=train_images.reshape((60000,28*28)).astype('float')  #将28*28的二维数组转变为784的一维数组,浮点数类型
test_images=test_images.reshape((10000,28*28)).astype ('float')
train_labels=to_categorical(train_labels)  #to_categorical就是将类别向量转换为二进制(只有0和1)的矩阵类型表示
test_labels=to_categorical(test_labels)
#print(train_labels[0])

#搭建神经网络
network = models.Sequential()   #选用的是Sequential 序贯模型
network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),kernel_regularizer=regularizers.l1(0.0001)))#添加一个(隐藏层)全连接层,神经元为15,激活函数是relu线性整流函数,输入形状为28*28
network.add(layers.Dropout(0.01)) #以百分之一的概率使得神经元丧失功能
network.add(layers.Dense(units=32, activation='relu',kernel_regularizer=regularizers.l1(0.0001)))#添加一个(隐藏层)全连接层,神经元为15,激活函数是relu线性整流函数,输入形状为28*28
network.add(layers.Dropout(0.01))
network.add(layers.Dense(units=10, activation='softmax'))#添加一个(输出层)全连接层,神经元为10,激活函数为softmax(Softmax 具有更好的解释性,包含属于猫的这一类的特征越多,输出为猫的概率就越大)

print(network.summary())  #查看神经网络model结构


#神经网络的训练
# 编译步骤,损失函数是模型优化的目标,优化器使用RMSporp,学习率为0.001,损失函数是categorical_crossentropy,评价函数为accuracy准确率
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# 训练网络,用fit函数(model.fit()方法用于执行训练过程), epochs表示训练多少个回合, batch_size表示每次训练给多大的数据,一次训练所选取的样本数。
network.fit(train_images, train_labels, epochs=22, batch_size=128, verbose=2)  #verbose:日志显示 0 为不在标准输出流输出日志信息 1 为输出进度条记录2 为每个epoch输出一行记录
print(network.summary())  #查看神经网络model结构

# 测试集上测试模型性能
y_pre = network.predict(test_images[:5])  #预测前五张图片的,model.predict 实际预测,其输出是目标值,根据输入数据预测。
print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)  #model.evaluate函数预测给定输入的输出
print("test_loss:", test_loss, "    test_accuracy:", test_accuracy)

更改之前

在这里插入图片描述

更改之后在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仓鼠球球-O-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值