tensorflow/models · error · ValueError

total_sample must be positive.

Error message

total_sample must be positive.

What it means

Aggregation guard in the metrics accumulator's get(): total_sample (the count of accumulated examples) is zero or negative, so per-example means cannot be computed. This means get() was called before any examples were accumulated — an eval loop that never fed predictions, an empty dataset shard or glob mismatch, or a second get() call after the first already flushed the counters.

Solutions

  1. Ensure accumulate(...) runs with at least one batch before calling get()
  2. Check the input pipeline actually yields examples (empty shard or file-glob mismatch)
  3. Do not call get() twice without accumulating in between
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/yt8m/eval_utils/eval_util.py:237 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/f82c6480a220d3d6. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/yt8m/eval_utils/eval_util.py:237

    return {"hit_at_one": mean_hit_at_one, "perr": mean_perr}

  def get(self, return_per_class_ap=False):
    """Calculate the evaluation metrics for the whole epoch.

    Args:
      return_per_class_ap: a bool variable to determine whether return the
        detailed class-wise ap for more detailed analysis. Default is `False`.

    Raises:
      ValueError: If no examples were accumulated.

    Returns:
      dictionary: a dictionary storing the evaluation metrics for the epoch. The
        dictionary has the fields: avg_hit_at_one, avg_perr, and
        aps (default nan).
    """
    if self.num_examples <= 0:
      raise ValueError("total_sample must be positive.")
    avg_hit_at_one = self.sum_hit_at_one / self.num_examples
    avg_perr = self.sum_perr / self.num_examples

    aps = self.map_calculator.peek_map_at_n()
    mean_ap = sum(aps) / self.num_class
    gap = self.global_ap_calculator.peek_ap_at_n()
    lw_map = self.map_calculator.peek_log_weighted_map_at_n()

    epoch_info_dict = {
        "avg_hit_at_one": avg_hit_at_one,
        "avg_perr": avg_perr,
        "map": mean_ap,
        "gap": gap,
        "lw_map": lw_map
    }

    if return_per_class_ap:
      epoch_info_dict["per_class_ap"] = aps

View on GitHub (pinned to e006f5f0d5)