tensorflow/models · error · ValueError

n must be 'None' or a positive integer. It was

Error message

n must be 'None' or a positive integer. It was '%s'.

What it means

Parameter check inside ap_at_n (reached via peek_ap_at_n): the n limiting the ranked list for AP@n must be None or a positive integer. Values like 0, -1, floats, or strings cannot meaningfully index or slice the ranked predictions, so the calculation aborts before ranking. Distinct from the predictions/actuals shape check that runs just before it in the same function.

Solutions

  1. Pass n as a positive int (e.g. 20) or None
  2. Coerce config-sourced values with int(n) after validating they are numeric
  3. Guard user or flag inputs to peek_ap_at_n before calling
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/yt8m/eval_utils/average_precision_calculator.py:209 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/fd437204af19d027. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/yt8m/eval_utils/average_precision_calculator.py:209

        positive in the list. If specified, it will be used in calculation.

    Returns:
      The non-interpolated average precision at n.
      If n is larger than the length of the ranked list,
      the average precision will be returned.

    Raises:
      ValueError: An error occurred when
      1) the format of the input is not the numpy 1-D array;
      2) the shape of predictions and actuals does not match;
      3) the input n is not a positive integer.
    """
    if len(predictions) != len(actuals):
      raise ValueError("the shape of predictions and actuals does not match.")

    if n is not None:
      if not isinstance(n, int) or n <= 0:
        raise ValueError("n must be 'None' or a positive integer."
                         " It was '%s'." % n)

    ap = 0.0

    predictions = numpy.array(predictions)
    actuals = numpy.array(actuals)

    # add a shuffler to avoid overestimating the ap
    predictions, actuals = AveragePrecisionCalculator._shuffle(
        predictions, actuals)
    sortidx = sorted(
        range(len(predictions)), key=lambda k: predictions[k], reverse=True)

    if total_num_positives is None:
      numpos = numpy.size(numpy.where(actuals > 0))
    else:
      numpos = total_num_positives

View on GitHub (pinned to e006f5f0d5)