-
-
Notifications
You must be signed in to change notification settings - Fork 712
feat(early_stopping): add get_default_score_fn and get_default_event_filter helpers (#3411) #3745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MukundaKatta
wants to merge
1
commit into
pytorch:master
Choose a base branch
from
MukundaKatta:feat/early-stopping-enhancements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ def score_function(engine): | |
| Renamed ``min_delta_mode`` to ``threshold_mode``. | ||
| Renamed ``min_delta`` to ``threshold``. | ||
| Renamed ``cumulative_delta`` to ``cumulative``. | ||
| Added :meth:`get_default_score_fn` and :meth:`get_default_event_filter` static helpers. | ||
| .. versionchanged:: 0.5.4 | ||
| Added `mode` parameter to support minimization in addition to maximization. | ||
| Added `min_delta_mode` parameter to support both absolute and relative improvements. | ||
|
|
@@ -291,3 +292,90 @@ def load_state_dict(self, state_dict: Mapping) -> None: | |
| self.counter = state_dict["counter"] | ||
| self.best_score = state_dict["best_score"] | ||
| self.threshold_mode = state_dict.get("threshold_mode", self.threshold_mode) | ||
|
|
||
| @staticmethod | ||
| def get_default_score_fn(metric_name: str, score_sign: float = 1.0) -> Callable: | ||
| """Helper method to build a score function from an engine metric name. | ||
|
|
||
| The returned callable reads ``engine.state.metrics[metric_name]`` and multiplies it | ||
| by ``score_sign``. Use ``score_sign=-1.0`` for error-like metrics (smaller is better) | ||
| when the handler is configured with ``mode="max"`` so that decreases in the metric | ||
| register as score improvements. | ||
|
|
||
| Args: | ||
| metric_name: name of the metric in ``engine.state.metrics``. | ||
| score_sign: ``1.0`` (default) or ``-1.0``. For error-like metrics where smaller | ||
| is better, use ``-1.0``. | ||
|
|
||
| Returns: | ||
| A callable taking an :class:`~ignite.engine.engine.Engine` and returning ``float``. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| from ignite.handlers import EarlyStopping | ||
|
|
||
| # Validation accuracy: larger is better, default mode="max" | ||
| score_fn = EarlyStopping.get_default_score_fn("accuracy") | ||
| handler = EarlyStopping(patience=5, score_function=score_fn, trainer=trainer) | ||
|
|
||
| # Validation loss: smaller is better, flip the sign so larger is better | ||
| neg_loss_fn = EarlyStopping.get_default_score_fn("loss", -1.0) | ||
| handler = EarlyStopping(patience=5, score_function=neg_loss_fn, trainer=trainer) | ||
|
|
||
| .. versionadded:: 0.6.0 | ||
| """ | ||
| if score_sign not in (1.0, -1.0): | ||
| raise ValueError("Argument score_sign should be 1.0 or -1.0") | ||
|
|
||
| def wrapper(engine: Engine) -> float: | ||
| return score_sign * engine.state.metrics[metric_name] | ||
|
|
||
| return wrapper | ||
|
|
||
| def get_default_event_filter(self, after: int) -> Callable[[Engine, int], bool]: | ||
| """Build an event filter that delays early-stopping checks until the trainer | ||
| has completed at least ``after`` epochs. | ||
|
|
||
| This implements a warmup window for early stopping without coupling the warmup | ||
| logic to the handler itself, so it composes with any event the handler is | ||
| attached to (epoch, iteration, custom). The filter consults the trainer's | ||
| epoch counter (``self.trainer.state.epoch``) rather than the host engine's | ||
| event count, so the warmup is well-defined even when the handler is attached | ||
| to an evaluator that re-runs from scratch each epoch. | ||
|
|
||
| Args: | ||
| after: minimum number of trainer epochs that must have completed before | ||
| the early-stopping handler is allowed to run. Must be a non-negative | ||
| integer. | ||
|
|
||
| Returns: | ||
| A callable matching the ``event_filter`` signature ``(engine, event) -> bool``. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| from ignite.engine import Events | ||
| from ignite.handlers import EarlyStopping | ||
|
|
||
| handler = EarlyStopping(patience=5, score_function=score_fn, trainer=trainer) | ||
|
|
||
| # Skip the first 3 trainer epochs, then enforce early stopping. | ||
| evaluator.add_event_handler( | ||
| Events.COMPLETED(event_filter=handler.get_default_event_filter(after=3)), | ||
| handler, | ||
| ) | ||
|
|
||
| .. versionadded:: 0.6.0 | ||
| """ | ||
| if not isinstance(after, int) or after < 0: | ||
| raise ValueError("Argument after should be a non-negative integer.") | ||
|
|
||
| trainer = self.trainer | ||
|
|
||
| def event_filter(engine: Engine, event: int) -> bool: | ||
| # The host engine's `event` counter resets per `run()` (e.g. evaluators | ||
| # called repeatedly), so look at the trainer's epoch counter instead. | ||
| return trainer.state.epoch > after | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could add support for iteration too, so user can specify minimum iterations allowed |
||
|
|
||
| return event_filter | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats the use of this in early stopping?