Skip to content
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

Prune deprecated EarlyStopping(mode='auto') #6167

Merged
merged 5 commits into from
Feb 24, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Removed


- Removed `mode='auto'` from `EarlyStopping` ([#6167](https://github.com/PyTorchLightning/pytorch-lightning/pull/6167))


### Fixed

- Made the `Plugin.reduce` method more consistent across all Plugins to reflect a mean-reduction by default ([#6011](https://github.com/PyTorchLightning/pytorch-lightning/pull/6011))
37 changes: 7 additions & 30 deletions pytorch_lightning/callbacks/early_stopping.py
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
import torch

from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.utilities import rank_zero_info, rank_zero_warn
from pytorch_lightning.utilities import rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException


@@ -40,23 +40,18 @@ class EarlyStopping(Callback):
patience: number of validation epochs with no improvement
after which training will be stopped. Default: ``3``.
verbose: verbosity mode. Default: ``False``.
mode: one of {auto, min, max}. In `min` mode,
mode: one of {min, max}. In `min` mode,
carmocca marked this conversation as resolved.
Show resolved Hide resolved
training will stop when the quantity
monitored has stopped decreasing; in `max`
carmocca marked this conversation as resolved.
Show resolved Hide resolved
mode it will stop when the quantity
monitored has stopped increasing; in `auto`
mode, the direction is automatically inferred
from the name of the monitored quantity.

.. warning::
Setting ``mode='auto'`` has been deprecated in v1.1 and will be removed in v1.3.
monitored has stopped increasing.
carmocca marked this conversation as resolved.
Show resolved Hide resolved

strict: whether to crash the training if `monitor` is
not found in the validation metrics. Default: ``True``.

Raises:
MisconfigurationException:
If ``mode`` is none of ``"min"``, ``"max"``, and ``"auto"``.
If ``mode`` is none of ``"min"``, ``"max"``.
carmocca marked this conversation as resolved.
Show resolved Hide resolved
RuntimeError:
If the metric ``monitor`` is not available.

@@ -78,7 +73,7 @@ def __init__(
min_delta: float = 0.0,
patience: int = 3,
verbose: bool = False,
mode: str = 'auto',
mode: str = 'min',
strict: bool = True,
):
super().__init__()
@@ -92,31 +87,13 @@ def __init__(
self.mode = mode
self.warned_result_obj = False

self.__init_monitor_mode()
if self.mode not in self.mode_dict:
raise MisconfigurationException(f"`mode` can be {', '.join(self.mode_dict.keys())}, got {self.mode}")

self.min_delta *= 1 if self.monitor_op == torch.gt else -1
torch_inf = torch.tensor(np.Inf)
self.best_score = torch_inf if self.monitor_op == torch.lt else -torch_inf

def __init_monitor_mode(self):
if self.mode not in self.mode_dict and self.mode != 'auto':
raise MisconfigurationException(f"`mode` can be auto, {', '.join(self.mode_dict.keys())}, got {self.mode}")

# TODO: Update with MisconfigurationException when auto mode is removed in v1.3
if self.mode == 'auto':
rank_zero_warn(
"mode='auto' is deprecated in v1.1 and will be removed in v1.3."
" Default value for mode with be 'min' in v1.3.", DeprecationWarning
)

if "acc" in self.monitor or self.monitor.startswith("fmeasure"):
self.mode = 'max'
else:
self.mode = 'min'

if self.verbose > 0:
rank_zero_info(f'EarlyStopping mode set to {self.mode} for monitoring {self.monitor}.')

def _validate_condition_metric(self, logs):
monitor_val = logs.get(self.monitor)

3 changes: 0 additions & 3 deletions tests/deprecated_api/test_remove_1-3.py
Original file line number Diff line number Diff line change
@@ -36,9 +36,6 @@ def test_v1_3_0_deprecated_arguments(tmpdir):
with pytest.deprecated_call(match='will be removed in v1.3'):
ModelCheckpoint(mode='auto')

with pytest.deprecated_call(match='will be removed in v1.3'):
EarlyStopping(mode='auto')

with pytest.deprecated_call(match="The setter for self.hparams in LightningModule is deprecated"):

class DeprecatedHparamsModel(LightningModule):