tensorflow2.0入门(3):softmax多分类、参数选择和过拟合

1、softmax多分类模型

使用Fashion MNIST数据集,构建多分类模型,

(1)Fashion MNIST数据集包含10个类别中的70,000个灰度图像。

from tensorflow import keras

使用60,000张图像来训练网络和10,000张图像,以评估网络学习图像分类的准确程度。

展示一张图片:

import matplotlib.pyplot as plt

对图像数据进行归一化:

train_images = train_images / 255.0
test_images = test_images / 255.0

构造网络:

model = keras.Sequential(
[
    layers.Flatten(input_shape=[28, 28]),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

将二维图片数据通过Flatten层转化为一维的,输出为10个概率值,因此输出层激活函数使用softmax。

模型编译:

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

模型训练:

model.fit(train_images, train_labels, epochs=5)

在测试数据上进行模型评价:

model.evaluate(test_images, test_labels)

输出为:loss: 0.4053 - accuracy: 0.8075

2、网络优化和参数选择

网络中的神经元数越多,层数越多,神经网络的拟合能力越强,但是训练速度、难度越大,越容易产生过拟合。

增加两个隐藏层:

model = keras.Sequential(
[
    layers.Flatten(input_shape=[28, 28
]),
    layers.Dense(128, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])
model.summary()

增加迭代次数。
模型参数增加如下:

image

model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001),
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)

模型训练集结果为:loss: 0.3396 - accuracy: 0.8763
模型测试结果为:loss: 0.3350 - accuracy: 0.8512
准确率有明显上升。

3、过拟合和抑制方法

网络中的神经元数越多,层数越多,神经网络的拟合能力越强,但是训练速度、难度越大,越容易产生过拟合。

history = model.fit(train_images, train_labels, 
          epochs=10,
          validation_data=(test_images,test_labels))
#loss图          
plt.plot(history.epoch, history.history.get('loss'),label='loss')
plt.plot(history.epoch, history.history.get('val_loss'),label='val_loss')
plt.legend()
#accuracy图
plt.plot(history.epoch, history.history.get('accuracy'),label='accuracy')
plt.plot(history.epoch, history.history.get('val_accuracy'),label='val_accuracy')
plt.legend()

训练结果如图:

image

image

测试准确率远低于训练集,这就是过拟合。

通过dropout层来抑制过拟合,就是训练时随机将一部分隐藏层单元丢弃:

image

model2 = keras.Sequential(
[
    layers.Flatten(input_shape=[28, 28
]),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

结果是测试集准确率高于训练集,说明dropout对于抑制过拟合的作用。

image

此外,抑制过拟合还有减小网络容量和增加训练集数据的方法。

关注公众号,获取海量学习资源。

image

1024程序开发者社区的交流群已经建立,许多小伙伴已经加入其中,感谢大家的支持。大家可以在群里就技术问题进行交流,还没有加入的小伙伴可以扫描下方“社区物业”二维码,让管理员帮忙拉进群,期待大家的加入。

image

//猜你喜欢//

发布了19 篇原创文章 · 获赞 14 · 访问量 7574
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章