在隐藏层添加正则化,并使得部分神经元丧失功能,可以改善准确率过拟合
network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),kernel_regularizer=regularizers.l1(0.0001)))
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
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
train_images=train_images.reshape((60000,28*28)).astype('float')
test_images=test_images.reshape((10000,28*28)).astype ('float')
train_labels=to_categorical(train_labels)
test_labels=to_categorical(test_labels)
network = models.Sequential()
network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),kernel_regularizer=regularizers.l1(0.0001)))
network.add(layers.Dropout(0.01))
network.add(layers.Dense(units=32, activation='relu',kernel_regularizer=regularizers.l1(0.0001)))
network.add(layers.Dropout(0.01))
network.add(layers.Dense(units=10, activation='softmax'))
print(network.summary())
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
network.fit(train_images, train_labels, epochs=22, batch_size=128, verbose=2)
print(network.summary())
y_pre = network.predict(test_images[:5])
print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)
print("test_loss:", test_loss, " test_accuracy:", test_accuracy)
更改之前
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e55f0b7f1b02bc1dda0e9d6589e66272.png#pic_center)
更改之后![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/48f933a0a7ceea45081050c010440526.png#pic_center)