torch.bincount
-
torch.bincount(input, weights=None, minlength=0) → Tensor
-
Count the frequency of each value in an array of non-negative ints.
The number of bins (size 1) is one larger than the largest value in
input
unlessinput
is empty, in which case the result is a tensor of size 0. Ifminlength
is specified, the number of bins is at leastminlength
and ifinput
is empty, then the result is tensor of sizeminlength
filled with zeros. Ifn
is the value at positioni
,out[n] += weights[i]
ifweights
is specified elseout[n] += 1
.Note
This operation may produce nondeterministic gradients when given tensors on a CUDA device. See Reproducibility for more information.
- Parameters
- Returns
-
a tensor of shape
Size([max(input) + 1])
ifinput
is non-empty, elseSize(0)
- Return type
-
output (Tensor)
Example:
>>> input = torch.randint(0, 8, (5,), dtype=torch.int64) >>> weights = torch.linspace(0, 1, steps=5) >>> input, weights (tensor([4, 3, 6, 3, 4]), tensor([ 0.0000, 0.2500, 0.5000, 0.7500, 1.0000]) >>> torch.bincount(input) tensor([0, 0, 0, 2, 2, 0, 1]) >>> input.bincount(weights) tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/generated/torch.bincount.html