gpt4 book ai didi

python - 多条件提前停止

转载 作者:行者123 更新时间:2023-12-03 16:47:41 26 4
gpt4 key购买 nike

我正在为推荐系统(项目推荐)进行多类分类,我目前正在使用 sparse_categorical_crossentropy 训练我的网络损失。因此,执行EarlyStopping是合理的。通过监控我的验证损失,val_loss像这样:

tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
它按预期工作。然而,网络(推荐系统)的性能是通过 Average-Precision-at-10 来衡量的,并在训练期间作为一个指标进行跟踪,如 average_precision_at_k10 .因此,我还可以使用此指标执行提前停止:
tf.keras.callbacks.EarlyStopping(monitor='average_precision_at_k10', patience=10)
这也按预期工作。
我的问题:
有时验证损失会增加,而 Average-Precision-at-10 会提高,反之亦然。因此,我需要监控 两者 ,并执行提前停止, 当且仅当 两者都在恶化。我想做什么:
tf.keras.callbacks.EarlyStopping(monitor=['val_loss', 'average_precision_at_k10'], patience=10)
这显然不起作用。任何想法如何做到这一点?

最佳答案

Gerry P 的指导下上面我设法创建了自己的自定义 EarlyStopping 回调,并认为我将其发布在这里以防其他人希望实现类似的功能。
两者 验证损失 patience 的平均精度在 10 处没有提高epoch 数,执行提前停止。

class CustomEarlyStopping(keras.callbacks.Callback):
def __init__(self, patience=0):
super(CustomEarlyStopping, self).__init__()
self.patience = patience
self.best_weights = None

def on_train_begin(self, logs=None):
# The number of epoch it has waited when loss is no longer minimum.
self.wait = 0
# The epoch the training stops at.
self.stopped_epoch = 0
# Initialize the best as infinity.
self.best_v_loss = np.Inf
self.best_map10 = 0

def on_epoch_end(self, epoch, logs=None):
v_loss=logs.get('val_loss')
map10=logs.get('val_average_precision_at_k10')

# If BOTH the validation loss AND map10 does not improve for 'patience' epochs, stop training early.
if np.less(v_loss, self.best_v_loss) and np.greater(map10, self.best_map10):
self.best_v_loss = v_loss
self.best_map10 = map10
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print("Restoring model weights from the end of the best epoch.")
self.model.set_weights(self.best_weights)

def on_train_end(self, logs=None):
if self.stopped_epoch > 0:
print("Epoch %05d: early stopping" % (self.stopped_epoch + 1))
然后用作:
model.fit(
x_train,
y_train,
batch_size=64,
steps_per_epoch=5,
epochs=30,
verbose=0,
callbacks=[CustomEarlyStopping(patience=10)],
)

关于python - 多条件提前停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64556120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com