每次我 运行 一个神经网络时表现不一致

Inconsistent performance each time I run 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 指定为损失函数。您应该使用适合 classification 的输出层。任何 ReLU 变体都不适合。因为您使用了 3 个标签,所以 softmax 会更合适:
x = Dense(3, activation='relu')(x)
x = Dense(3, activation='softmax')(x)
  1. 此外,最后一层之前的最后一个 Dense 层正在进行巨大的潜在 space 矢量压缩,直至三维。我不相信这在语义上正确地捕获了 classes,所以我建议使用更多的神经元。也许128? 256?您需要试验一下:
x = Dense(128, activation='relu')(x)
x = Dense(3, activation='softmax') (x)
  1. 回到损失函数,请确保您使用正确的损失函数进行多重class class化。 sparse_categorical_crossentropy 此处更合适:
model_final.compile(loss = "sparse_categorical_crossentropy", 
                    optimizer = tf_SGD(learning_rate =lr_schedule1),
                    metrics= ["accuracy"])
  1. 最后,您可能希望使用更激进的优化器,例如 Adam。我会同时尝试 SGD(你现在拥有的)和 Adam,看看哪个合适。

希望这对您有所帮助。如果您解决了遇到的性能错误,请告诉我们!