|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import Union |
| 3 | +from torch import Tensor |
| 4 | +from lpd.enums import MonitorMode |
| 5 | + |
| 6 | + |
| 7 | +class ThresholdChecker(ABC): |
| 8 | + """ |
| 9 | + Check if the current value is better than the previous best value according to different threshold criteria |
| 10 | + This is an abstract class meant to be inherited by different threshold checkers |
| 11 | + Can also be inherited by the user to create custom threshold checkers |
| 12 | + """ |
| 13 | + def __init__(self, monitor_mode: MonitorMode, threshold: float): |
| 14 | + self.monitor_mode = monitor_mode |
| 15 | + self.threshold = threshold |
| 16 | + |
| 17 | + def validate_input(self): |
| 18 | + if self.threshold < 0: |
| 19 | + raise ValueError(f"Threshold must be non-negative, but got {self.threshold}") |
| 20 | + |
| 21 | + @abstractmethod |
| 22 | + def __call__(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +class AbsoluteThresholdChecker(ThresholdChecker): |
| 27 | + """ |
| 28 | + A threshold checker that checks if the difference between the current value and the previous best value |
| 29 | + is greater than or equal to a given threshold |
| 30 | +
|
| 31 | + Args: |
| 32 | + monitor_mode: MIN or MAX |
| 33 | + threshold - the threshold to check (must be non-negative) |
| 34 | + """ |
| 35 | + def __init__(self, monitor_mode: MonitorMode, threshold: float = 0.0): |
| 36 | + super(AbsoluteThresholdChecker, self).__init__(monitor_mode, threshold) |
| 37 | + |
| 38 | + def _is_new_value_lower(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 39 | + return old_value - new_value > self.threshold |
| 40 | + |
| 41 | + def _is_new_value_higher(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 42 | + return new_value - old_value > self.threshold |
| 43 | + |
| 44 | + def __call__(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 45 | + if self.monitor_mode == MonitorMode.MIN: |
| 46 | + return self._is_new_value_lower(new_value, old_value) |
| 47 | + if self.monitor_mode == MonitorMode.MAX: |
| 48 | + return self._is_new_value_higher(new_value, old_value) |
| 49 | + |
| 50 | + |
| 51 | +class RelativeThresholdChecker(ThresholdChecker): |
| 52 | + """ |
| 53 | + A threshold checker that checks if the relative difference between the current value and the previous best value |
| 54 | + is greater than or equal to a given threshold |
| 55 | +
|
| 56 | + Args: |
| 57 | + threshold - the threshold to check (must be non-negative) |
| 58 | + """ |
| 59 | + def __init__(self, monitor_mode: MonitorMode, threshold: float = 0.0): |
| 60 | + super(RelativeThresholdChecker, self).__init__(monitor_mode, threshold) |
| 61 | + |
| 62 | + def _is_new_value_lower(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 63 | + return (old_value - new_value) / old_value > self.threshold |
| 64 | + |
| 65 | + def _is_new_value_higher(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 66 | + return (new_value - old_value) / old_value > self.threshold |
| 67 | + |
| 68 | + def __call__(self, new_value: Union[float, Tensor], old_value: Union[float, Tensor]) -> bool: |
| 69 | + if self.monitor_mode == MonitorMode.MIN: |
| 70 | + return self._is_new_value_lower(new_value, old_value) |
| 71 | + if self.monitor_mode == MonitorMode.MAX: |
| 72 | + return self._is_new_value_higher(new_value, old_value) |
0 commit comments