MCPcopy Create free account
hub / github.com/apple/ml-pointersect / record

Method record

cdslib/core/utils/print_and_save.py:564–599  ·  view source on GitHub ↗

Record the values in val_dict Args: val_dict: a dictionary containing the floats to compute statistics.

(
            self,
            val_dict: T.Dict[str, float],
    )

Source from the content-addressed store, hash-verified

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.

Callers 1

_loop_dataloaderMethod · 0.95

Calls 1

detachMethod · 0.45

Tested by

no test coverage detected