Skip to content

torch.bincount errors on empty input #9756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidbau opened this issue Jul 24, 2018 · 2 comments
Closed

torch.bincount errors on empty input #9756

davidbau opened this issue Jul 24, 2018 · 2 comments

Comments

@davidbau
Copy link

davidbau commented Jul 24, 2018

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.
@zou3519
Copy link
Contributor

zou3519 commented Jul 24, 2018

@davidbau Numpy doesn't document this behavior docs here for the numpy.bincount([]) case.

The docs suggest the output should be

out : ndarray of ints
The result of binning the input array. The length of out is equal to np.amax(x)+1.

but if x is [], the size of the output isn't well defined. Do you think torch.bincount(torch.LongTensor([])) = torch.tensor([]) makes sense?

@davidbau
Copy link
Author

Producing an empty vector from an empty count makes sense to me, although I find that in practice, I'm always using minlength, in which case the right output shape is unambiguous.

PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this issue Aug 10, 2018
Summary:
Added tests too. Fixes pytorch#9756 .
Pull Request resolved: pytorch#9757

Differential Revision: D8966879

Pulled By: soumith

fbshipit-source-id: 9f08a9d5d5d037db16319141d7a227a5efa23869
goodlux pushed a commit to goodlux/pytorch that referenced this issue Aug 15, 2018
Summary:
Added tests too. Fixes pytorch#9756 .
Pull Request resolved: pytorch#9757

Differential Revision: D8966879

Pulled By: soumith

fbshipit-source-id: 9f08a9d5d5d037db16319141d7a227a5efa23869
facebook-github-bot pushed a commit that referenced this issue Aug 16, 2018
Summary:
Added tests too. Fixes #9756 .
Pull Request resolved: #9757

Reviewed By: Yangqing

Differential Revision: D9348485

Pulled By: soumith

fbshipit-source-id: e13afadf8dbea20ee6ee595383c522dcbaf8796a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants