tfm.vision.sampling_ops.BalancedPositiveNegativeSampler
Stay organized with collections
Save and categorize content based on your preferences.
Subsamples minibatches to a desired balance of positives and negatives.
tfm.vision.sampling_ops.BalancedPositiveNegativeSampler(
positive_fraction=0.5, is_static=False
)
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
|
Methods
subsample
View source
subsample(
indicator, batch_size, labels, scope=None
)
Returns subsampled minibatch.
| 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.
|
| Returns |
sampled_idx_indicator
|
boolean tensor of shape [N], True for entries which
are sampled.
|
| Raises |
ValueError
|
if labels and indicator are not 1D boolean tensors.
|
subsample_indicator
View source
@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.
| Args |
indicator
|
a 1-dimensional boolean tensor indicating which elements
are allowed to be sampled and which are not.
|
num_samples
|
int32 scalar tensor
|
| Returns |
|
a boolean tensor with the same shape as input (indicator) tensor
|
[null,null,["Last updated 2026-11-06 UTC."],[],[]]