且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 keras 中保存***模型

更新时间:2022-01-10 07:45:27

EarlyStoppingModelCheckpoint 是您从 Keras 文档中需要的.

EarlyStopping and ModelCheckpoint is what you need from Keras documentation.

您应该在 ModelCheckpoint 中设置 save_best_only=True.如果需要任何其他调整,都是微不足道的.

You should set save_best_only=True in ModelCheckpoint. If any other adjustments needed, are trivial.

为了帮助你更多,你可以看到一个用法在 Kaggle 上.

Just to help you more you can see a usage here on Kaggle.

如果上面的 Kaggle 示例链接不可用,请在此处添加代码:

Adding the code here in case the above Kaggle example link is not available:

model = getModel()
model.summary()

batch_size = 32

earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')
mcp_save = ModelCheckpoint('.mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='min')
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')

model.fit(Xtr_more, Ytr_more, batch_size=batch_size, epochs=50, verbose=0, callbacks=[earlyStopping, mcp_save, reduce_lr_loss], validation_split=0.25)