-
-
Notifications
You must be signed in to change notification settings - Fork 619
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
add NoImprovementHandler #2574
Open
Ishan-Kumar2
wants to merge
5
commits into
pytorch:master
Choose a base branch
from
Ishan-Kumar2:no_improv_handler
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.
Open
add NoImprovementHandler #2574
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eabb372
added NoImprovementHandler in EarlyStopping
Ishan-Kumar2 0504071
fixed mypy error
Ishan-Kumar2 b40b2f1
added tests for NoImprovementHandler
Ishan-Kumar2 256df7d
added logger for parent class
Ishan-Kumar2 b51e4a5
Merge branch 'master' into no_improv_handler
vfdev-5 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 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
This file contains 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 |
---|---|---|
|
@@ -5,36 +5,49 @@ | |
from ignite.engine import Engine | ||
from ignite.utils import setup_logger | ||
|
||
__all__ = ["EarlyStopping"] | ||
__all__ = ["NoImprovementHandler", "EarlyStopping"] | ||
|
||
|
||
class EarlyStopping(Serializable): | ||
"""EarlyStopping handler can be used to stop the training if no improvement after a given number of events. | ||
|
||
class NoImprovementHandler(Serializable): | ||
"""NoImprovementHandler is a generalised version of Early stopping where you can define what should | ||
happen if no improvement occurs after a given number of events. | ||
Args: | ||
patience: Number of events to wait if no improvement and then stop the training. | ||
patience: Number of events to wait if no improvement and then call stop_function. | ||
score_function: It should be a function taking a single argument, an :class:`~ignite.engine.engine.Engine` | ||
object, and return a score `float`. An improvement is considered if the score is higher. | ||
pass_function: It should be a function taking a single argument, the trainer | ||
object, and defines what to do in the case when the stopping condition is not met. | ||
stop_function: It should be a function taking a single argument, the trainer | ||
object, and defines what to do in the case when the stopping condition is met. | ||
trainer: Trainer engine to stop the run if no improvement. | ||
min_delta: A minimum increase in the score to qualify as an improvement, | ||
i.e. an increase of less than or equal to `min_delta`, will count as no improvement. | ||
cumulative_delta: It True, `min_delta` defines an increase since the last `patience` reset, otherwise, | ||
it defines an increase after the last event. Default value is False. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
from ignite.engine import Engine, Events | ||
from ignite.handlers import EarlyStopping | ||
#Example where if the score doesn't improve a user defined value `alpha` is doubled. | ||
|
||
from ignite.engine import Engine, Events | ||
from ignite.handlers import NoImprovementHandler | ||
def score_function(engine): | ||
val_loss = engine.state.metrics['nll'] | ||
return -val_loss | ||
def pass_function(engine): | ||
pass | ||
def stop_function(trainer): | ||
trainer.state.alpha *= 2 | ||
|
||
trainer = Engine(do_nothing_update_fn) | ||
trainer.state_dict_user_keys.append("alpha") | ||
trainer.state.alpha = 0.1 | ||
|
||
h = NoImprovementHandler(patience=3, score_function=score_function, pass_function=pass_function, | ||
stop_function=stop_function, trainer=trainer) | ||
|
||
handler = EarlyStopping(patience=10, score_function=score_function, trainer=trainer) | ||
# Note: the handler is attached to an *Evaluator* (runs one epoch on validation dataset). | ||
evaluator.add_event_handler(Events.COMPLETED, handler) | ||
|
||
""" | ||
|
||
_state_dict_all_req_keys = ( | ||
|
@@ -46,45 +59,61 @@ def __init__( | |
self, | ||
patience: int, | ||
score_function: Callable, | ||
stop_function: Callable, | ||
trainer: Engine, | ||
pass_function: Callable = lambda engine: 0, | ||
min_delta: float = 0.0, | ||
cumulative_delta: bool = False, | ||
): | ||
|
||
if not callable(score_function): | ||
raise TypeError("Argument score_function should be a function.") | ||
|
||
if not callable(pass_function): | ||
raise TypeError("Argument pass_function should be a function.") | ||
|
||
if not callable(stop_function): | ||
raise TypeError("Argument stop_function should be a function.") | ||
|
||
if not isinstance(trainer, Engine): | ||
raise TypeError("Argument trainer should be an instance of Engine.") | ||
|
||
if patience < 1: | ||
raise ValueError("Argument patience should be positive integer.") | ||
|
||
if min_delta < 0.0: | ||
raise ValueError("Argument min_delta should not be a negative number.") | ||
|
||
if not isinstance(trainer, Engine): | ||
raise TypeError("Argument trainer should be an instance of Engine.") | ||
|
||
self.score_function = score_function | ||
self.patience = patience | ||
self.min_delta = min_delta | ||
self.cumulative_delta = cumulative_delta | ||
self.score_function = score_function | ||
self.pass_function = pass_function | ||
self.stop_function = stop_function | ||
self.trainer = trainer | ||
self.counter = 0 | ||
self.best_score = None # type: Optional[float] | ||
self.min_delta = min_delta | ||
self.cumulative_delta = cumulative_delta | ||
self.logger = setup_logger(__name__ + "." + self.__class__.__name__) | ||
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. We may want also the logger for |
||
|
||
def __call__(self, engine: Engine) -> None: | ||
score = self.score_function(engine) | ||
|
||
self._update_state(score) | ||
sdesrozis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.counter >= self.patience: | ||
self.stop_function(self.trainer) | ||
else: | ||
self.pass_function(self.trainer) | ||
|
||
def _update_state(self, score: int) -> None: | ||
|
||
if self.best_score is None: | ||
self.best_score = score | ||
|
||
elif score <= self.best_score + self.min_delta: | ||
if not self.cumulative_delta and score > self.best_score: | ||
self.best_score = score | ||
self.counter += 1 | ||
self.logger.debug("EarlyStopping: %i / %i" % (self.counter, self.patience)) | ||
if self.counter >= self.patience: | ||
self.logger.info("EarlyStopping: Stop training") | ||
self.trainer.terminate() | ||
|
||
else: | ||
self.best_score = score | ||
self.counter = 0 | ||
|
@@ -104,3 +133,62 @@ def load_state_dict(self, state_dict: Mapping) -> None: | |
super().load_state_dict(state_dict) | ||
self.counter = state_dict["counter"] | ||
self.best_score = state_dict["best_score"] | ||
|
||
|
||
class EarlyStopping(NoImprovementHandler): | ||
"""EarlyStopping handler can be used to stop the training if no improvement after a given number of events. | ||
Args: | ||
patience: Number of events to wait if no improvement and then stop the training. | ||
score_function: It should be a function taking a single argument, an :class:`~ignite.engine.engine.Engine` | ||
object, and return a score `float`. An improvement is considered if the score is higher. | ||
trainer: Trainer engine to stop the run if no improvement. | ||
min_delta: A minimum increase in the score to qualify as an improvement, | ||
i.e. an increase of less than or equal to `min_delta`, will count as no improvement. | ||
cumulative_delta: It True, `min_delta` defines an increase since the last `patience` reset, otherwise, | ||
it defines an increase after the last event. Default value is False. | ||
Examples: | ||
.. code-block:: python | ||
from ignite.engine import Engine, Events | ||
from ignite.handlers import EarlyStopping | ||
def score_function(engine): | ||
val_loss = engine.state.metrics['nll'] | ||
return -val_loss | ||
handler = EarlyStopping(patience=10, score_function=score_function, trainer=trainer) | ||
# Note: the handler is attached to an *Evaluator* (runs one epoch on validation dataset). | ||
evaluator.add_event_handler(Events.COMPLETED, handler) | ||
""" | ||
|
||
_state_dict_all_req_keys = ( | ||
"counter", | ||
"best_score", | ||
) | ||
|
||
def __init__( | ||
self, | ||
patience: int, | ||
score_function: Callable, | ||
trainer: Engine, | ||
min_delta: float = 0.0, | ||
cumulative_delta: bool = False, | ||
): | ||
super(EarlyStopping, self).__init__( | ||
patience=patience, | ||
score_function=score_function, | ||
pass_function=self.pass_function, | ||
stop_function=self.stop_function, | ||
trainer=trainer, | ||
min_delta=min_delta, | ||
cumulative_delta=cumulative_delta, | ||
) | ||
|
||
self.logger = setup_logger(__name__ + "." + self.__class__.__name__) | ||
|
||
def __call__(self, engine: Engine) -> None: | ||
super(EarlyStopping, self).__call__(engine) | ||
|
||
def pass_function(self, trainer: Engine) -> None: | ||
pass | ||
|
||
def stop_function(self, trainer: Engine) -> None: | ||
self.logger.info("EarlyStopping: Stop training") | ||
trainer.terminate() |
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.
If we want to save NoImprovementHandler's internal state we have to keep
_state_dict_all_req_keys
, IMO