huggingface config_error ai_generated true

ValueError: 提前停止需要在训练参数中设置 `load_best_model_at_end=True`,但它当前为 False。

ValueError: Early stopping requires `load_best_model_at_end=True` in the training arguments, but it is set to False.

ID: huggingface/trainer-early-stopping-config

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2024-04-05首次发现

版本兼容性

版本状态引入弃用备注
transformers 4.39.0 active
Python 3.10 active

根因分析

在 `TrainingArguments` 中 `load_best_model_at_end` 为 False 时使用了 `EarlyStoppingCallback`,但提前停止需要此标志为 True 才能保存最佳模型。

English

The `EarlyStoppingCallback` is used with `TrainingArguments` where `load_best_model_at_end` is False, but early stopping requires this flag to be True to save the best model.

generic

官方文档

https://huggingface.co/docs/transformers/en/main_classes/callback#transformers.EarlyStoppingCallback

解决方案

  1. 在 `TrainingArguments` 中设置 `load_best_model_at_end=True`,并适当设置 `metric_for_best_model` 和 `greater_is_better`。例如:
    
    from transformers import TrainingArguments, Trainer, EarlyStoppingCallback
    training_args = TrainingArguments(
        output_dir='./results',
        load_best_model_at_end=True,
        metric_for_best_model='eval_loss',
        greater_is_better=False,
        evaluation_strategy='steps',
        save_strategy='steps',
    )
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
    )
  2. 如果不需要提前停止,从回调列表中移除 `EarlyStoppingCallback`,并依赖手动检查点选择。

无效尝试

常见但无效的做法:

  1. 90% 失败

    The default is False, so removing it doesn't change behavior; the error persists.

  2. 95% 失败

    This directly contradicts the requirement; early stopping cannot function without loading the best model.