Skip to content
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 raises no error when sampling from zero weight and replacement=False. #125388

Open
FlyDogDaDa opened this issue May 2, 2024 · 1 comment · May be fixed by #125495
Open

torch.multinomial raises no error when sampling from zero weight and replacement=False. #125388

FlyDogDaDa opened this issue May 2, 2024 · 1 comment · May be fixed by #125495
Labels
actionable topic: docs topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@FlyDogDaDa
Copy link

🐛 Describe the bug

As in the example, torch.multinomial should raise an error.

If errors are not raised to block developers, it will happen that the wrong index samples are used to train the model without knowing it, like me.

Here is the code I executed:

import torch
print("git_version =", torch.version.git_version)
print("torch_version =", torch.version.__version__)
weights = torch.tensor([0, 10, 3, 0], dtype=torch.float)
a = torch.multinomial(weights, 2, replacement=False)
b = torch.multinomial(weights, 4, replacement=False)
print(a)
print(b)

I get the output:

git_version = 39901f229520a5256505ec24782f716ee7ddc843
torch_version = 2.2.2+cpu
tensor([1, 2])
tensor([2, 1, 0, 3])

Versions

Here is the version I'm using:torch_version=2.2.2+cpu

@drisspg
Copy link
Contributor

drisspg commented May 3, 2024

I think this is an issue with the docs but not with the actual code since there are 4 elements in the weights matrix and there are 4 things to draw. If you said this to 5 you should see the error

@drisspg drisspg added actionable topic: docs topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels May 3, 2024
shengfukevin pushed a commit to shengfukevin/pytorch that referenced this issue May 7, 2024
…pytorch#125495)

Summary: Updated docs to add the error condition for torch.multinomial

Test Plan: No change in code

Reviewers: @drisspg

Subscribers: @drisspg

Tasks:

Tags:

Fixes pytorch#125388

Pull Request resolved: pytorch#125495
Approved by: https://github.com/drisspg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable topic: docs topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants