bincount(input: Tensor, weights: Tensor | None = None, minlength: int = 0)Count occurrences of each integer value in input.
One scatter_add into a zero tensor of the output's length. That
length depends on the largest value, so it costs one read of the
input's minimum and maximum back to the host — the device round trip
every data-dependent output size takes on Metal.
Parameters
inputTensor1-D tensor of non-negative integers.
1-D, as long as
input. When supplied, output[i] is the
sum of weights for entries with value i (rather than the
plain count). Default None.minlengthint= 0Floor for the output length. The output has length
max(input.max() + 1, minlength). Default 0.Returns
Tensor1-D tensor on input's device: int64 counts without
weights; with them, the weights' dtype for float32 /
float64 weights and float64 for any other (float32 on
Metal, which holds no float64).
Raises
TypeErrorinput is not an integer tensor.ValueErrorinput is not 1-D or holds a negative value, minlength is
negative, or weights is not 1-D and as long as input.Examples
>>> import lucid
>>> lucid.bincount(lucid.tensor([0, 1, 1, 3]))
tensor([1, 2, 0, 1], dtype=lucid.int64)
>>> lucid.bincount(lucid.tensor([0, 1, 1]), weights=lucid.tensor([1.0, 2.0, 3.0]))
tensor([1., 5.])