question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

state.best_metric does not update in EarlyStoppingCallback

See original GitHub issue

https://github.com/huggingface/transformers/blob/b18dfd95e1f60ae65a959a7b255fc06522170d1b/src/transformers/trainer_callback.py#L534

I print the state.best_metric, but it is always None. I wonder whether the state.best_metric should be updated if the condition is satisfied, like the following:

if state.best_metric is None or (
            operator(metric_value, state.best_metric)
            and abs(metric_value - state.best_metric) > self.early_stopping_threshold
        ):
            self.early_stopping_patience_counter = 0
            state.best_metric = metric_value

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
baskrahmercommented, Jun 24, 2022

Cool, I’ll be working on one!

0reactions
dataleecommented, Jul 21, 2022

I have set load_best_model_at_end to True, but it doesn’t work. The following is my code:

args_list=[...]
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, Seq2SeqTrainingArguments))
model_args, data_args, training_args = parser.parse_args_into_dataclasses(args_list)
training_args.load_best_model_at_end = True
training_args.metric_for_best_model = 'eval_rouge-l'
training_args.greater_is_better = True

However, when I add control.should_save in

https://github.com/huggingface/transformers/blob/b18dfd95e1f60ae65a959a7b255fc06522170d1b/src/transformers/trainer_callback.py#L531

it works. Just like this:

def check_metric_value(self, args, state, control, metric_value):
      # best_metric is set by code for load_best_model
      operator = np.greater if args.greater_is_better else np.less
      if state.best_metric is None or (
          operator(metric_value, state.best_metric)
          and abs(metric_value - state.best_metric) > self.early_stopping_threshold
      ):
          self.early_stopping_patience_counter = 0
          control.should_save = True
      else:
          self.early_stopping_patience_counter += 1
          control.should_save = False

not work for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Callbacks - Hugging Face
Callbacks are objects that can customize the behavior of the training loop in the PyTorch Trainer (this feature is not yet implemented in...
Read more >
Callbacks — Catalyst 20.12 documentation
minimize_metric – indicator for selecting best metric, if true then best metric will be the metric with the lowest value, otherwise with the...
Read more >
Keras early stopping callback error, val_loss metric not available
The error message appears to relate to the early stopping callback but the callback looks OK. Also the error states that the val_loss...
Read more >
Callbacks — pytorch-accelerated 0.1.3 documentation
This is done to support the pattern of updating the trainer's state in a method ... A callback which stops training early if...
Read more >
Tracking callbacks - fastai
EarlyStoppingCallback (monitor='valid_loss', comp=None, min_delta=0.0, ... patience, int, 1, number of epochs to wait when training has not improved model.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found