簡體   English   中英

dropout層會提高精度嗎

[英]will dropout layer enhance accuracy

我知道在 CNN 模型中添加一個 dropout 層可以提高准確性,因為它減少了過擬合的影響。 但是,我構建了一個具有 16,32 和 64 個過濾器、大小為 3 且最大池為 2 的 CNN 模型,並注意到沒有 dropout 層的模型在所有情況下都比具有 dropout 層的模型表現更好。

from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from IPython.display import display
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.metrics import classification_report, confusion_matrix
import keras
from keras.layers import BatchNormalization
from keras.optimizers import Adam
import pickle

classifier = Sequential()
classifier.add(Conv2D(16,(3,3),input_shape=(200,200,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(128))
classifier.add(Activation('relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(7))
classifier.add(Activation('softmax'))
classifier.summary()
classifier.compile(optimizer =keras.optimizers.Adam(lr=0.001),
                   loss ='categorical_crossentropy',
                   metrics =['accuracy'])
train_datagen = ImageDataGenerator(rescale =1./255,
                                   shear_range =0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip =True)
test_datagen = ImageDataGenerator(rescale = 1./255)

batchsize=10
training_set = train_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/Journal_Paper/Malware_Families/Spectrogram/Train/',            
                                                target_size=(200,200),
                                                batch_size= batchsize,
                                                class_mode='categorical')

test_set = test_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/Journal_Paper/Malware_Families/Spectrogram/Validate/',    
                                           target_size = (200,200),
                                           batch_size = batchsize,
                       shuffle=False,
                                           class_mode ='categorical')
history=classifier.fit_generator(training_set,
                        steps_per_epoch = 2340 // batchsize,
                        epochs = 100,
                        validation_data =test_set,
                        validation_steps = 781 // batchsize)

classifier.save('16_With_Dropout_rl_001.h5')
with open('16_With_Dropout_rl_001.h5', 'wb') as file_pi:
        pickle.dump(history.history, file_pi)
Y_pred = classifier.predict_generator(test_set, steps= 781 // batchsize+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys()) 
target_names = ['coinhive','emotet','fareit','gafgyt','mirai','ramnit','razy']  
report = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(report) 

# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy 16 with dropout rl .001')
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 16 with dropout rl .001')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明

我知道在 CNN 模型中添加一個 dropout 層可以提高准確性,因為它減少了過擬合的影響。

你可以這樣說,但一般來說並不成立。 Dropout 層是一種泛化技術,它會降低模型的靈活性,假設您的模型足夠靈活以處理任務(實際上,假設您的模型比需要的更靈活),則可以防止過度擬合。 如果您的模型一開始就無法處理任務,這意味着它太弱了,那么添加任何類型的正則化可能只會降低其性能。

話雖如此,當您包含不止一個卷積層時,CNN 通常會表現得更好。 這個想法是更深的卷積層學習更復雜的特征,而靠近輸入的層只學習基本形狀(當然,這取決於網絡本身的結構和任務的復雜性)。 並且由於您通常希望包含更多卷積層,因此此類模型的復雜性(和靈活性)會增加,這可能導致過度擬合,因此需要正則化技術。 (3 個帶正則化的卷積層通常會勝過一個不帶正則化的卷積層)。

您的設計僅包含一個卷積層。 我建議將多個卷積/池化層堆疊在一起,並在必要時添加一些 dropout 層來對抗過度擬合(可能很難看到正則化對這樣一個簡單模型的任何積極影響)。

我同意@Matus Dubrava 所說的一切,但也建議您嘗試將輟學率降低到遠低於 0.5。 通常,人們使用介於 0.15 和 0.3 之間的值。 我通常使用0.2。 嘗試幾個不同的值,看看哪個效果最好。 而且,就像 Matus 建議的那樣,嘗試更多的卷積層。 我在表格和圖像生成模型的三個 CN 架構方面取得了很大的成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM