加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

操作Keras中的权重束缚镌汰深度神经收集中的过拟合

发布时间:2020-07-16 01:36:28 所属栏目:创业 来源:站长网
导读:副问题#e# 权重束缚提供了一种要领,用于镌汰深度进修神经收集模子对实习数据的太过拟合,并改进模子对新数据(譬喻测试集)的机能。有多种范例的权重束缚,譬喻最大和单元向量类型,有些必要必需设置的超参数。 在本教程中,您将发明Keras API,用于向深度

运行该示例会建设一个散点图,表现每个类中调查的半圆形或玉轮外形。我们可以看到点的分手中的噪音使得卫星不太明明。

操作Keras中的权重束缚镌汰深度神经收集中的过拟合

这是一个很好的测试题目,由于类不能用一行来脱离,譬喻不是线性可分的,必要非线性要领,如神经收集来办理。我们只天生了100个样本,这对付神经收集而言很小,提供了太过拟合实习数据集的机遇,而且在测试数据集上具有更高的偏差:行使正则化的一个好例子。另外,样本具有噪声,使模子有机遇进修纷歧致的样本的各个方面。

太过多层感知器

我们可以开拓一个MLP模子来办理这个二进制分类题目。该模子将具有一个潜匿层,其具有比办理该题目所需的节点更多的节点,从而提供太过拟合的机遇。我们还将实习模子的时刻高出确保模子太过所需的时刻。在我们界说模子之前,我们将数据集拆分为实习集和测试集,行使30个示例来实习模子,行使70个示例来评估拟合模子的机能。

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:] 

接下来,我们可以界说模子。潜匿层行使潜匿层中的500个节点和整流的线性激活函数。在输出层中行使S形激活函数以猜测0或1的类值。该模子行使二元交错熵丧失函数举办优化,合用于二元分类题目和梯度降落的有用Adam版本。

# define model  model = Sequential()  model.add(Dense(500, input_dim=2, activation='relu'))  model.add(Dense(1, activation='sigmoid'))  model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 

然后,界说的模子拟合4,000个实习数据,默认批量巨细为32。我们还将行使测试数据集作为验证数据集。

# 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() 

我们可以将全部这些部门组合在一路; 下面列出了完备的示例。

# mlp overfit on the moons dataset  from sklearn.datasets import make_moons  from keras.layers import Dense  from keras.models import Sequential  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'))  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() 

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读