簡體   English   中英

為什么我的 val_loss 一開始就很低,然后增加,這對遷移學習有影響嗎?

[英]Why is my val_loss starting low and increasing does it even matters with transfer learning?

嘿伙計們,我是機器學習的新手,我正在從https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-to-classify-photos-of-dogs-and-cats運行此代碼/

我想了解為什么我的 val_loss 開始低然后增加。 這是過擬合還是欠擬合? 我還能用什么來改善 val_loss 使其更適合? 在博客文章中,他的交叉熵 plot 與我的有很大不同。 在此處輸入圖像描述

def define_model():
    # load model
    model = VGG16(include_top=False, input_shape=(224, 224, 3))
    # mark loaded layers as not trainable
    for layer in model.layers:
        layer.trainable = False
    # add new classifier layers
    flat1 = Flatten()(model.layers[-1].output)
    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)
    output = Dense(1, activation='sigmoid')(class1)
    # define new model
    model = Model(inputs=model.inputs, outputs=output)
    # compile model
    opt = SGD(lr=0.001, momentum=0.9)
    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
    return model


# plot diagnostic learning curves
def summarize_diagnostics(history):
    # plot loss
    pyplot.subplot(211)
    pyplot.title('Cross Entropy Loss')
    pyplot.plot(history.history['loss'], color='blue', label='train')
    pyplot.plot(history.history['val_loss'], color='orange', label='test')
    # plot accuracy
    pyplot.subplot(212)
    pyplot.title('Classification Accuracy')
    pyplot.plot(history.history['accuracy'], color='blue', label='train')
    pyplot.plot(history.history['val_accuracy'], color='orange', label='test')
    # save plot to file
    filename = sys.argv[0].split('/')[-1]
    pyplot.savefig(filename + '_plot.png')
    pyplot.close()


# run the test harness for evaluating a model
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',
                                           class_mode='binary', batch_size=64, target_size=(224, 224))
    test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',
                                          class_mode='binary', batch_size=64, target_size=(224, 224))
    # fit model

   
    history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
                                  validation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1)
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history)
    model.save('Transfer_Learning_Model.h5')


# entry point, run the test harness
run_test_harness()

若干問題。 VGG model 使用從 -1 到 +1 的像素值進行訓練,因此您需要添加以下內容

 def scaler(x):
        y=x/127.5-1
        return y
datagen = ImageDataGenerator(preprocessing_function=scaler)

我會刪除 featurewise_center=True。 如果你使用它,你必須在火車上跑步。 在 model.fit 中有 steps_per_epoch=len(train_it) 和 validation_steps=len(test_it)。 由於您在生成器中將 batch_size 設置為 64,因此這些值應該是 steps_per_epoch=len(train_it.labels)//64 和 validaion_steps=len(test_it)/64。 實際上在 model.fit 中忽略這些參數。 Model.fit 將在內部計算正確的值。 您的驗證損失曲線表明存在一定程度的過度擬合。 在 class1 層之后添加一個 drop out 層。 將輟學率設置為類似 2。 如果您想獲得最高精度,我建議您合並兩個 Keras 回調。 EarlyStopping 回調監控驗證損失,如果在“耐心”連續 epoch 數之后損失未能減少,則停止訓練。 設置 restore_best_weights=True 將加載具有最低驗證損失的時期的權重,因此您不必保存然后重新加載權重。 將 epochs 設置為較大的數字以確保激活此回調。 使用 keras 回調 ReduceLROnPlateau 根據驗證損失自動調整學習率。 回調的文檔位於此處。 我使用的代碼如下所示

es=tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=3,
                                     verbose=1,  restore_best_weights=True)
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1,
                                             verbose=1)
callbacks=[es, rlronp]

在 model.fit 添加回調=回調

暫無
暫無

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

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