簡體   English   中英

每次運行一個神經網絡

[英]Each run for a Neural Network

神經網絡的每次運行都不同嗎? 我想提一下,我有一個記錄在案的非常好的性能,現在模型在我每次運行時的性能都不一樣。 有種子命令可以使用嗎?

我正在寫一篇關於 VGG 16 模型的計算機科學研究論文,所以我如何實際報告一個好的運行。 現在我有 CSV 記錄器和從 tensorflow.keras.callbacks 導入的 ModelCheckpoint。 如果每次運行都不同,那么在選擇模型輸出時的做法是什么?

train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range=0.1,
rotation_range=5)

test_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range=0.1,
rotation_range=5)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = "categorical")

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size = (img_height, img_width),
    batch_size = batch_size,
    class_mode = "categorical"
)
lr_schedule1 =  ExponentialDecay(
            initial_learning_rate = .9,
            decay_steps=100000,
            decay_rate=0.96,
            staircase=True)
model = VGG16(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
for layer in model.layers[:10]: 
    layer.trainable = False
x = model.output 
x = Flatten()(x)
x = Dense(3, activation='relu')(x)
x = Dense(3, activation='selu') (x)
predictions = Dense(num_classes, activation="softmax")(x)
model_final = Model(model.input, predictions)
model_final.compile(loss = "binary_crossentropy", 
                    optimizer = tf_SGD(learning_rate =lr_schedule1),
                    metrics= ["accuracy"])
checkpoint = ModelCheckpoint("weather1.h5", monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=10, verbose=1, mode='auto')
filename = str(input("What is the desired filename? \n \t"))
csv_logger = CSVLogger(f'{filename}.log')

history_object = model_final.fit(
train_generator, 
#steps_per_epoch = 42, #nb_train_samples,
epochs = 100, #epochs=15,
validation_data = validation_generator,
#validation_steps = nb_validation_samples,
callbacks = [checkpoint, early,csv_logger])

在此處輸入圖片說明

所以這段代碼有幾個問題會導致你看到的性能問題:

  1. 用於驗證數據集的ImageDataGenerator引入了隨機增強。 你不應該做任何增強,因為性能評估是不可重復的,而且你沒有與以前的時代進行比較的基礎。 你不會知道性能是由於網絡學習還是批次中的某些東西使它變得更好。 您將需要刪除這些,而只需添加rescale操作。 您當然可以為訓練生成器保留增強功能,但您應該為驗證/測試進行隨機增強:
test_datagen = ImageDataGenerator(rescale = 1./255)
  1. 您對輸出層的最終激活是 ReLU 變體。 但是,您指定binary_crossentropy作為損失函數。 您應該使用適合分類的輸出層。 任何 ReLU 變體都不適合。 因為您使用了 3 個標簽,所以softmax會更合適:
x = Dense(3, activation='relu')(x)
x = Dense(3, activation='softmax') (x)
  1. 此外,最后一層之前的最后一個 Dense 層正在將巨大的潛在空間向量壓縮到三個維度。 我不相信這在語義上正確地捕獲了類,所以我建議使用更多的神經元。 也許是128? 256? 您需要對此進行試驗:
x = Dense(128, activation='relu')(x)
x = Dense(3, activation='softmax') (x)
  1. 回到損失函數,請確保您使用正確的損失函數進行多類分類。 sparse_categorical_crossentropy這里會更合適:
model_final.compile(loss = "sparse_categorical_crossentropy", 
                    optimizer = tf_SGD(learning_rate =lr_schedule1),
                    metrics= ["accuracy"])
  1. 最后,您可能想要使用更激進的優化器,例如Adam 我會同時嘗試 SGD(你現在擁有的)和 Adam,看看哪個合適。

希望這在某種程度上有所幫助。 如果您解決了遇到的性能錯誤,請告訴我們!

暫無
暫無

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

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