Skip to content

torch.bincount errors on empty input #9756

Closed
@davidbau

Description

@davidbau

Issue description

torch.bincount gives an error when given an empty tensor, rather than just returning zero counts like numpy.bincount. Trips up edge cases when counting filtered data.

Code example

>>> import torch, numpy
>>> numpy.bincount([1]), numpy.bincount([]), numpy.bincount([], minlength=10)
(array([0, 1]), array([], dtype=int64), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

>>> torch.bincount(torch.LongTensor([]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: bincount only supports 1-d non-negative integral inputs.

>>> torch.bincount(torch.LongTensor([]), minlength=10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: bincount only supports 1-d non-negative integral inputs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions