这句代码什么意思?
self._best_score: Optional[float] = None
import datetime
import enum
import inspect
import secrets
import time
from contextlib import ContextDecorator
from typing import Any, Dict, Optional
import torch
import torch.nn as nn
from . import random as delu_random
class _ProgressStatus(enum.Enum):
NEUTRAL = enum.auto()
SUCCESS = enum.auto()
FAIL = enum.auto()
class ProgressTracker:
"""Tracks the best score, helps with early stopping.
For `~ProgressTracker`, **the greater score is the better score**.
At any moment the tracker is in one of the following states:
- *success*: the last update changed the best score
- *fail*: last :code:`n > patience` updates are not better than the best score
- *neutral*: if neither success nor fail
.. rubric:: Tutorial
.. testcode::
progress = ProgressTracker(2)
progress.update(-999999999)
assert progress.success # the first update always updates the best score
progress.update(123)
assert progress.success
assert progress.best_score == 123
progress.update(0)
assert not progress.success and not progress.fail
progress.update(123)
assert not progress.success and not progress.fail
progress.update(123)
# patience is 2 and the best score is not updated for more than 2 steps
assert progress.fail
assert progress.best_score == 123 # fail doesn't affect the best score
progress.update(123)
assert progress.fail # still no improvements
progress.forget_bad_updates()
assert not progress.fail and not progress.success
assert progress.best_score == 123
progress.update(0)
assert not progress.fail # just 1 bad update (the patience is 2)
progress.reset()
assert not progress.fail and not progress.success
assert progress.best_score is None
"""
def __init__(self, patience: Optional[int], min_delta: float = 0.0) -> None:
"""Initialize self.
Args:
patience: Allowed number of bad updates. For example, if patience is 2, then
2 bad updates is not a fail, but 3 bad updates is a fail. If `None`,
then the progress tracker never fails.
min_delta: minimal improvement over current best score to count it as
success.
Examples:
.. testcode::
progress = ProgressTracker(2)
progress = ProgressTracker(3, 0.1)
"""
self._patience = patience
self._min_delta = float(min_delta)
self._best_score: Optional[float] = None
self._status = _ProgressStatus.NEUTRAL
self._bad_counter = 0
@property
def best_score(self) -> Optional[float]:
"""The best score so far.
If the tracker is just created/reset, return `None`.
"""
return self._best_score