Record the values in val_dict Args: val_dict: a dictionary containing the floats to compute statistics.
(
self,
val_dict: T.Dict[str, float],
)
| 562 | self.x_count_dict = dict() |
| 563 | |
| 564 | def record( |
| 565 | self, |
| 566 | val_dict: T.Dict[str, float], |
| 567 | ): |
| 568 | """ |
| 569 | Record the values in val_dict |
| 570 | |
| 571 | Args: |
| 572 | val_dict: |
| 573 | a dictionary containing the floats to compute statistics. |
| 574 | """ |
| 575 | |
| 576 | for key, val in val_dict.items(): |
| 577 | if isinstance(val, torch.Tensor) and val.numel() > 1: |
| 578 | continue |
| 579 | |
| 580 | if self.convert_to_float: |
| 581 | if isinstance(val, torch.Tensor): |
| 582 | val = val.detach().cpu().item() |
| 583 | elif isinstance(val, np.ndarray): |
| 584 | val = val.item() |
| 585 | elif isinstance(val, int): |
| 586 | val = float(val) |
| 587 | elif isinstance(val, float): |
| 588 | pass |
| 589 | else: |
| 590 | raise NotImplementedError(f"{type(val)}") |
| 591 | |
| 592 | if key not in self.x_sum_dict: |
| 593 | self.x_sum_dict[key] = val |
| 594 | self.x2_sum_dict[key] = val ** 2 |
| 595 | self.x_count_dict[key] = 1 |
| 596 | else: |
| 597 | self.x_sum_dict[key] = self.x_sum_dict[key] + val |
| 598 | self.x2_sum_dict[key] = self.x2_sum_dict[key] + val ** 2 |
| 599 | self.x_count_dict[key] = self.x_count_dict[key] + 1 |
| 600 | |
| 601 | def compute_statistics(self) -> T.Dict[str, T.Dict[str, float]]: |
| 602 | """Compute the statistics. |
no test coverage detected