机器学习之实战篇——MNIST手写数字0~9识别(全连接神经网络模型)

机器学习之实战篇——Mnist手写数字0~9识别(全连接神经网络模型)

  • 文章传送
  • MNIST数据集介绍:
  • 实验过程
    • 实验环境
    • 导入模块
    • 导入MNIST数据集
    • 创建神经网络模型进行训练,测试,评估
    • 模型优化

文章传送

机器学习之监督学习(一)线性回归、多项式回归、算法优化[巨详细笔记]
机器学习之监督学习(二)二元逻辑回归
机器学习之监督学习(三)神经网络基础
机器学习之实战篇——预测二手房房价(线性回归)
机器学习之实战篇——肿瘤良性/恶性分类器(二元逻辑回归)

MNIST数据集介绍:

MNIST数据集是机器学习和计算机视觉领域中最知名和广泛使用的数据集之一。它是一个大型手写数字数据库,包含 70,000 张手写数字图像,60,000 张训练图像,10,000 张测试图像。每张图像是 28x28 像素的灰度图像素值范围从 0(白色)到 255(黑色),每张图像对应一个 0 到 9 的数字标签。

在实验开始前,为了熟悉这个伟大的数据集,读者可以先做一下下面的小实验,测验你的手写数字识别能力。尽管识别手写数字对于人类来说小菜一碟,但由于图像分辨率比较低同时有些数字写的比较抽象,因此想要达到100%准确率还是很难的,实验表明类的平均准确率约为97.5%到98.5%,实验代码如下:

import numpy as np
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
from random import sample# 加载MNIST数据集
(_, _), (x_test, y_test) = mnist.load_data()# 随机选择100个样本
indices = sample(range(len(x_test)), 100)correct = 0
total = 100for i, idx in enumerate(indices, 1):# 显示图像plt.imshow(x_test[idx], cmap='gray')plt.axis('off')plt.show()# 获取用户输入user_answer = input(f"问题 {i}/100: 这个数字是什么? ")# 检查答案if int(user_answer) == y_test[idx]:correct += 1print("正确!")else:print(f"错误. 正确答案是 {y_test[idx]}")print(f"当前正确率: {correct}/{i} ({correct/i*100:.2f}%)")print(f"\n最终正确率: {correct}/{total} ({correct/total*100:.2f}%)")

实验过程

实验环境

pycharm+jupyter notebook

导入模块

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.metrics import accuracy_score
from tensorflow.keras.layers import Input,Dense,Dropout
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.callbacks import EarlyStoppingimport matplotlib
matplotlib.rcParams['font.family'] = 'SimHei'  # 或者 'Microsoft YaHei'
matplotlib.rcParams['axes.unicode_minus'] = False  # 解决负号 '-'

导入MNIST数据集

导入mnist手写数字集(包括训练集和测试集)


from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

查看训练、测试数据集的规模

print(f'x_train.shape:{x_train.shape}')
print(f'y_train.shape:{y_train.shape}')
print(f'x_test.shape:{x_test.shape}')
print(f'y_test.shape:{y_test.shape}')
x_train.shape:(60000, 28, 28)
y_train.shape:(60000,)
x_test.shape:(10000, 28, 28)
y_test.shape:(10000,)

查看64张手写图片

#查看64张训练手写图片内容
#获取训练集规模
m=x_train.shape[0]
#创建4*4子图布局
fig,axes=plt.subplots(8,8,figsize=(8,8))
#每张子图随机呈现一张手写图片
for i,ax in enumerate(axes.flat):idx=np.random.randint(m)#imshow():传入图片的像素矩阵,cmap='gray',显示黑白图片ax.imshow(x_train[idx],cmap='gray')#设置子图标题,将图片标签显示在图片上方ax.set_title(y_train[idx])# 移除坐标轴ax.axis('off')
#调整子图之间的间距
plt.tight_layout()

(由于空间限制,没有展现全64张图片)
在这里插入图片描述

将图片灰度像素矩阵转为灰度像素向量[展平],同时进行归一化[/255](0-255->0-1)

x_train_flat=x_train.reshape(60000,28*28).astype('float32')/255
x_test_flat=x_test.reshape(10000,28*28).astype('float32')/255

查看展平后数据集规模

print(f'x_train.shape:{x_train_flat.shape}')
print(f'x_test.shape:{x_test_flat.shape}')
x_train.shape:(60000, 784)
x_test.shape:(10000, 784)

创建神经网络模型进行训练,测试,评估

初步创建第一个三层全连接神经网络,隐层中采用‘relu’激活函数,使用分类交叉熵损失函数(设置from_logits=True,减少训练过程计算误差),Adam学习率自适应器(设置初始学习率0.001)

#创建神经网络
model1=Sequential([Input(shape=(784,)),Dense(128,activation='relu',name='L1'),Dense(32,activation='relu',name='L2'),Dense(10,activation='linear',name='L3'),],name='model1',
)
#编译模型
model1.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model1.summary()
Model: "model1"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================L1 (Dense)                  (None, 128)               100480    L2 (Dense)                  (None, 32)                4128      L3 (Dense)                  (None, 10)                330       =================================================================
Total params: 104,938
Trainable params: 104,938
Non-trainable params: 0

model1拟合训练集开始训练,迭代次数初步设置为20

model1.fit(x_train_flat,y_train,epochs=20)
Epoch 1/20
1875/1875 [==============================] - 12s 5ms/step - loss: 0.2502
Epoch 2/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.1057
Epoch 3/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0748
Epoch 4/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0547
Epoch 5/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0438
Epoch 6/20
1875/1875 [==============================] - 8s 5ms/step - loss: 0.0360
Epoch 7/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0300
Epoch 8/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0237
Epoch 9/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0223
Epoch 10/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0201
Epoch 11/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0166
Epoch 12/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0172
Epoch 13/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0131
Epoch 14/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0124
Epoch 15/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0133
Epoch 16/20
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0108
Epoch 17/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0095
Epoch 18/20
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0116
Epoch 19/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0090
Epoch 20/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0084

查看model1训练结果,由于模型直接输出Logits,需要通过softmax函数激活输出概率向量,然后通过最大概率索引得出模型识别的手写数字

#查看训练结果
z_train_hat=model1.predict(x_train_flat)
#经过softmax激活后得到概率向量构成的矩阵
p_train_hat=tf.nn.softmax(z_train_hat).numpy()
#找出每个概率向量最大概率对应的索引,即识别的数字
y_train_hat=np.argmax(p_train_hat,axis=1)
print(y_train_hat)

可以将上述代码编写为函数:

#神经网络输出->最终识别结果
def get_result(z):p=tf.nn.softmax(z)y=np.argmax(p,axis=1)return y

为了理解上面的输出处理过程,查看第一个样本的逻辑输出、概率向量和识别数字

print(f'Logits:{z_train_hat[0]}')
print(f'Probabilities:{p_train_hat[0]}')
print(f'targe:{y_train_hat[0]}')
Logits:[-21.427883  -11.558845  -15.150495   15.6205845 -58.351833   29.704205-23.925339  -30.009314  -11.389831  -14.521982 ]
Probabilities:[6.2175050e-23 1.2013921e-18 3.3101813e-20 7.6482343e-07 0.0000000e+009.9999928e-01 5.1166414e-24 1.1661356e-26 1.4226123e-18 6.2059749e-20]
targe:5

输出model1训练准确率,准确率达到99.8%

print(f'model1训练集准确率:{accuracy_score(y_train,y_train_hat)}')
model1训练集准确率:0.998133

测试model1,准确率达到97.9%,相当不戳

z_test_hat=model1.predict(x_test_flat)
y_test_hat=get_result(z_test_hat)
print(f'model1测试集准确率:{accuracy_score(y_test,y_test_hat)}')
313/313 [==============================] - 1s 3ms/step
model1测试集准确率:0.9789

为了方便后续神经网络模型的实验,编写run_model函数包含训练、测试模型的整个过程,引入早停机制,即当10个epoch内训练损失没有改善,则停止训练

early_stopping = EarlyStopping(monitor='loss', patience=10,  # 如果10个epoch内训练损失没有改善,则停止训练restore_best_weights=True  # 恢复最佳权重
)def run_model(model,epochs):model.fit(x_train_flat,y_train,epochs=epochs,callbacks=[early_stopping]) z_train_hat=model.predict(x_train_flat)y_train_hat=get_result(z_train_hat)print(f'{model.name}训练准确率:{accuracy_score(y_train,y_train_hat)}')z_test_hat=model.predict(x_test_flat)y_test_hat=get_result(z_test_hat)print(f'{model.name}测试准确率:{accuracy_score(y_test,y_test_hat)}')

查看模型在哪些图片上栽了跟头:

#显示n张错误识别图片的函数def show_error_pic(x, y, y_pred, n=64):wrong_idx = (y != y_pred)# 获取错误识别的图片和标签x_wrong = x[wrong_idx]y_wrong = y[wrong_idx]y_pred_wrong = y_pred[wrong_idx]# 选择前n张错误图片n = min(n, len(x_wrong))x_wrong = x_wrong[:n]y_wrong = y_wrong[:n]y_pred_wrong = y_pred_wrong[:n]# 设置图片网格rows = int(np.ceil(n / 8))fig, axes = plt.subplots(rows, 8, figsize=(20, 2.5*rows))axes = axes.flatten()for i in range(n):ax = axes[i]ax.imshow(x_wrong[i].reshape(28, 28), cmap='gray')ax.set_title(f'True: {y_wrong[i]}, Pred: {y_pred_wrong[i]}')ax.axis('off')# 隐藏多余的子图for i in range(n, len(axes)):axes[i].axis('off')plt.tight_layout()plt.show()show_error_pic(x_test,y_test,y_test_hat)

(出于空间限制,只展示部分图片)
在这里插入图片描述

模型优化

目前来看我们第一个较简单的神经网络表现得非常不错,训练准确率达到99.8%,测试准确率达到97.9%,而人类的平均准确率约为97.5%到98.5%,因此我们诊断模型存在一定高方差的问题,可以考虑引入正则化技术或增加数据量来优化模型,或者从另一方面,考虑采用更加大型的神经网络看看能否达到更优的准确率。

model2:model1基础上,增加迭代次数至40次

#创建神经网络
model2=Sequential([Input(shape=(784,)),Dense(128,activation='relu',name='L1'),Dense(32,activation='relu',name='L2'),Dense(10,activation='linear',name='L3'),],name='model2',
)
#编译模型
model2.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model2.summary()run_model(model2,40)
Model: "model2"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================L1 (Dense)                  (None, 128)               100480    L2 (Dense)                  (None, 32)                4128      L3 (Dense)                  (None, 10)                330       =================================================================
Total params: 104,938
Trainable params: 104,938
Non-trainable params: 0
_________________________________________________________________
Epoch 1/40
1875/1875 [==============================] - 10s 5ms/step - loss: 0.2670
Epoch 2/40
1875/1875 [==============================] - 10s 5ms/step - loss: 0.1124
Epoch 3/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0786
Epoch 4/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0593
Epoch 5/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0468
Epoch 6/40
1875/1875 [==============================] - 8s 5ms/step - loss: 0.0377
Epoch 7/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0310
Epoch 8/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0266
Epoch 9/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0246
Epoch 10/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0183
Epoch 11/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0180
Epoch 12/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0160
Epoch 13/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0170
Epoch 14/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0133
Epoch 15/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0135
Epoch 16/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0117
Epoch 17/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0108
Epoch 18/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0110
Epoch 19/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0107
Epoch 20/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0086
Epoch 21/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0096
Epoch 22/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0101
Epoch 23/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0083
Epoch 24/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0079
Epoch 25/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0095
Epoch 26/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0087
Epoch 27/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0063
Epoch 28/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0087
Epoch 29/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0080
Epoch 30/40
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0069
Epoch 31/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0053
Epoch 32/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0071
Epoch 33/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0056
Epoch 34/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0089
Epoch 35/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0062
Epoch 36/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0084
Epoch 37/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0051
Epoch 38/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0063
Epoch 39/40
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0074
Epoch 40/40
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0063
1875/1875 [==============================] - 5s 3ms/step
model2训练准确率:0.9984166666666666
313/313 [==============================] - 1s 3ms/step
model2测试准确率:0.98

可以看到测试准确率达到98%,略有提升,但考虑到运行时间翻倍,收益并不明显

model3:采用宽度和厚度更大的神经网络,迭代次数20

#增加模型宽度和厚度
model3 = Sequential([Input(shape=(784,)),Dense(256, activation='relu', name='L1'),Dense(128, activation='relu', name='L2'),Dense(64, activation='relu', name='L3'),Dense(10, activation='linear', name='L4'),
], name='model3')#编译模型
model3.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model3.summary()run_model(model3,20)
Model: "model3"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================L1 (Dense)                  (None, 256)               200960    L2 (Dense)                  (None, 128)               32896     L3 (Dense)                  (None, 64)                8256      L4 (Dense)                  (None, 10)                650       =================================================================
Total params: 242,762
Trainable params: 242,762
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.2152
Epoch 2/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0908
Epoch 3/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0623
Epoch 4/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0496
Epoch 5/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0390
Epoch 6/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0341
Epoch 7/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0291
Epoch 8/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0244
Epoch 9/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0223
Epoch 10/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0187
Epoch 11/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0206
Epoch 12/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0145
Epoch 13/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0176
Epoch 14/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0153
Epoch 15/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0120
Epoch 16/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0148
Epoch 17/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0125
Epoch 18/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0123
Epoch 19/20
1875/1875 [==============================] - 13s 7ms/step - loss: 0.0120
Epoch 20/20
1875/1875 [==============================] - 13s 7ms/step - loss: 0.0094
1875/1875 [==============================] - 6s 3ms/step
model3训练准确率:0.9989333333333333
313/313 [==============================] - 1s 4ms/step
model3测试准确率:0.9816

model3训练准确率达到99.9%,测试准确率也取得了目前为止的新高98.2%

model4:model1基础上,加入Dropout层引入正则化

#Dropout正则化
model4 = Sequential([Input(shape=(784,)),Dense(128, activation='relu', name='L1'),Dropout(0.3),Dense(64, activation='relu', name='L2'),Dropout(0.2),Dense(10, activation='linear', name='L3'),
], name='model4')#编译模型
model4.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model4.summary()run_model(model4,20)
Model: "model5"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================L1 (Dense)                  (None, 128)               100480    dropout_2 (Dropout)         (None, 128)               0         L2 (Dense)                  (None, 64)                8256      dropout_3 (Dropout)         (None, 64)                0         L3 (Dense)                  (None, 10)                650       =================================================================
Total params: 109,386
Trainable params: 109,386
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
1875/1875 [==============================] - 15s 7ms/step - loss: 0.3686
Epoch 2/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.1855
Epoch 3/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1475
Epoch 4/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1289
Epoch 5/20
1875/1875 [==============================] - 20s 11ms/step - loss: 0.1124
Epoch 6/20
1875/1875 [==============================] - 19s 10ms/step - loss: 0.1053
Epoch 7/20
1875/1875 [==============================] - 22s 12ms/step - loss: 0.0976
Epoch 8/20
1875/1875 [==============================] - 15s 8ms/step - loss: 0.0907
Epoch 9/20
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0861
Epoch 10/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0807
Epoch 11/20
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0794
Epoch 12/20
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0744
Epoch 13/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0733
Epoch 14/20
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0734
Epoch 15/20
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0691
Epoch 16/20
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0656
Epoch 17/20
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0674
Epoch 18/20
1875/1875 [==============================] - 12s 7ms/step - loss: 0.0614
Epoch 19/20
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0601
Epoch 20/20
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0614
1875/1875 [==============================] - 5s 3ms/step
model5训练准确率:0.9951833333333333
313/313 [==============================] - 1s 2ms/step
model5测试准确率:0.98

model5训练准确率下降到了99.5%,但是相比model1测试准确率98%略有提升,Dropout正则化的确有效降低了模型方差,增强了模型的泛化能力

综上考虑,使用model3的框架同时引入Dropout正则化,迭代训练40次,构建model7

#最终全连接神经网络
model7 = Sequential([Input(shape=(784,)),Dense(256, activation='relu', name='L1'),Dropout(0.3),Dense(128, activation='relu', name='L2'),Dropout(0.2),Dense(64, activation='relu', name='L3'),Dropout(0.1),Dense(10, activation='linear', name='L4'),
], name='model7')#编译模型
model7.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model7.summary()run_model(model7,40)
Model: "model7"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================L1 (Dense)                  (None, 256)               200960    dropout_4 (Dropout)         (None, 256)               0         L2 (Dense)                  (None, 128)               32896     dropout_5 (Dropout)         (None, 128)               0         L3 (Dense)                  (None, 64)                8256      dropout_6 (Dropout)         (None, 64)                0         L4 (Dense)                  (None, 10)                650       =================================================================
Total params: 242,762
Trainable params: 242,762
Non-trainable params: 0
_________________________________________________________________
Epoch 1/40
1875/1875 [==============================] - 16s 8ms/step - loss: 0.3174
Epoch 2/40
1875/1875 [==============================] - 14s 7ms/step - loss: 0.1572
Epoch 3/40
1875/1875 [==============================] - 16s 9ms/step - loss: 0.1255
Epoch 4/40
1875/1875 [==============================] - 23s 12ms/step - loss: 0.1047
Epoch 5/40
1875/1875 [==============================] - 19s 10ms/step - loss: 0.0935
Epoch 6/40
1875/1875 [==============================] - 30s 16ms/step - loss: 0.0839
Epoch 7/40
1875/1875 [==============================] - 20s 11ms/step - loss: 0.0776
Epoch 8/40
1875/1875 [==============================] - 21s 11ms/step - loss: 0.0728
Epoch 9/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0661
Epoch 10/40
1875/1875 [==============================] - 14s 8ms/step - loss: 0.0629
Epoch 11/40
1875/1875 [==============================] - 16s 8ms/step - loss: 0.0596
Epoch 12/40
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0566
Epoch 13/40
1875/1875 [==============================] - 22s 12ms/step - loss: 0.0533
Epoch 14/40
1875/1875 [==============================] - 16s 8ms/step - loss: 0.0520
Epoch 15/40
1875/1875 [==============================] - 14s 7ms/step - loss: 0.0467
Epoch 16/40
1875/1875 [==============================] - 15s 8ms/step - loss: 0.0458
Epoch 17/40
1875/1875 [==============================] - 15s 8ms/step - loss: 0.0451
Epoch 18/40
1875/1875 [==============================] - 19s 10ms/step - loss: 0.0443
Epoch 19/40
1875/1875 [==============================] - 43s 23ms/step - loss: 0.0417
Epoch 20/40
1875/1875 [==============================] - 38s 20ms/step - loss: 0.0409
Epoch 21/40
1875/1875 [==============================] - 21s 11ms/step - loss: 0.0392
Epoch 22/40
1875/1875 [==============================] - 16s 9ms/step - loss: 0.0396
Epoch 23/40
1875/1875 [==============================] - 20s 11ms/step - loss: 0.0355
Epoch 24/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0368
Epoch 25/40
1875/1875 [==============================] - 18s 10ms/step - loss: 0.0359
Epoch 26/40
1875/1875 [==============================] - 18s 10ms/step - loss: 0.0356
Epoch 27/40
1875/1875 [==============================] - 16s 8ms/step - loss: 0.0360
Epoch 28/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0326
Epoch 29/40
1875/1875 [==============================] - 19s 10ms/step - loss: 0.0335
Epoch 30/40
1875/1875 [==============================] - 19s 10ms/step - loss: 0.0310
Epoch 31/40
1875/1875 [==============================] - 21s 11ms/step - loss: 0.0324
Epoch 32/40
1875/1875 [==============================] - 16s 9ms/step - loss: 0.0301
Epoch 33/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0303
Epoch 34/40
1875/1875 [==============================] - 15s 8ms/step - loss: 0.0319
Epoch 35/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0300
Epoch 36/40
1875/1875 [==============================] - 17s 9ms/step - loss: 0.0305
Epoch 37/40
1875/1875 [==============================] - 14s 7ms/step - loss: 0.0290
Epoch 38/40
1875/1875 [==============================] - 19s 10ms/step - loss: 0.0288
Epoch 39/40
1875/1875 [==============================] - 20s 11ms/step - loss: 0.0272
Epoch 40/40
1875/1875 [==============================] - 38s 20ms/step - loss: 0.0264
1875/1875 [==============================] - 18s 9ms/step
model7训练准确率:0.9984333333333333
313/313 [==============================] - 2s 5ms/step
model7测试准确率:0.9831

model7训练准确率99.8%,测试准确率达到了98.3%,相比model1的97.9%,取得了接近0.4%的提升。

本实验是学习了神经网络基础后的一个实验练习,因此只采用全连接神经网络模型。我们知道CNN模型在图像识别上能力更强,因此在实验最后创建一个CNN网络进行测试(gpt生成网络框架)。

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flattenmodel8 = Sequential([Input(shape=(28, 28, 1)),Conv2D(32, kernel_size=(3, 3), activation='relu'),MaxPooling2D(pool_size=(2, 2)),Conv2D(64, kernel_size=(3, 3), activation='relu'),MaxPooling2D(pool_size=(2, 2)),Flatten(),Dense(128, activation='relu'),Dense(10, activation='linear')
], name='cnn_model')#编译模型
model8.compile(loss=SparseCategoricalCrossentropy(from_logits=True),optimizer=Adam(learning_rate=0.001))
#查看模型总结
model8.summary()model8.fit(x_train,y_train,epochs=20,callbacks=[early_stopping]) 
z_train_hat=model8.predict(x_train)
y_train_hat=get_result(z_train_hat)
print(f'{model8.name}训练准确率:{accuracy_score(y_train,y_train_hat)}')z_test_hat=model8.predict(x_test)
y_test_hat=get_result(z_test_hat)
print(f'{model8.name}测试准确率:{accuracy_score(y_test,y_test_hat)}')

cnn网络:
cnn_model训练准确率:0.9982333333333333
cnn_model测试准确率:0.9878
可以看到测试准确率达到了98.8%,比我们上面的全连接神经网络要优异。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/422809.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt绘制动态仪表(模仿汽车仪表指针、故障灯)

背景: 项目需要,可能需要做一些仪表显示。此篇除了介绍实现方法,还要说明心路历程。对我而言,重要的是心理,而不是技术。写下来也是自勉。 本人起初心里是比较抵触的,从业20多年了,深知所谓界…

springboot Web基础开发

Spring Boot 是一个用于简化 Spring 应用开发的框架,它通过自动配置和开箱即用的功能,使得创建和部署 Spring 应用变得更为高效。以下是 Spring Boot 基础 Web 开发的一些关键点和实操总结: 1. 项目搭建 使用 Spring Initializr: 访问 Spring…

【Centos】关于容器启动Centos7缺失字符集的问题

最近做一些软件测试和打包,需要频繁的装系统,索性用docker启Centos容器来做,基础镜像做好直接启就好,过程中遇到一个和字符集有关的问题,记录一下,方便遇到同样问题的小伙伴。 问题描述 运行某些python脚本…

Stable Diffusion绘画 | ControlNet应用-Inpaint(局部重绘):更完美的重绘

Inpaint(局部重绘) 相当于小号的AI版PS,不但可以进行局部画面的修改,还可以去除背景中多余的内容,或者是四周画面内容的扩充。 预处理器说明 Inpaint_Global_Harmonious:重绘-全局融合算法,会对整个图片的画面和色调…

达梦CASE_SENSITIVE参数解析

1. 参数含义 标识符大小写敏感,默认值为 Y。 当大小写敏感时,小写的标识符应用双引号括起,否则被转换为大写;当大小写不敏感时,系统不自动转换标识符的大小写,在标识符比较时也不区分大小写。 CASE_SENS…

QtCreator学习(二).在stm32mp1中使用

0.配置编译环境 复制【正点原子】STM32MP157开发板(A盘)-基础资料\05、开发工具\01、交叉编译器st-example-image-qtwayland-openstlinux-weston-stm32mp1-x86_64-toolchain-3.1-snapshot.sh到虚拟机chmod添加可执行文件,./st*运行&#xff…

【JAVA】Tomcat性能优化、安全配置、资源控制以及运行模式超详细

文章目录 一、Tomcat性能优化application.yml配置maxThreads 连接数限制压缩传输AJP禁用 二、JVM方向优化设置并行垃圾回收器查看gc日志文件 三、Tomcat安全配置入侵防范禁用非法HTTP请求方法禁止目录列出防止恶意关闭服务配置HTTPS加密协议HttpOnly标记安全头配置 四、Tomcat资…

【鸿蒙】HarmonyOS NEXT星河入门到实战8-自定义组件-组件通信

目录 1、模块化语法 1.1 模块化基本认知 1.2 默认导出和导入 1.3 按需导出和导入 1.4 全部导入 2、自定义组件 -基础 2.1 自定义组件 - 基本使用 2.2 自定义组件 -通用样式 2.3 自定义组件 -成员函数变量 3、 状态管理 3.1 状态管理概述 3.2 State 自己的状态 3.3…

硬盘格式化后能恢复数据吗?教你如何恢复硬盘数据

在数字时代,硬盘作为存储数据的重要设备,承载着人们大量的工作文件、珍贵照片、重要视频等。然而,由于误操作、病毒感染或系统升级等原因,有时我们不得不对硬盘进行格式化。那么,硬盘格式化后,里面的数据还…

spring综合性利用工具-SpringBootVul-GUI(五)

项目地址 https://github.com/wh1t3zer/SpringBootVul-GUI 0x01简介 本着简单到极致的原则,开发了这么一款半自动化工具(PS:这个工具所包含了20个漏洞,开发不易,有任何问题可提issue) 尽管是一个为懒人量…

【免费刷题】实验室安全第一知识题库分享

道路千万条,实验安全第一条。 嘿,实验室的小伙伴们!是不是还在为实验室安全考试而烦恼?别担心,今天就让我来分享一些实用的题库,帮助你轻松应对考试,同时也更好地保护自己和实验室的安全。 一、…

petalinux开发 添加iperf

如何把iperf编译到petalinux工程中去 目录: /home/xxx/7z020/project-spec/meta-user/conf 里面有一个user-rootfsconfig文件 它默认里面有 CONFIG_gpio-demo CONFIG_peekpoke 把iperf添加进去 #Note: Mention Each package in individual line #These packages w…

网络安全实训八(y0usef靶机渗透实例)

1 信息收集 1.1 扫描靶机IP 1.2 收集靶机的端口开放情况 1.3 探测靶机网站的目录 1.4 发现可疑网站 1.5 打开可疑网站 2 渗透 2.1 使用BP获取请求 2.2 使用工具403bypasser.py探测可疑网页 2.3 显示可以添加头信息X-Forwarded-For:localhost来访问 2.4 添加之后转发&#xff…

如何在Django中创建新的模型实例

在 Django 中,创建新的模型实例可以通过以下几个步骤进行,通常包括定义模型、创建模型实例、保存数据到数据库,以及访问和操作这些实例。 1、问题背景 在 Django 中,可以使用 models.Model 类来创建模型,并使用 creat…

sqlguna靶场get shell

一、打开靶场,发现一个搜索框,尝试sql注入,发现可以注入,爆破数据库,表名,字段名以及 用户名密码 二、发现密码被MD5,解密后得到密码 三、进入后台界面登陆查看 四、发现添加新闻出可以上传图片…

前端开发macbook——NVM环境配置以及git配置流程

本文主要针对前端使用mac电脑时需要安装nvm对应环境,一文解决环境安装问题 主要步骤如下: 安装homebrew 安装nvm 安装git 第一步:安装homebrew /bin/bash -c "$(curl -fsSL https:/raw.githubusercontent.com/Homebrew/install/HE…

Redis 篇-深入了解分布式锁 Redisson 原理(可重入原理、可重试原理、主从一致性原理、解决超时锁失效)

🔥博客主页: 【小扳_-CSDN博客】 ❤感谢大家点赞👍收藏⭐评论✍ 本章目录 1.0 基于 Redis 实现的分布式锁存在的问题 2.0 Redisson 功能概述 3.0 Redisson 具体使用 4.0 Redisson 可重入锁原理 5.0 Redisson 锁重试原理 6.0 Redisson WatchDo…

第7篇:【系统分析师】计算机网络

考点汇总 考点详情 1网络模型和协议:OSI/RM七层模型,网络标准和协议,TCP/IP协议族,端口 七层:应用层,表示层,会话层,传输层,网络层,数据链路层,…

如何为Google RSA安排广告定制器 [2024]

近年来,响应式搜索广告(RSA)的人气稳步上升,这也就不足为奇了。通过谷歌的机器学习能力,RSA 提供了一种强大的方式来自动测试多个标题和描述,以确保更接近用户的意图。其好处显而易见:RSA 意味着…