tensorflow/models · error · ValueError

positive_fraction should be in range [0,1]. Received

Error message

positive_fraction should be in range [0,1]. Received: %s.

What it means

Config guard in BalancedPositiveNegativeSampler.__init__: positive_fraction drives the target positive/negative balance of every subsampled minibatch, so values outside [0, 1] (negative or > 1) are mathematically invalid and rejected at construction time.

Solutions

  1. Set positive_fraction to a value within [0, 1].
  2. Fix the sampler config; typical values are around 0.5.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/vision/utils/object_detection/balanced_positive_negative_sampler.py:55 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/d226c902f2a053c0. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/utils/object_detection/balanced_positive_negative_sampler.py:55

from official.vision.utils.object_detection import ops


class BalancedPositiveNegativeSampler(minibatch_sampler.MinibatchSampler):
  """Subsamples minibatches to a desired balance of positives and negatives."""

  def __init__(self, positive_fraction=0.5, is_static=False):
    """Constructs a minibatch sampler.

    Args:
      positive_fraction: desired fraction of positive examples (scalar in [0,1])
        in the batch.
      is_static: If True, uses an implementation with static shape guarantees.

    Raises:
      ValueError: if positive_fraction < 0, or positive_fraction > 1
    """
    if positive_fraction < 0 or positive_fraction > 1:
      raise ValueError('positive_fraction should be in range [0,1]. '
                       'Received: %s.' % positive_fraction)
    self._positive_fraction = positive_fraction
    self._is_static = is_static

  def _get_num_pos_neg_samples(self, sorted_indices_tensor, sample_size):
    """Counts the number of positives and negatives numbers to be sampled.

    Args:
      sorted_indices_tensor: A sorted int32 tensor of shape [N] which contains
        the signed indices of the examples where the sign is based on the label
        value. The examples that cannot be sampled are set to 0. It samples
        at most sample_size*positive_fraction positive examples and remaining
        from negative examples.
      sample_size: Size of subsamples.

    Returns:
      A tuple containing the number of positive and negative labels in the
      subsample.

View on GitHub (pinned to e006f5f0d5)