gpt4 book ai didi

machine-learning - 如何避免简单前馈网络的过度拟合

转载 作者:行者123 更新时间:2023-11-30 08:23:00 25 4
gpt4 key购买 nike

使用pima indians diabetes dataset我正在尝试使用 Keras 构建准确的模型。我编写了以下代码:

# Visualize training history
from keras import callbacks
from keras.layers import Dropout

tb = callbacks.TensorBoard(log_dir='/.logs', histogram_freq=10, batch_size=32,
write_graph=True, write_grads=True, write_images=False,
embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
# Visualize training history
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import numpy

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu', name='first_input'))
model.add(Dense(500, activation='tanh', name='first_hidden'))
model.add(Dropout(0.5, name='dropout_1'))
model.add(Dense(8, activation='relu', name='second_hidden'))
model.add(Dense(1, activation='sigmoid', name='output_layer'))

# Compile model
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=1000, batch_size=10, verbose=0, callbacks=[tb])
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

经过几次尝试,我添加了 dropout 层以避免过度拟合,但没有成功。下图显示验证损失和训练损失在某一点分离。

enter image description here

我还能做些什么来优化这个网络?

更新:根据我收到的评论,我对代码进行了如下调整:

model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', kernel_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l1(0.01), activation='relu',
name='first_input')) # added regularizers
model.add(Dense(8, activation='relu', name='first_hidden')) # reduced to 8 neurons
model.add(Dropout(0.5, name='dropout_1'))
model.add(Dense(5, activation='relu', name='second_hidden'))
model.add(Dense(1, activation='sigmoid', name='output_layer'))

这是 500 个时期的图表

enter image description here enter image description here

最佳答案

enter image description here

第一个示例的验证准确度 > 75%,第二个示例的验证准确度 < 65%,如果您比较 100 以下历元的损失,则第一个示例的损失小于 < 0.5,第二个示例的损失小于 < 0.5 0.6。但第二种情况如何更好呢?

对我来说第二个是欠拟合的情况:模型没有足够的学习能力。而第一种情况存在过拟合问题,因为它的训练在过拟合开始时并未停止(提前停止)。如果训练在 100 个 epoch 时停止,那么与两者相比,这将是一个更好的模型。

目标应该是在未见的数据中获得较小的预测误差,并为此增加网络的容量,直到超过拟合开始发生的点。

那么在这种特殊情况下如何避免过度拟合呢?采用提前停止

代码更改:包括提前停止输入缩放

 # input scaling
scaler = StandardScaler()
X = scaler.fit_transform(X)

# Early stopping
early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, mode='auto')

# create model - almost the same code
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu', name='first_input'))
model.add(Dense(500, activation='relu', name='first_hidden'))
model.add(Dropout(0.5, name='dropout_1'))
model.add(Dense(8, activation='relu', name='second_hidden'))
model.add(Dense(1, activation='sigmoid', name='output_layer')))

history = model.fit(X, Y, validation_split=0.33, epochs=1000, batch_size=10, verbose=0, callbacks=[tb, early_stop])

准确度损失图表:

enter image description here

关于machine-learning - 如何避免简单前馈网络的过度拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44909134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com