五神经网络过拟合处理方法:手写数字识别(Mnist)数据集

Posted 满目星辰wwq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了五神经网络过拟合处理方法:手写数字识别(Mnist)数据集相关的知识,希望对你有一定的参考价值。

神经网络过拟合处理方法

手写数字识别(Mnist)数据集

Mnist数据集是一个手写数字识别数据集,被称为深度学习界的“Hello World”。

在这里插入图片描述

Mnist数据集包含:

  • 训练集:60,000张28×28灰度图

  • 测试集:10,000张28×28灰度图

共有0~9这10个手写数字体类别。

导入必要的模块

import tensorflow as tf
from tensorflow.keras import datasets, regularizers, Input, Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, Activation
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import os

载入Mnist数据集

(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
(60000, 28, 28)
(60000,)
(10000, 28, 28)
(10000,)

查看数据集图片

定义画图函数plot_images

参数介绍:

  • images:包含多张图片数据的序列。

  • labels:包含图片对应标签的序列(序列中的元素需要是0,1,2,…,9这样的正整数)。

def plot_images(images, labels):
    class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    fig, axes = plt.subplots(3, 10, figsize=(30,8))
    axes = axes.flatten()
    for img, label, ax in zip(images, labels, axes):
        ax.imshow(img)
        ax.set_title(class_names[label])
        ax.axis('off')
    plt.tight_layout()
    plt.show()

随机抽取30张训练集图片进行查看

np.random.seed(99)
index_list = np.random.randint(0,59999,30)

plot_images(x_train[index_list], y_train[index_list])

随机抽取30张测试集图片进行查看

在这里插入图片描述

np.random.seed(99)
index_list = np.random.randint(0,9999,30)

plot_images(x_test[index_list], y_test[index_list])

在这里插入图片描述

数据预处理

图片都除以255,也就是归一化

x_train = x_train/255.0
x_test = x_test/255.0

为了达到过拟合的效果,选取1000张图片作为训练集。

np.random.seed(43)
index_list = np.random.randint(0,59999,1000)

x_train = x_train[index_list] 
y_train = y_train[index_list]

模型搭建

用到的api:

展平层tf.keras.layers.Flatten

在这里插入图片描述

全连接层tf.keras.layers.Dense

用到的参数:

  • units:输入整数,全连接层神经元个数。

  • activation:激活函数。

    可选项:

    • ‘sigmoid’:sigmoid激活函数

    • ‘tanh’:tanh激活函数

    • ‘relu’:relu激活函数

    • 'elu’或tf.keras.activations.elu(alpha=1.0):elu激活函数

    • ‘selu’:selu激活函数

    • ‘swish’: swish激活函数(tf2.2版本以上才有)

    • ‘softmax’: softmax函数

  • kernel_initializer:权重初始化,默认是’glorot_uniform’(即Xavier均匀初始化)。

    可选项:

    • ‘RandomNormal’:正态分布采样,均值为0,标准差0.05

    • ‘glorot_normal’:正态分布采样,均值为0,标准差stddev = sqrt(2 / (fan_in + fan_out))

    • ‘glorot_uniform’:均匀分布采样,范围[-limit, limit],标准差limit = sqrt(6 / (fan_in + fan_out))

    • ‘lecun_normal’:正态分布采样,均值为0,标准差stddev = sqrt(1 / fan_in)

    • ‘lecun_uniform’:均匀分布采样,范围[-limit, limit],标准差limit = sqrt(3 / fan_in)

    • ‘he_normal’:正态分布采样,均值为0,标准差stddev = sqrt(2 / fan_in)

    • ‘he_uniform’:均匀分布采样,范围[-limit, limit],标准差limit = sqrt(6 / fan_in)

    fan_in是输入的神经元个数,fan_out是输出的神经元个数。

  • kernel_regularizer: 正则项方法,一般使用tf.keras.regularizers.l2。

  • name:输入字符串,给该层设置一个名称。

模型设置tf.keras.Sequential.compile

用到的参数:

  • loss:损失函数,分类任务一般使用"sparse_categorical_crossentropy"。

  • optimizer:优化器,这里选用"adam",更多优化器请查看https://tensorflow.google.cn/api_docs/python/tf/keras/optimizers

  • metrics:评价指标,这里选用"accuracy",更多优化器请查看https://tensorflow.google.cn/api_docs/python/tf/keras/metrics

优化器tf.keras.optimizers.SGD

用到的参数:

  • learning_rate:学习率,默认为0.01。

搭建神经网络

  • 网络结构:20个隐层,每个隐层有500个神经元

  • 激活函数:tanh

  • 初始化:lecun_normal

  • 损失函数:交叉熵损失

  • 优化器:SGD,学习率0.001

  • 评价指标:accuracy

model = Sequential()

# 展平层flatten
model.add(Flatten(input_shape=(28, 28), name='flatten'))

# 隐层dense
for i in range(20):
    model.add(Dense(units=512, activation='tanh',
                    kernel_initializer='lecun_normal'))

# 加正则的隐层dense
for i in range(20):
    model.add(Dense(units=512, activation='tanh', kernel_initializer='lecun_normal',
                    kernel_regularizer=tf.keras.regularizers.l2(1e-5)))

# dropout层
# model.add(Dropout(rate=0.5))

# 输出层
model.add(Dense(units=10, activation='softmax', name='logit'))

# 设置损失函数loss、优化器optimizer、评价标准metrics
model.compile(loss='sparse_categorical_crossentropy', optimizer=tf.keras.optimizers.SGD(
    learning_rate=0.001), metrics=['accuracy'])

查看模型每层的参数量和输出的大小

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dense_1 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_3 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_4 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_5 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_6 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_7 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_8 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_9 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_10 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_11 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_12 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_13 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_14 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_15 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_16 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_17 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_18 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_19 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_20 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_21 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_22 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_23 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_24 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_25 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_26 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_27 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_28 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_29 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_30 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_31 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_32 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_33 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_34 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_35 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_36 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_37 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_38 (Dense)             (None, 512)               262656    
_________________________________________________________________
dense_39 (Dense)             (None, 512)               262656    
_________________________________________________________________
logit (Dense)                (None, 10)                5130      
=================================================================
Total params: 10,650,634
Trainable params: 10,650,634
Non-trainable params: 0
_________________________________________________________________

过拟合解决方法

增加训练数据量

  1. 采集更多的数据

  2. 数据增广(image augmentation):对已有数据做一系列随机改变,来产生相似但又不同的训练样本,从而扩大训练数据集的规模。这里不讲数据增广,有兴趣的可以查看tf.keras.preprocessing.image.ImageDataGenerator这个api。

减小模型复杂度

  1. 减少隐层

  2. 减少神经元个数

加正则项

tf.keras.regularizers.l2

用到的参数:

  • l:惩罚项,默认为0.01。

提前停止

tf.keras.callbacks.EarlyStopping

用到的参数:

  • monitor:监控的数据,一般为’val_loss’。

  • min_delta:定义模型改善的最小量,只有大于min_delta才会认为模型有改善,默认为0。

  • patience:有多少个epoch,模型没有改善,训练就会停止,默认为0。

  • restore_best_weights:是否使用监控数据最好的模型参数,如果是False,使用的是训练最后一步的模型参数,默认False。

# 设置EarlyStopping,val_loss经过10个epoch都没有改善就停止训练
earlystop = EarlyStopping(monitor='val_loss', min_delta=1e-4, 
                          patience=10, restore_best_weights=True)

Dropout

tf.keras.layers.Dropout

用到的参数:

  • rate:神经元失效的概率,取值0~1的浮点数。

  • seed:随机种子,取正整数。

  • name:输入字符串,给该层设置一个名称。

设置TensorBoard

tf.keras.callbacks.TensorBoard

用到的参数:

  • log_dir:tensorboard日志保存路径,默认为当前目录的’logs’文件。

  • histogram_freq:权重和截距的分布直方图的统计频率(多少个epoch或step统计一次),默认为0,也就是不统计频率,也不画出直方图。

  • write_graph:是否画出网络结构图,默认True。

  • write_images:是否画出输入图片,默认False。

  • update_freq:更新日志的频率,可选项有’batch’、‘epoch’和正整数,默认’epoch’。如果选择’batch’,表示每个batch都记录模型的loss和metrics;'epoch’同理;如果设定为正整数,如1000,表示每1000个batch记录一次模型的loss和metrics。注意,如果太频繁记录会导致模型训练很慢。

# 创建保存路径
model_name = "model-{}".format(int(time.time()))
logdir = os.path.join('logs', model_name)
if not os.path.exists(logdir):
    os.makedirs(logdir)

tensorboard = TensorBoard(log_dir=logdir, histogram_freq=1, write_graph=True, write_images=True, update_freq='epoch')

模型训练

tf.keras.Sequential.fit

用到的参数:

  • x:输入数据。

  • y:输入标签。

  • batch_size:一次梯度更新使用的数据量。

  • epochs:数据集跑多少轮模型训练,一轮表示整个数据集训练一次。

  • validation_split:验证集占总数据量的比例,取值0~1。

  • shuffle:每轮训练是否打乱数据顺序,默认True。

  • callbacks:回调函数,它是一组在训练的特定阶段被调用的函数集,你可以使用回调函数来观察训练过程中网络内部的状态和统计信息,通过传递回调函数列表到模型的.fit()中,即可在给定的训练阶段调用该函数集中的函数。

返回:History对象,History.history属性会记录每一轮训练集和验证集的损失函数值和评价指标。

# 开始模型训练
history = model.fit(x=x_train, y=y_train, batch_size=32,
                     epochs=200, validation_split=0.4,
                     shuffle=True, callbacks=[tensorboard])
Epoch 1/200
 2/19 [==>...........................] - ETA: 11s - loss: 2.4172 - accuracy: 0.0781WARNING:tensorflow:Method (on_train_batch_end) is slow compared to the batch update (0.704117). Check your callbacks.
19/19 [==============================] - 5s 285ms/step - loss: 2.3712 - accuracy: 0.1500 - val_loss: 2.3191 - val_accuracy: 0.1750
Epoch 2/200
19/19 [==============================] - 4s 184ms/step - loss: 2.2486 - accuracy: 0.3300 - val_loss: 2.1968 - val_accuracy: 0.4250
Epoch 3/200
19/19 [==============================] - 4s 190ms/step - loss: 2.1014 - accuracy: 0.4600 - val_loss: 2.0500 - val_accuracy: 0.4600
Epoch 4/200
19/19 [==============================] - 4s 192ms/step - loss: 1.9412 - accuracy: 0.5150 - val_loss: 1.9069 - val_accuracy: 0.4700
Epoch 5/200
19/19 [==============================] - 3s 183ms/step - loss: 1.7935 - accuracy: 0.5383 - val_loss: 1.7905 - val_accuracy: 0.4875
Epoch 6/200
19/19 [==============================] - 4s 187ms/step - loss: 1.6675 - accuracy: 0.5633 - val_loss: 1.6802 - val_accuracy: 0.5050
Epoch 7/200
19/19 [==============================] - 4s 189ms/step - loss: 1.5546 - accuracy: 0.6017 - val_loss: 1.5876 - val_accuracy: 0.5250
Epoch 8/200
19/19 [==============================] - 4s 190ms/step - loss: 1.4534 - accuracy: 0.6233 - val_loss: 1.5052 - val_accuracy: 0.5575
Epoch 9/200
19/19 [==============================] - 4s 188ms/step - loss: 1.3624 - accuracy: 0.6517 - val_loss: 1.4285 - val_accuracy: 0.5875
Epoch 10/200
19/19 [==============================] - 4s 189ms/step - loss: 1.2894 - accuracy: 0.6733 - val_loss: 1.3671 - val_accuracy: 0.6100
Epoch 11/200
19/19 [==============================] - 4s 188ms/step - loss: 1.2197 - accuracy: 0.7050 - val_loss: 1.3083 - val_accuracy: 0.6400
Epoch 12/200
19/19 [==============================] - 4s 187ms/step - loss: 1.1535 - accuracy: 0.7400 - val_loss: 1.2598 - val_accuracy: 0.6525
Epoch 13/200
19/19 [==============================] - 3s 180ms/step - loss: 1.0948 - accuracy: 0.7533 - val_loss: 1.2115 - val_accuracy: 0.6825
Epoch 14/200
19/19 [==============================] - 4s 194ms/step - loss: 1.0389 - accuracy: 0.7800 - val_loss: 1.1674 - val_accuracy: 0.7025
Epoch 15/200
19/19 [==============================] - 3s 182ms/step - loss: 0.9875 - accuracy: 0.7917 - val_loss: 1.1367 - val_accuracy: 0.6900
Epoch 16/200
19/19 [==============================] - 4s 189ms/step - loss: 0.9337 - accuracy: 0.8067 - val_loss: 1.1019 - val_accuracy: 0.7050
Epoch 17/200
19/19 [==============================] - 4s 197ms/step - loss: 0.8906 - accuracy: 0.8200 - val_loss: 1.0556 - val_accuracy: 0.7250
Epoch 18/200
19/19 [==============================] - 4s 188ms/step - loss: 0.8467 - accuracy: 0.8300 - val_loss: 1.0149 - val_accuracy: 0.7450
Epoch 19/200
19/19 [==============================] - 4s 197ms/step - loss: 0.8050 - accuracy: 0.8483 - val_loss: 0.9865 - val_accuracy: 0.7525
Epoch 20/200
19/19 [==============================] - 3s 180ms/step - loss: 0.7667 - accuracy: 0.8700 - val_loss: 0.9653 - val_accuracy: 0.7575
Epoch 21/200
19/19 [==============================] - 4s 188ms/step - loss: 0.7319 - accuracy: 0.8783 - val_loss: 0.9364 - val_accuracy: 0.7725
Epoch 22/200
19/19 [==============================] - 4s 188ms/step - loss: 0.6989 - accuracy: 0.8833 - val_loss: 0.9158 - val_accuracy: 0.7875
Epoch 23/200
19/19 [==============================] - 4s 187ms/step - loss: 0.6696 - accuracy: 0.8917 - val_loss: 0.8995 - val_accuracy: 0.7875
Epoch 24/200
19/19 [==============================] - 3s 182ms/step - loss: 0.6449 - accuracy: 0.8983 - val_loss: 0.8799 - val_accuracy: 0.7825
Epoch 25/200
19/19 [==============================] - 4s 185ms/step - loss: 0.6143 - accuracy: 0.9200 - val_loss: 0.8642 - val_accuracy: 0.7825
Epoch 26/200
19/19 [==============================] - 4s 194ms/step - loss: 0.5879 - accuracy: 0.9150 - val_loss: 0.8607 - val_accuracy: 0.7925
Epoch 27/200
19/19 [==============================] - 4s 216ms/step - loss: 0.5699 - accuracy: 0.9183 - val_loss: 0.8380 - val_accuracy: 0.8100
Epoch 28/200
19/19 [==============================] - 5s 237ms/step - loss: 0.5476 - accuracy: 0.9250 - val_loss: 0.8264 - val_accuracy: 0.7975
Epoch 29/200
19/19 [==============================] - 4s 234ms/step - loss: 0.5273 - accuracy: 0.9317 - val_loss: 0.8240 - val_accuracy: 0.8050
Epoch 30/200
19/19 [==============================] - 5s 238ms/step - loss: 0.5063 - accuracy: 0.9350 - val_loss: 0.8178 - val_accuracy: 0.8100
Epoch 31/200
19/19 [==============================] - 4s 235ms/step - loss: 0.4863 - accuracy: 0.9433 - val_loss: 0.8144 - val_accuracy: 0.8000
Epoch 32/200
19/19 [==============================] - 4s 222ms/step - loss: 0.4730 - accuracy: 0.9433 - val_loss: 0.8116 - val_accuracy: 0.8225
Epoch 33/200
19/19 [==============================] - 4s 232ms/step - loss: 0.4546 - accuracy: 0.9533 - val_loss: 0.8066 - val_accuracy: 0.7950
Epoch 34/200
19/19 [==============================] - 4s 222ms/step - loss: 0.4411 - accuracy: 0.9500 - val_loss: 0.7922 - val_accuracy: 0.8125
Epoch 35/200
19/19 [==============================] - 4s 229ms/step - loss: 0.4260 - accuracy: 0.9567 - val_loss: 0.7828 - val_accuracy: 0.8150
Epoch 36/200
19/19 [==============================] - 5s 238ms/step - loss: 0.4102 - accuracy: 0.9550 - val_loss: 0.7931 - val_accuracy: 0.8150
Epoch 37/200
19/19 [==============================] - 4s 186ms/step - loss: 0.4000 - accuracy: 0.9567 - val_loss: 0.7897 - val_accuracy: 0.8050
Epoch 38/200
19/19 [==============================] - 4s 190ms/step - loss: 0.3893 - accuracy: 0.9650 - val_loss: 0.7867 - val_accuracy: 0.8025
Epoch 39/200
19/19 [==============================] - 3s 180ms/step - loss: 0.3740 - accuracy: 0.9667 - val_loss: 0.7854 - val_accuracy: 0.8000
Epoch 40/200
19/19 [==============================] - 4s 185ms/step - loss: 0.3659 - accuracy: 0.9683 - val_loss: 0.7773 - val_accuracy: 0.8100
Epoch 41/200
19/19 [==============================] - 4s 193ms/step - loss: 0.3557 - accuracy: 0.9717 - val_loss: 0.7713 - val_accuracy: 0.7975
Epoch 42/200
19/19 [==============================] - 4s 196ms/step - loss: 0.3471 - accuracy: 0.9717 - val_loss: 0.7718 - val_accuracy: 0.8050
Epoch 43/200
19/19 [==============================] - 4s 188ms/step - loss: 0.3352 - accuracy: 0.9767 - val_loss: 0.7745 - val_accuracy: 0.8100
Epoch 44/200
19/19 [==============================] - 3s 181ms/step - loss: 0.3277 - accuracy: 0.9800 - val_loss: 0.7695 - val_accuracy: 0.8100
Epoch 45/200
19/19 [==============================] - 4s 191ms/step - loss: 0.3181 - accuracy: 0.9733 - val_loss: 0.7641 - val_accuracy: 0.8075
Epoch 46/200
19/19 [==============================] - 4s 193ms/step - loss: 0.3084 - accuracy: 0.9900 - val_loss: 0.7651 - val_accuracy: 0.8125
Epoch 47/200
19/19 [==============================] - 4s 188ms/step - loss: 0.3010 - accuracy: 0.9900 - val_loss: 0.7661 - val_accuracy: 0.8075
Epoch 48/200
19/19 [==============================] - 4s 195ms/step - loss: 0.2928 - accuracy: 0.9883 - val_loss: 0.7723 - val_accuracy: 0.8000
Epoch 49/200
19/19 [==============================] - 4s 187ms/step - loss: 0.2864 - accuracy: 0.9900 - val_loss: 0.7715 - val_accuracy: 0.8075
Epoch 50/200
19/19 [==============================] - 4s 205ms/step - loss: 0.2798 - accuracy: 0.9917 - val_loss: 0.7827 - val_accuracy: 0.7900
Epoch 51/200
19/19 [==============================] - 3s 181ms/step - loss: 0.2742 - accuracy: 0.9933 - val_loss: 0.7697 - val_accuracy: 0.8050
Epoch 52/200
19/19 [==============================] - 4s 189ms/step - loss: 0.2658 - accuracy: 0.9933 - val_loss: 0.7735 - val_accuracy: 0.8050
Epoch 53/200
19/19 [==============================] - 4s 187ms/step - loss: 0.2609 - accuracy: 0.9933 - val_loss: 0.7763 - val_accuracy: 0.7975
Epoch 54/200
19/19 [==============================] - 4s 191ms/step - loss: 0.2566 - accuracy: 0.9917 - val_loss: 0.7694 - val_accuracy: 0.8075
Epoch 55/200
19/19 [==============================] - 4s 196ms/step - loss: 0.2490 - accuracy: 0.9933 - val_loss: 0.7626 - val_accuracy: 0.8100
Epoch 56/200
19/19 [==============================] - 3s 184ms/step - loss: 0.2459 - accuracy: 0.9933 - val_loss: 0.7731 - val_accuracy: 0.8075
Epoch 57/200
19/19 [==============================] - 4s 191ms/step - loss: 0.2394 - accuracy: 0.9967 - val_loss: 0.7745 - val_accuracy: 0.8000
Epoch 58/200
19/19 [==============================] - 4s 185ms/step - loss: 0.2341 - accuracy: 0.9983 - val_loss: 0.7792 - val_accuracy: 0.7975
Epoch 59/200
19/19 [==============================] - 3s 181ms/step - loss: 0.2302 - accuracy: 0.9950 - val_loss: 0.7717 - val_accuracy: 0.7950
Epoch 60/200
19/19 [==============================] - 3s 183ms/step - loss: 0.2263 - accuracy: 0.9983 - val_loss: 0.7832 - val_accuracy: 0.7975
Epoch 61/200
19/19 [==============================] - 3s 181ms/step - loss: 0.2218 - accuracy: 0.9967 - val_loss: 0.7798 - val_accuracy: 0.8025
Epoch 62/200
19/19 [==============================] - 3s 180ms/step - loss: 0.2182 - accuracy: 0.9983 - val_loss: 0.7756 - val_accuracy: 0.8025
Epoch 63/200
19/19 [==============================] - 3s 181ms/step - loss: 0.2140 - accuracy: 0.9983 - val_loss: 0.7765 - val_accuracy: 0.8000
Epoch 64/200
19/19 [==============================] - 4s 197ms/step - loss: 0.2114 - accuracy: 0.9983 - val_loss: 0.7844 - val_accuracy: 0.7975
Epoch 65/200
19/19 [==============================] - 3s 184ms/step - loss: 0.2087 - accuracy: 0.9983 - val_loss: 0.7819 - val_accuracy: 0.8075
Epoch 66/200
19/19 [==============================] - 4s 186ms/step - loss: 0.2048 - accuracy: 0.9983 - val_loss: 0.7815 - val_accuracy: 0.8000
Epoch 67/200
19/19 [==============================] - 3s 174ms/step - loss: 0.2019 - accuracy: 0.9983 - val_loss: 0.7838 - val_accuracy: 0.7950
Epoch 68/200
19/19 [==============================] - 3s 179ms/step - loss: 0.1987 - accuracy: 1.0000 - val_loss: 0.7768 - val_accuracy: 0.7950
Epoch 69/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1960 - accuracy: 1.0000 - val_loss: 0.7880 - val_accuracy: 0.7950
Epoch 70/200
19/19 [==============================] - 4s 191ms/step - loss: 0.1941 - accuracy: 1.0000 - val_loss: 0.7923 - val_accuracy: 0.7975
Epoch 71/200
19/19 [==============================] - 4s 186ms/step - loss: 0.1907 - accuracy: 1.0000 - val_loss: 0.7883 - val_accuracy: 0.7975
Epoch 72/200
19/19 [==============================] - 3s 180ms/step - loss: 0.1876 - accuracy: 0.9983 - val_loss: 0.7864 - val_accuracy: 0.7975
Epoch 73/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1856 - accuracy: 1.0000 - val_loss: 0.7858 - val_accuracy: 0.7950
Epoch 74/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1825 - accuracy: 1.0000 - val_loss: 0.7901 - val_accuracy: 0.8025
Epoch 75/200
19/19 [==============================] - 4s 189ms/step - loss: 0.1813 - accuracy: 1.0000 - val_loss: 0.7878 - val_accuracy: 0.7925
Epoch 76/200
19/19 [==============================] - 3s 178ms/step - loss: 0.1792 - accuracy: 1.0000 - val_loss: 0.7900 - val_accuracy: 0.7925
Epoch 77/200
19/19 [==============================] - 4s 194ms/step - loss: 0.1768 - accuracy: 1.0000 - val_loss: 0.7964 - val_accuracy: 0.7775
Epoch 78/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1754 - accuracy: 1.0000 - val_loss: 0.7921 - val_accuracy: 0.7950
Epoch 79/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1732 - accuracy: 1.0000 - val_loss: 0.7962 - val_accuracy: 0.7875
Epoch 80/200
19/19 [==============================] - 4s 189ms/step - loss: 0.1712 - accuracy: 1.0000 - val_loss: 0.7909 - val_accuracy: 0.7925
Epoch 81/200
19/19 [==============================] - 4s 184ms/step - loss: 0.1692 - accuracy: 1.0000 - val_loss: 0.7955 - val_accuracy: 0.7875
Epoch 82/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1680 - accuracy: 1.0000 - val_loss: 0.7954 - val_accuracy: 0.7975
Epoch 83/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1661 - accuracy: 1.0000 - val_loss: 0.8050 - val_accuracy: 0.7775
Epoch 84/200
19/19 [==============================] - 4s 190ms/step - loss: 0.1649 - accuracy: 1.0000 - val_loss: 0.8020 - val_accuracy: 0.7850
Epoch 85/200
19/19 [==============================] - 3s 184ms/step - loss: 0.1636 - accuracy: 1.0000 - val_loss: 0.8026 - val_accuracy: 0.7875
Epoch 86/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1624 - accuracy: 1.0000 - val_loss: 0.8027 - val_accuracy: 0.7875
Epoch 87/200
19/19 [==============================] - 4s 187ms/step - loss: 0.1603 - accuracy: 1.0000 - val_loss: 0.8082 - val_accuracy: 0.7775
Epoch 88/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1589 - accuracy: 1.0000 - val_loss: 0.8035 - val_accuracy: 0.7900
Epoch 89/200
19/19 [==============================] - 3s 181ms/step - loss: 0.1578 - accuracy: 1.0000 - val_loss: 0.8086 - val_accuracy: 0.7850
Epoch 90/200
19/19 [==============================] - 3s 176ms/step - loss: 0.1568 - accuracy: 1.0000 - val_loss: 0.8114 - val_accuracy: 0.7725
Epoch 91/200
19/19 [==============================] - 3s 179ms/step - loss: 0.1554 - accuracy: 1.0000 - val_loss: 0.8096 - val_accuracy: 0.7850
Epoch 92/200
19/19 [==============================] - 3s 179ms/step - loss: 0.1542 - accuracy: 1.0000 - val_loss: 0.8129 - val_accuracy: 0.7850
Epoch 93/200
19/19 [==============================] - 4s 185ms/step - loss: 0.1534 - accuracy: 1.0000 - val_loss: 0.8090 - val_accuracy: 0.7875
Epoch 94/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1522 - accuracy: 1.0000 - val_loss: 0.8057 - val_accuracy: 0.7900
Epoch 95/200
19/19 [==============================] - 3s 184ms/step - loss: 0.1513 - accuracy: 1.0000 - val_loss: 0.8085 - val_accuracy: 0.7925
Epoch 96/200
19/19 [==============================] - 4s 193ms/step - loss: 0.1499 - accuracy: 1.0000 - val_loss: 0.8110 - val_accuracy: 0.7875
Epoch 97/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1490 - accuracy: 1.0000 - val_loss: 0.8170 - val_accuracy: 0.7850
Epoch 98/200
19/19 [==============================] - 3s 176ms/step - loss: 0.1481 - accuracy: 1.0000 - val_loss: 0.8100 - val_accuracy: 0.7875
Epoch 99/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1471 - accuracy: 1.0000 - val_loss: 0.8174 - val_accuracy: 0.7875
Epoch 100/200
19/19 [==============================] - 4s 187ms/step - loss: 0.1465 - accuracy: 1.0000 - val_loss: 0.8178 - val_accuracy: 0.7725
Epoch 101/200
19/19 [==============================] - 4s 188ms/step - loss: 0.1457 - accuracy: 1.0000 - val_loss: 0.8147 - val_accuracy: 0.7900
Epoch 102/200
19/19 [==============================] - 3s 173ms/step - loss: 0.1447 - accuracy: 1.0000 - val_loss: 0.8154 - val_accuracy: 0.7925
Epoch 103/200
19/19 [==============================] - 3s 184ms/step - loss: 0.1437 - accuracy: 1.0000 - val_loss: 0.8284 - val_accuracy: 0.7775
Epoch 104/200
19/19 [==============================] - 3s 173ms/step - loss: 0.1434 - accuracy: 1.0000 - val_loss: 0.8199 - val_accuracy: 0.7900
Epoch 105/200
19/19 [==============================] - 3s 179ms/step - loss: 0.1424 - accuracy: 1.0000 - val_loss: 0.8194 - val_accuracy: 0.7825
Epoch 106/200
19/19 [==============================] - 3s 178ms/step - loss: 0.1415 - accuracy: 1.0000 - val_loss: 0.8200 - val_accuracy: 0.7950
Epoch 107/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1410 - accuracy: 1.0000 - val_loss: 0.8213 - val_accuracy: 0.7875
Epoch 108/200
19/19 [==============================] - 3s 183ms/step - loss: 0.1405 - accuracy: 1.0000 - val_loss: 0.8229 - val_accuracy: 0.7850
Epoch 109/200
19/19 [==============================] - 3s 179ms/step - loss: 0.1395 - accuracy: 1.0000 - val_loss: 0.8242 - val_accuracy: 0.7950
Epoch 110/200
19/19 [==============================] - 3s 178ms/step - loss: 0.1392 - accuracy: 1.0000 - val_loss: 0.8255 - val_accuracy: 0.7900
Epoch 111/200
19/19 [==============================] - 3s 175ms/step - loss: 0.1384 - accuracy: 1.0000 - val_loss: 0.8224 - val_accuracy: 0.7925
Epoch 112/200
19/19 [==============================] - 4s 190ms/step - loss: 0.1376 - accuracy: 1.0000 - val_loss: 0.8268 - val_accuracy: 0.7825
Epoch 113/200
19/19 [====================

以上是关于五神经网络过拟合处理方法:手写数字识别(Mnist)数据集的主要内容,如果未能解决你的问题,请参考以下文章

TensorFlow入门实战|第1周:实现mnist手写数字识别

TensorFlow入门实战|第1周:实现mnist手写数字识别

TensorFlow入门实战|第1周:实现mnist手写数字识别

深度学习100例-卷积神经网络(CNN)实现mnist手写数字识别 | 第1天

tensorflow 基础学习五:MNIST手写数字识别

深度学习100例-卷积神经网络(CNN)实现mnist手写数字识别 | 第1天