EarlyStopping 放在方法中时不起作用。
EarlyStopping在运行时不起作用:
def model_training():
# model, train_images, val_images, train_labels, val_images DEFINED HERE
train_it = ImageDataGenerator().flow(train_images, y=train_labels, batch_size=32)
val_it = ImageDataGenerator().flow(val_images, y=val_labels, batch_size=32)
mc = ModelCheckpoint('model_name.h5', monitor='val_acc', save_best_only=True)
es = EarlyStopping(monitor='val_loss',patience=1)
history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
validation_data=val_it, validation_steps=len(val_it),
epochs=50, callbacks=[es, mc])
_, acc = model.evaluate_generator(val_it, steps=len(val_it), verbose=1)
model_training()
EarlyStopping 在运行时有效:
# model, train_images, val_images, train_labels, val_images DEFINED HERE
train_it = ImageDataGenerator().flow(train_images, y=train_labels, batch_size=32)
val_it = ImageDataGenerator().flow(val_images, y=val_labels, batch_size=32)
mc = ModelCheckpoint('model_name.h5', monitor='val_acc', save_best_only=True)
es = EarlyStopping(monitor='val_loss',patience=1)
history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
validation_data=val_it, validation_steps=len(val_it),
epochs=50, callbacks=[es, mc])
_, acc = model.evaluate_generator(val_it, steps=len(val_it), verbose=1)
知道为什么吗?
更新:
EarlyStopping 仅在 patience=1
.