pytorch - 💡(How to fix) Fix [Feedback about torch.multinomial][Docs] Inconsistency in torch.multinomial: Note says "lower than" but example allows "equal to" for num_samples without replacement [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#181347Fetched 2026-04-25 06:03:02
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Error Message

I noticed a minor wording inconsistency in the documentation for torch.multinomial. The Note section states that when replacement=False, num_samples must be strictly lower than the number of non-zero elements. However, the provided example explicitly demonstrates that num_samples can be equal to the number of non-zero elements without raising an error.

Code Example

>>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float)  # Contains exactly 2 non-zero elements
>>> torch.multinomial(weights, 2)  # Runs successfully, returns tensor([1, 2])
RAW_BUFFERClick to expand / collapse

There is the following issue on this page: https://docs.pytorch.org/docs/stable/generated/torch.multinomial.html

Description: I noticed a minor wording inconsistency in the documentation for torch.multinomial. The Note section states that when replacement=False, num_samples must be strictly lower than the number of non-zero elements. However, the provided example explicitly demonstrates that num_samples can be equal to the number of non-zero elements without raising an error.

Current Documentation Text:

Note: When drawn without replacement, num_samples must be lower than number of non-zero elements in input (or the min number of non-zero elements in each row of input if it is a matrix).

Evidence from Example:

>>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float)  # Contains exactly 2 non-zero elements
>>> torch.multinomial(weights, 2)  # Runs successfully, returns tensor([1, 2])

extent analysis

TL;DR

The documentation for torch.multinomial should be updated to reflect that num_samples can be equal to the number of non-zero elements when replacement=False.

Guidance

  • Review the documentation for torch.multinomial to ensure accuracy regarding the relationship between num_samples and non-zero elements when replacement=False.
  • Verify the behavior of torch.multinomial with different inputs and num_samples values to confirm the observed inconsistency.
  • Consider updating the Note section to indicate that num_samples can be less than or equal to the number of non-zero elements.
  • Check for any potential edge cases or errors that may arise when num_samples equals the number of non-zero elements.

Example

weights = torch.tensor([0, 10, 3, 0], dtype=torch.float)
print(torch.multinomial(weights, 2))  # Expected to run successfully

Notes

The current documentation may cause confusion for users who rely on it for understanding the behavior of torch.multinomial. Updating the documentation will help ensure accuracy and clarity.

Recommendation

Apply workaround: Update the documentation to reflect the correct behavior of torch.multinomial when replacement=False, allowing num_samples to be equal to the number of non-zero elements.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING