深度阅读

How to implement early stopping in a Keras model?

作者
作者
2023年08月22日
更新时间
13.31 分钟
阅读时间
0
阅读量

To implement early stopping in a Keras model, you can use the EarlyStopping callback from Keras. This callback allows you to specify a metric to monitor during training, and will stop training if the monitored metric stops improving.

Here is an example of how to use the EarlyStopping callback:

from tensorflow import keras

model = keras.Sequential([keras.layers.Dense(10, input_shape=(784,), activation='softmax')])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)

history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels), callbacks=[early_stop])

In this example, monitor='val_loss' means that we will monitor the validation loss during training, and patience=3 means that we will stop training after 3 epochs of no improvement in the validation loss.

You can also specify additional arguments to the EarlyStopping callback, such as mode='min' to specify that the metric should be minimized, or restore_best_weights=True to automatically restore the weights of the best-performing model during training.

Once training is complete, you can access the training history and metrics using the history object returned by fit().

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。