| | |
| | | import humanfriendly |
| | | import numpy as np |
| | | import torch |
| | | from typeguard import check_argument_types |
| | | from typeguard import check_return_type |
| | | |
| | | Num = Union[float, int, complex, torch.Tensor, np.ndarray] |
| | | |
| | |
| | | |
| | | |
| | | def to_reported_value(v: Num, weight: Num = None) -> "ReportedValue": |
| | | assert check_argument_types() |
| | | if isinstance(v, (torch.Tensor, np.ndarray)): |
| | | if np.prod(v.shape) != 1: |
| | | raise ValueError(f"v must be 0 or 1 dimension: {len(v.shape)}") |
| | |
| | | retval = WeightedAverage(v, weight) |
| | | else: |
| | | retval = Average(v) |
| | | assert check_return_type(retval) |
| | | return retval |
| | | |
| | | |
| | | def aggregate(values: Sequence["ReportedValue"]) -> Num: |
| | | assert check_argument_types() |
| | | |
| | | for v in values: |
| | | if not isinstance(v, type(values[0])): |
| | |
| | | |
| | | else: |
| | | raise NotImplementedError(f"type={type(values[0])}") |
| | | assert check_return_type(retval) |
| | | return retval |
| | | |
| | | |
| | |
| | | """ |
| | | |
| | | def __init__(self, key: str, epoch: int, total_count: int): |
| | | assert check_argument_types() |
| | | self.key = key |
| | | self.epoch = epoch |
| | | self.start_time = time.perf_counter() |
| | |
| | | stats: Dict[str, Optional[Union[Num, Dict[str, Num]]]], |
| | | weight: Num = None, |
| | | ) -> None: |
| | | assert check_argument_types() |
| | | if self._finished: |
| | | raise RuntimeError("Already finished") |
| | | if len(self._seen_keys_in_the_step) == 0: |
| | |
| | | """ |
| | | |
| | | def __init__(self, epoch: int = 0): |
| | | assert check_argument_types() |
| | | if epoch < 0: |
| | | raise ValueError(f"epoch must be 0 or more: {epoch}") |
| | | self.epoch = epoch |