[英]keras evaluate() using best model not last model
我正在使用ModelCheckpoint save_best_only=True
和EarlyStopping patience=5
訓練我的 model。 我使用我的測試數據進行驗證。 當使用相同的測試數據和相同的回調應用evaluate()
時,我得到與最后一個時期模型的val_
指標相同的指標,而不是“最佳模型”指標。 我如何使用evaluate()
並獲得最佳模型的指標? 我想對過時數據使用evaluate()
來獲取這些指標進行比較,並且需要確保它使用的是最好的 model。
model.fit(x_train, y_train, callbacks=[stopper, checkpoint], validation_data=(x_test, y_test))
model.evaluate(x_test, y_test, callbacks=[stopper, checkpoint])
期望來自第 4 個時期的指標,但從第 9 個時期獲得指標
訓練結束后,您需要加載best_model
,然后對其進行評估。 另外我不確定您是否需要model.evaluate()
中的回調。 所以代碼可能是這樣的,
# callback to save the best model
checkpoint_path = 'best_model.h5'
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_best_only=True)
# train
model.fit(x_train,
y_train,
callbacks=[stopper, checkpoint],
validation_data=(x_test, y_test))
# load best model
model.load_weights(checkpoint_path)
# evaluate
model.evaluate(x_test, y_test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.