[英]How to see the loss of the best epoch from early stopping in Keras?
I have managed to implement early stopping into my Keras model, but I am not sure how I can view the loss of the best epoch.我已经设法在我的 Keras model 中实现了提前停止,但我不确定如何查看最佳时代的损失。
es = EarlyStopping(monitor='val_out_soft_loss',
mode='min',
restore_best_weights=True,
verbose=2,
patience=10)
model.fit(tr_x,
tr_y,
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks=[es],
validation_data=(val_x, val_y))
loss = model.history.history["val_out_soft_loss"][-1]
return model, loss
The way I have defined the loss score, means that the returned score comes from the final epoch, not the best epoch.我定义损失分数的方式意味着返回的分数来自最终时期,而不是最佳时期。
Example:例子:
from sklearn.model_selection import train_test_split, KFold
losses = []
models = []
for k in range(2):
kfold = KFold(5, random_state = 42 + k, shuffle = True)
for k_fold, (tr_inds, val_inds) in enumerate(kfold.split(train_y)):
print("-----------")
print("-----------")
model, loss = get_model(64, 100)
models.append(model)
print(k_fold, loss)
losses.append(loss)
print("-------")
print(losses)
print(np.mean(losses))
Epoch 23/100
18536/18536 [==============================] - 7s 362us/step - loss: 0.0116 - out_soft_loss: 0.0112 - out_reg_loss: 0.0393 - val_loss: 0.0131 - val_out_soft_loss: 0.0127 - val_out_reg_loss: 0.0381
Epoch 24/100
18536/18536 [==============================] - 7s 356us/step - loss: 0.0116 - out_soft_loss: 0.0112 - out_reg_loss: 0.0388 - val_loss: 0.0132 - val_out_soft_loss: 0.0127 - val_out_reg_loss: 0.0403
Restoring model weights from the end of the best epoch
Epoch 00024: early stopping
0 0.012735568918287754
So in this example, I would like to see the loss at Epoch 00014 (which is 0.0124).所以在这个例子中,我希望看到 Epoch 00014(即 0.0124)的损失。
I also have a separate question: How can I set the decimal places for the val_out_soft_loss score?我还有一个单独的问题:如何设置 val_out_soft_loss 分数的小数位?
Assign the fit()
call in Keras to a variable so you can track the metrics through the epochs.将 Keras 中的fit()
调用分配给一个变量,以便您可以在各个时期跟踪指标。
history = model.fit(tr_x, ...
It will return a dictionary, access it like this:它将返回一个字典,像这样访问它:
loss_hist = history.history['loss']
And then get the min()
, max()
, or whatever you want.然后得到min()
、 max()
或任何你想要的。
np.min(loss_hist)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.