-
Notifications
You must be signed in to change notification settings - Fork 22.1k
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
torch.multinomial example is incorrect #48273
Comments
Thanks you for reporting this issue, @evanphilipsmith. cc @ngimel |
Hasn't this part been revised yet? Unlike documentation, neither I don't get an error message in torch 1.10.0. |
Issue description (brief recap)In fact it is not a documentation issue it is a bug - unexpected behaviour Steps to reproduce the issueimport torch
weights = torch.tensor([0, 10, 3, 0], dtype=torch.float)
torch.multinomial(weights, 4) What's the expected result?RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False,
not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320 What's the actual result?No errors are raised tensor([2, 1, 0, 3]) Possible causeThis seems to be an error in an old file THArgCheckWithCleanup((with_replacement || (n_categories - n_zeros >= n_sample)),
THCleanup(THDoubleTensor_free(cum_dist); if (start_dim == 1) THTensor_(squeeze1d)(prob_dist, prob_dist, 0);),
2,
"invalid multinomial distribution (with replacement=False, not enough non-negative category to sample)");
Now the file in charge of checking for the right behavior is |
Also reported here. |
Hey, 3 years have passed, why this bug is still not fixed? |
π Documentation
The
torch.multinomial
documentation page has an example that seems out-of-date or just wrong π€·ββοΈHere is a link to the example in the code
pytorch/torch/_torch_docs.py
Lines 5588 to 5597 in 8819bad
The example seems to contradict the note that says
I am fairly sure that 0 is non-negative and finite, so as long as there are other positive inputs in the input tensor (to satisfy the non-zero sum requirement), it should work.
Here is the output that I see when I run the example code in Google Colab.
I think it is also worth noting that the example shows the following runtime error
Which gives me the impression that you can have negative numbers in the input tensor, and it just will not sample them. This is not the case, however. Note the observed runtime error when I change the 0's in the weight tensor to -1's:
And just for the sake of being thorough, this is the runtime error that shows up when you try to take too many samples:
cc @fritzo @neerajprad @alicanb @vishwakftw @nikitaved @jlin27 @mruberry
The text was updated successfully, but these errors were encountered: