KEMBAR78
DOC: update CrossEntropyLoss with note and example of incorrect target specification by loganthomas · Pull Request #155649 · pytorch/pytorch · GitHub
Skip to content

Conversation

@loganthomas
Copy link
Contributor

@loganthomas loganthomas commented Jun 11, 2025

@pytorch-bot
Copy link

pytorch-bot bot commented Jun 11, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/155649

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 89f38af with merge base 2259dbe (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions github-actions bot added docathon-h1-2025 medium Label for medium docathon tasks module: docs Related to our documentation, both in docs/ and docblocks module: loss Problem is related to loss function triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jun 11, 2025
@loganthomas loganthomas changed the title doc: update CrossEntropyLoss with note and example of incorrect target specification DOC: update CrossEntropyLoss with note and example of incorrect target specification Jun 11, 2025
@svekars svekars added the topic: not user facing topic category label Jun 11, 2025
@loganthomas
Copy link
Contributor Author

@svekars any update on this? Thanks!

@albanD albanD removed their request for review June 16, 2025 13:13
Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@loganthomas
Copy link
Contributor Author

Thank you both!

@loganthomas
Copy link
Contributor Author

@svekars any blockers on this PR getting merged? Thanks! 🚀

@loganthomas
Copy link
Contributor Author

@svekars @mikaylagawarecki are there any blockers I can take care of to get this PR merged?

@svekars
Copy link
Contributor

svekars commented Jul 3, 2025

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 3, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorch-bot pytorch-bot bot removed the ciflow/trunk Trigger trunk jobs on your pull request label Jul 7, 2025
@loganthomas
Copy link
Contributor Author

@svekars @mikaylagawarecki trying to sync with latest main to remove merge failures

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 8, 2025

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@loganthomas
Copy link
Contributor Author

@pytorchbot merge

@loganthomas
Copy link
Contributor Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / build

Details for Dev Infra team Raised by workflow job

@loganthomas
Copy link
Contributor Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 1, 3, macos-m1-stable)

Details for Dev Infra team Raised by workflow job

@loganthomas
Copy link
Contributor Author

@mikaylagawarecki @svekars is it possible to rerun the one failing test to get this green? I'm not sure how to do that. I don't see any documentation on a pytorchbot command for that.

Thank you for your help. Sorry a simple doc string fix is taking this much time and effort!

@sekyondaMeta
Copy link
Collaborator

@loganthomas I keep getting an error that disappears when I run it. Will run it again and send it your way to see if you can make heads or tails of it. It has something to do with the example in the docstring

@sekyondaMeta
Copy link
Collaborator

@loganthomas see error below:

=== Found 1 errors ===
--- Error: 1 / 1 ---
    * REASON: GotWantException
    DOCTEST DEBUG INFO
      XDoc "/Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py::CrossEntropyLoss:1", line 17 <- wrt doctest
      File "/Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py", line 1349, <- wrt source file
    DOCTEST PART BREAKDOWN
    Passed Parts:
         2 >>> # Example of target with incorrectly specified class probabilities
         3 >>> loss = nn.CrossEntropyLoss()
         4 >>> torch.manual_seed(283)
         5 >>> input = torch.randn(3, 5, requires_grad=True)
         6 >>> target = torch.randn(3, 5)
         7 >>> # Provided target class probabilities are not in range [0,1]
         8 >>> target
        13 >>> target.sum(axis=1)
    Failed Part:
        16 >>> loss(input, target).item()
    Remaining Parts:
        18 >>>
        19 >>> # Example of target with correctly specified class probabilities
        20 >>> # Use .softmax() to ensure true probability distribution
        21 >>> target_new = target.softmax(dim=1)
        22 >>> # New target class probabilities all in range [0,1]
        23 >>> target_new
        27 >>> # New target class probabilities sum to 1
        28 >>> target_new.sum(axis=1)
        30 >>> loss(input, target_new).item()
    DOCTEST TRACEBACK
    Expected:
        4.6379876136779785
    Got:
        4.637986660003662
    Repr Difference:
        got  = '4.637986660003662'
        want = '4.6379876136779785'
    DOCTEST REPRODUCTION
    CommandLine:
        python -m xdoctest /Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py CrossEntropyLoss:1

=== Failed tests ===
python -m xdoctest /Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py CrossEntropyLoss:1
=== 1 failed, 338 passed, 392 skipped, 153 warnings in 10.01 seconds ===
GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, or ARTIFACTS_FILE_SUFFIX not set, not uploading
Uploading artifacts took 0.00 seconds
Writing 1 documents to S3 ossci-raw-job-status/ossci_uploaded_metrics/td_test_failure_stats_v2_1755177625_70944228791111f08d22268327012cbd
Done! Finish writing document to S3 ossci-raw-job-status/ossci_uploaded_metrics/td_test_failure_stats_v2_1755177625_70944228791111f08d22268327012cbd 
    main()
  File "/Users/ec2-user/runner/_work/pytorch/pytorch/test/run_test.py", line 2037, in main
    run_tests(
  File "/Users/ec2-user/runner/_work/pytorch/pytorch/test/run_test.py", line 1872, in run_tests
    raise RuntimeError(failure.message + keep_going_message)
RuntimeError: doctests 1/1 failed!

Tip: You can keep running tests even on failure by passing --keep-going to run_test.py.
If running on CI, add the 'keep-going' label to your PR and rerun your jobs.
Error: Process completed with exit code 1.

@sekyondaMeta
Copy link
Collaborator

@loganthomas see error below:

=== Found 1 errors ===
--- Error: 1 / 1 ---
    * REASON: GotWantException
    DOCTEST DEBUG INFO
      XDoc "/Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py::CrossEntropyLoss:1", line 17 <- wrt doctest
      File "/Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py", line 1349, <- wrt source file
    DOCTEST PART BREAKDOWN
    Passed Parts:
         2 >>> # Example of target with incorrectly specified class probabilities
         3 >>> loss = nn.CrossEntropyLoss()
         4 >>> torch.manual_seed(283)
         5 >>> input = torch.randn(3, 5, requires_grad=True)
         6 >>> target = torch.randn(3, 5)
         7 >>> # Provided target class probabilities are not in range [0,1]
         8 >>> target
        13 >>> target.sum(axis=1)
    Failed Part:
        16 >>> loss(input, target).item()
    Remaining Parts:
        18 >>>
        19 >>> # Example of target with correctly specified class probabilities
        20 >>> # Use .softmax() to ensure true probability distribution
        21 >>> target_new = target.softmax(dim=1)
        22 >>> # New target class probabilities all in range [0,1]
        23 >>> target_new
        27 >>> # New target class probabilities sum to 1
        28 >>> target_new.sum(axis=1)
        30 >>> loss(input, target_new).item()
    DOCTEST TRACEBACK
    Expected:
        4.6379876136779785
    Got:
        4.637986660003662
    Repr Difference:
        got  = '4.637986660003662'
        want = '4.6379876136779785'
    DOCTEST REPRODUCTION
    CommandLine:
        python -m xdoctest /Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py CrossEntropyLoss:1

=== Failed tests ===
python -m xdoctest /Users/ec2-user/runner/_work/_temp/venv-3.12-1755177273/lib/python3.12/site-packages/torch/nn/modules/loss.py CrossEntropyLoss:1
=== 1 failed, 338 passed, 392 skipped, 153 warnings in 10.01 seconds ===
GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, or ARTIFACTS_FILE_SUFFIX not set, not uploading
Uploading artifacts took 0.00 seconds
Writing 1 documents to S3 ossci-raw-job-status/ossci_uploaded_metrics/td_test_failure_stats_v2_1755177625_70944228791111f08d22268327012cbd
Done! Finish writing document to S3 ossci-raw-job-status/ossci_uploaded_metrics/td_test_failure_stats_v2_1755177625_70944228791111f08d22268327012cbd 
    main()
  File "/Users/ec2-user/runner/_work/pytorch/pytorch/test/run_test.py", line 2037, in main
    run_tests(
  File "/Users/ec2-user/runner/_work/pytorch/pytorch/test/run_test.py", line 1872, in run_tests
    raise RuntimeError(failure.message + keep_going_message)
RuntimeError: doctests 1/1 failed!

Tip: You can keep running tests even on failure by passing --keep-going to run_test.py.
If running on CI, add the 'keep-going' label to your PR and rerun your jobs.
Error: Process completed with exit code 1.

@mikaylagawarecki this looks like a precision issue maybe something that isnt such a big issue?

@loganthomas
Copy link
Contributor Author

@sekyondaMeta thank you! Yes, this appears to be a minor precision discrepancy and not blocking in the context of the example. For the main purpose of the example, these should be considered equivalent.

@mikaylagawarecki
Copy link
Contributor

Yea, I think you can either update the value or skip the doctest for that example by doing something like

>>> # xdoctest: +SKIP

@pytorch-bot pytorch-bot bot removed the ciflow/trunk Trigger trunk jobs on your pull request label Aug 14, 2025
@loganthomas
Copy link
Contributor Author

@sekyondaMeta @mikaylagawarecki thank you both. is there a way to kick off a new test run? happy to do this is there is a build int command.

@mikaylagawarecki
Copy link
Contributor

we have to kick it off for you, but I just did that :)

@mikaylagawarecki
Copy link
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 14, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@loganthomas
Copy link
Contributor Author

@sekyondaMeta @mikaylagawarecki @svekars 🎉 thank you so much for all your help!!!

can-gaa-hou pushed a commit to can-gaa-hou/pytorch that referenced this pull request Aug 22, 2025
…t specification (pytorch#155649)

Fixes pytorch#134771

Pull Request resolved: pytorch#155649
Approved by: https://github.com/mikaylagawarecki

Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: mikaylagawarecki <mikaylagawarecki@gmail.com>
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…t specification (pytorch#155649)

Fixes pytorch#134771

Pull Request resolved: pytorch#155649
Approved by: https://github.com/mikaylagawarecki

Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
Co-authored-by: mikaylagawarecki <mikaylagawarecki@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request docathon-h1-2025 medium Label for medium docathon tasks Merged module: docs Related to our documentation, both in docs/ and docblocks module: loss Problem is related to loss function open source topic: not user facing 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 this pull request may close these issues.

Setting the tensor of the values out of [0,1] to target argument of nn.CrossEntropyLoss() with class probabilities works against the doc

6 participants