class_docstr: Monitor a metric and signal if execution should stop early.
  
  Args:
      mode: one of `min`, `max`. In `min` mode, signal to stop early will be given when
          the metric has stopped decreasing. In `max` mode, the signal is given when the
          metric has stopped increasing.
      patience: Number of checks without improvement after which early stop will be signaled.
      min_delta: Must be >= 0. Minimum absolute or relative change in the metric to qualify as
          an improvement. In `rel` mode, improvement_threshold = best_val * ( 1 + min_delta ) in 'max'
          mode or best_val * ( 1 - min_delta ) in `min` mode. In `abs` mode, improvement_threshold =
          best_val +  min_delta in `max` mode or best_val - threshold in `min` mode.
      check_finite: When set to `True`, signals early stop when metric becomes NaN or infinite.
      threshold_mode: one of `abs` or `rel`, threshold delta between checks for determining whether to stop.
      stopping_threshold: Signals early stop once the metric improves beyond this threshold.
      divergence_threshold: Signals early stop once the metric becomes worse than this threshold.
  
   Raises:
      ValueError:
          If `mode` is not `min` or `max`.
      ValueError:
          If `min_delta` < 0.
      ValueError:
          If `threshold_mode` is not `abs` or `rel`.
class_docstr_tok: ["Monitor","a","metric","and","signal","if","execution","should","stop","early",".","Args",":","mode",":","one","of","`","min","`",",","`","max","`",".","In","`","min","`","mode",",","signal","to","stop","early","will","be","given","when","the","metric","has","stopped","decreasing",".","In","`","max","`","mode",",","the","signal","is","given","when","the","metric","has","stopped","increasing",".","patience",":","Number","of","checks","without","improvement","after","which","early","stop","will","be","signaled",".","min_delta",":","Must","be",">","=","0",".","Minimum","absolute","or","relative","change","in","the","metric","to","qualify","as","an","improvement",".","In","`","rel","`","mode",",","improvement_threshold","=","best_val","*","(","1","+","min_delta",")","in","'max'","mode","or","best_val","*","(","1","-","min_delta",")","in","`","min","`","mode",".","In","`","abs","`","mode",",","improvement_threshold","=","best_val","+","min_delta","in","`","max","`","mode","or","best_val","-","threshold","in","`","min","`","mode",".","check_finite",":","When","set","to","`","True","`",",","signals","early","stop","when","metric","becomes","NaN","or","infinite",".","threshold_mode",":","one","of","`","abs","`","or","`","rel","`",",","threshold","delta","between","checks","for","determining","whether","to","stop",".","stopping_threshold",":","Signals","early","stop","once","the","metric","improves","beyond","this","threshold",".","divergence_threshold",":","Signals","early","stop","once","the","metric","becomes","worse","than","this","threshold",".","Raises",":","ValueError",":","If","`","mode","`","is","not","`","min","`","or","`","max","`",".","ValueError",":","If","`","min_delta","`","<","0",".","ValueError",":","If","`","threshold_mode","`","is","not","`","abs","`","or","`","rel","`","."]
docstr: Check the current value of a metric and determine whether to stop or not.
  
  Args:
      val: The metric that will be monitored to signal an early stop.
          This should be either a single element tensor or a float.
  
  Returns:
      A boolean indicating whether execution should stop early or not.
  
  Raises:
      ValueError:
          If `val` is a tensor that does not contain 1 element.
docstr_tok: ["Check","the","current","value","of","a","metric","and","determine","whether","to","stop","or","not",".","Args",":","val",":","The","metric","that","will","be","monitored","to","signal","an","early","stop",".","This","should","be","either","a","single","element","tensor","or","a","float",".","Returns",":","A","boolean","indicating","whether","execution","should","stop","early","or","not",".","Raises",":","ValueError",":","If","`","val","`","is","a","tensor","that","does","not","contain","1","element","."]
code: def check(self, val: Union[torch.Tensor, float]) -> bool:
      """
      Check the current value of a metric and determine whether to stop or not.
  
      Args:
          val: The metric that will be monitored to signal an early stop.
              This should be either a single element tensor or a float.
  
      Returns:
          A boolean indicating whether execution should stop early or not.
  
      Raises:
          ValueError:
              If `val` is a tensor that does not contain 1 element.
      """
      if type(val) is float:
          val = torch.tensor([val])
      if val.numel() != 1:
          raise ValueError(
              f"Expected tensor with only 1 element, but input has number of elements = {val.numel()}"
          )
  
      should_stop = False
      message = "No stopping conditions were satisfied."
  
      stopping_threshold = self.stopping_threshold
      if stopping_threshold:
          stopping_threshold = stopping_threshold.to(val.device)
      divergence_threshold = self.divergence_threshold
      if divergence_threshold:
          divergence_threshold = divergence_threshold.to(val.device)
      improvement_threshold = self.min_delta
      if self._threshold_mode == "rel":
          base_val = (
              self._best_value
              if torch.isfinite(self._best_value)
              else 0.0
          )
          improvement_threshold = self.min_delta * base_val
  
      improvement_threshold = improvement_threshold.to(val.device)
  
      # Check finite
      if self.check_finite and not torch.isfinite(val):
          _log.debug(
              f"Metric is not finite: {val}."
              f" Previous best value was {self._best_value}."
          )
          return True
  
      # Check if reached stopping threshold
      if stopping_threshold is not None and self._mode_func(
          val, stopping_threshold
      ):
          _log.debug(
              "Stopping threshold reached:"
              f" {val} {self._mode_char} {stopping_threshold}."
          )
          return True
  
      # Check if exceeding divergence threshold
      if divergence_threshold is not None and self._mode_func(
          -val, -divergence_threshold
      ):
          _log.debug(
              "Divergence threshold reached:"
              f" {val} {self._mode_char} {divergence_threshold}."
          )
          return True
  
      # Check if improvement is happening
      if self._mode_func(
          val - improvement_threshold, self._best_value.to(val.device)
      ):
          # Still improving
          should_stop = False
          message = self._improvement_message(val)
          self._best_value = val
          self._patience_count = 0
      else:
          # Not improving
          self._patience_count += 1
          # Check if patience has run out
          if self._patience_count >= self.patience:
              # Patience has run out
              should_stop = True
              message = (
                  f"Metric did not improve in the last {self._patience_count} checks."
                  f" Best score: {self._best_value}."
              )
          else:
              # Patience still remaining
              should_stop = False
              message = (
                  f"Metric did not improve in the last {self._patience_count} checks."
                  f" {self.patience - self._patience_count} checks of patience remaining."
              )
  
      _log.debug(message)
      return should_stop
  
imports: import logging
  from typing import Any, Callable, Dict, Optional, Union
  import torch
  from typing_extensions import final, Literal
label_desc: Use image node_id 12 for calling the EarlyStopChecker obj's underlying member method code with example usage: obj.check(val) and returns: should_stop, True, True, True