<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/models/blob/v2.20.0/official/vision/ops/sampling_ops.py#L119-L382) |

Subsamples minibatches to a desired balance of positives and negatives.

    tfm.vision.sampling_ops.BalancedPositiveNegativeSampler(
        positive_fraction=0.5, is_static=False
    )

<br />

<br />

<br />

| ## 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. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | if positive_fraction \< 0, or positive_fraction \> 1 |

<br />

## Methods

### `subsample`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/official/vision/ops/sampling_ops.py#L320-L382)

    subsample(
        indicator, batch_size, labels, scope=None
    )

Returns subsampled minibatch.

<br />

<br />

<br />

| Args ||
|---|---|
| `indicator` | boolean tensor of shape \[N\] whose True entries can be sampled. |
| `batch_size` | desired batch size. If None, keeps all positive samples and randomly selects negative samples so that the positive sample fraction matches self._positive_fraction. It cannot be None is is_static is True. |
| `labels` | boolean tensor of shape \[N\] denoting positive(=True) and negative (=False) examples. |
| `scope` | name scope. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `sampled_idx_indicator` | boolean tensor of shape \[N\], True for entries which are sampled. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | if labels and indicator are not 1D boolean tensors. |

<br />

### `subsample_indicator`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/official/vision/ops/sampling_ops.py#L139-L167)

    @staticmethod
    subsample_indicator(
        indicator, num_samples
    )

Subsample indicator vector.

Given a boolean indicator vector with M elements set to `True`, the function
assigns all but `num_samples` of these previously `True` elements to
`False`. If `num_samples` is greater than M, the original indicator vector
is returned.

<br />

<br />

<br />

| Args ||
|---|---|
| `indicator` | a 1-dimensional boolean tensor indicating which elements are allowed to be sampled and which are not. |
| `num_samples` | int32 scalar tensor |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| a boolean tensor with the same shape as input (indicator) tensor ||

<br />