我正在Python Keras中实现人工神经网络模型,并且在培训中获得了很高的准确性,但在测试中却获得了较低的准确性。这意味着数据中存在一些过拟合。
我想避免过拟合,其中一种技术是抖动或添加噪声。但是,我的问题是:如何在Python中做到这一点?
这是我的ANN代码:
def designANN(input_nodes, dropout, layer_nodes, output_nodes):
classifier = Sequential()
classifier.add(Dense(units = layer_nodes, kernel_initializer = "uniform",
activation = "relu", input_dim = input_nodes))
classifier.add(Dropout(dropout))
classifier.add(Dense(units = layer_nodes, kernel_initializer = "uniform",
activation = "relu"))
classifier.add(Dropout(dropout))
classifier.add(Dense(units = output_nodes, kernel_initializer = "uniform",
activation = "sigmoid"))
classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = [npv])
return classifier
您只需要GaussianNoise层。您可以将其放入网络中。我建议在激活功能之前使用它。这是relu的情况,如果我们添加随机噪声,则输出值可能超出范围(<0)]
def designANN(input_nodes, dropout, layer_nodes, output_nodes):
classifier = Sequential()
classifier.add(Dense(units = layer_nodes, kernel_initializer = "uniform",
input_dim = input_nodes))
classifier.add(GaussianNoise(0.1))
classifier.add(Activation('relu'))
classifier.add(Dropout(dropout))
classifier.add(Dense(units = layer_nodes, kernel_initializer = "uniform"))
classifier.add(GaussianNoise(0.1))
classifier.add(Activation('relu'))
classifier.add(Dropout(dropout))
classifier.add(Dense(units = output_nodes, kernel_initializer = "uniform",
activation = "sigmoid"))
classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = [npv])
return classifier
噪声通常是图像或信号添加到您的输入中,因此在这种情况下,它将取决于您的输入。