Skip to content

Commit

Permalink
Merge pull request #1595 from MattiaFanan/fix-EarlyStoppingPlugin
Browse files Browse the repository at this point in the history
Fix early stopping plugin
  • Loading branch information
AntonioCarta authored Feb 15, 2024
2 parents cbe1307 + 7dc09dc commit 071d813
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions avalanche/training/plugins/early_stopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ def _update_best(self, strategy):
f"Metric {self.metric_name} used by the EarlyStopping plugin "
f"is not computed yet. EarlyStopping will not be triggered."
)
if self.best_val is None or self.operator(val_acc, self.best_val):

if self.best_val is None:
self.best_state = deepcopy(strategy.model.state_dict())
self.best_val = val_acc
self.best_step = self._get_strategy_counter(strategy)
return None

delta_val = float(val_acc - self.best_val)
if self.operator(delta_val, 0) and abs(delta_val) >= self.margin:
self.best_state = deepcopy(strategy.model.state_dict())
if self.best_val is None:
self.best_val = val_acc
self.best_step = 0
return None

if self.operator(float(val_acc - self.best_val), self.margin):
self.best_step = self._get_strategy_counter(strategy)
self.best_val = val_acc
if self.verbose:
print("EarlyStopping: new best value:", val_acc)
self.best_val = val_acc
self.best_step = self._get_strategy_counter(strategy)
if self.verbose:
print("EarlyStopping: new best value:", val_acc)

return self.best_val

Expand Down

0 comments on commit 071d813

Please sign in to comment.