利用Keras中的权重约束减少深度神经网络中的过拟合
运行该示例报告列车和测试数据集上的模型性能。我们可以看到模型在训练数据集上的性能优于测试数据集,这是过度拟合的一个可能标志。鉴于神经网络和训练算法的随机性,您的具体结果可能会有所不同。因为模型是过度拟合的,所以我们通常不会期望在相同数据集上重复运行模型的精度差异(如果有的话)。 Train: 1.000, Test: 0.914 创建一个图,显示训练集和测试集上模型精度的曲线图。我们可以看到过度拟合模型的预期形状,其中测试精度增加到一个点然后再次开始减小。 具有加权约束的Overfit MLP 我们可以更新示例以使用权重约束。有一些不同的加权限制可供选择。这个模型的一个很好的简单约束是简单地标准化加权,使得范数等于1.0。此约束具有强制所有传入权重较小的效果。我们可以通过在Keras中使用unit_norm来实现。可以将此约束添加到第一个隐藏层,如下所示: model.add(Dense(500, input_dim=2, activation='relu', kernel_constraint=unit_norm())) 我们也可以通过使用min_max_norm并将min和maximum设置为1.0来实现相同的结果,例如: model.add(Dense(500, input_dim=2, activation='relu', kernel_constraint=min_max_norm(min_value=1.0, max_value=1.0))) 我们无法通过最大范数约束获得相同的结果,因为它允许规范等于或低于指定的限制; 例如: model.add(Dense(500, input_dim=2, activation='relu', kernel_constraint=max_norm(1.0))) 下面列出了具有单位规范约束的完整更新示例: # mlp overfit on the moons dataset with a unit norm constraint from sklearn.datasets import make_moons from keras.layers import Dense from keras.models import Sequential from keras.constraints import unit_norm from matplotlib import pyplot # generate 2d classification dataset X, y = make_moons(n_samples=100, noise=0.2, random_state=1) # split into train and test n_train = 30 trainX, testX = X[:n_train, :], X[n_train:, :] trainy, testy = y[:n_train], y[n_train:] # define model model = Sequential() model.add(Dense(500, input_dim=2, activation='relu', kernel_constraint=unit_norm())) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # fit model history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=4000, verbose=0) # evaluate the model _, train_acc = model.evaluate(trainX, trainy, verbose=0) _, test_acc = model.evaluate(testX, testy, verbose=0) print('Train: %.3f, Test: %.3f' % (train_acc, test_acc)) # plot history pyplot.plot(history.history['acc'], label='train') pyplot.plot(history.history['val_acc'], label='test') pyplot.legend() pyplot.show() 运行该示例报告训练集和测试数据集上的模型性能。我们可以看到,对权重大小的严格限制确实提高了模型在测试集上的性能,而不会影响训练集的性能。 Train: 1.000, Test: 0.943 回顾训练集的曲线和测试精度,我们可以看到模型已经过度拟合训练数据集了。训练集和测试集的模型精度继续提高到稳定水平。
(编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |