# 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`
- **Domain:** huggingface
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| transformers 4.39.0 | active | — | — |
| Python 3.10 | active | — | — |

## Workarounds

1. **Set `load_best_model_at_end=True` in `TrainingArguments` and also set `metric_for_best_model` and `greater_is_better` appropriately. For example:

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)],
)** (95% success)
   ```
   Set `load_best_model_at_end=True` in `TrainingArguments` and also set `metric_for_best_model` and `greater_is_better` appropriately. For example:

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. **If you don't need early stopping, remove the `EarlyStoppingCallback` from the callbacks list and rely on manual checkpoint selection.** (90% success)
   ```
   If you don't need early stopping, remove the `EarlyStoppingCallback` from the callbacks list and rely on manual checkpoint selection.
   ```

## Dead Ends

- **** — The default is False, so removing it doesn't change behavior; the error persists. (90% fail)
- **** — This directly contradicts the requirement; early stopping cannot function without loading the best model. (95% fail)
