Skip to content

fix(optimization_tutorial): replace deprecated ToTensor with v2 transforms#3870

Open
Kadermiyanyedi wants to merge 3 commits intopytorch:mainfrom
Kadermiyanyedi:fix/optimization-tutorial-v2-transforms
Open

fix(optimization_tutorial): replace deprecated ToTensor with v2 transforms#3870
Kadermiyanyedi wants to merge 3 commits intopytorch:mainfrom
Kadermiyanyedi:fix/optimization-tutorial-v2-transforms

Conversation

@Kadermiyanyedi
Copy link
Copy Markdown

Fixes #3854

Description

  • Replace deprecated torchvision.transforms.ToTensor() with v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) in beginner_source/basics/optimization_tutorial.py
  • Aligns the tutorial with the supported torchvision v2 transforms API (ToTensor deprecated since torchvision 0.16)

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

cc @subramen @svekars

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 6, 2026

🔗 Helpful Links

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

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

⏳ No Failures, 1 Pending

As of commit 4d6b286 with merge base 08435cc (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 6, 2026

Hi @Kadermiyanyedi!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla meta-cla Bot added the cla signed label May 6, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 6, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@sekyondaMeta
Copy link
Copy Markdown
Contributor

@claude Review this PR and compare it to the issue #3854

@claude
Copy link
Copy Markdown

claude Bot commented May 6, 2026

Claude finished @sekyondaMeta's task in 1m 49s —— View job


Review: PR #3870 vs Issue #3854


Summary

This PR replaces the deprecated torchvision.transforms.ToTensor() with the v2 transforms API (v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])) in beginner_source/basics/optimization_tutorial.py.

Alignment with Issue #3854

Based on the PR description, issue #3854 requests replacing the deprecated ToTensor() usage. This PR correctly addresses that by:

  1. Replacing the importfrom torchvision.transforms import ToTensorfrom torchvision.transforms import v2
  2. Updating both dataset transform calls — both training_data and test_data now use the v2 equivalent ✅
  3. Using the correct v2 replacementv2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) is the officially recommended replacement for ToTensor() per the torchvision docs

Code Review

The change is correct and minimal. The diff is +3/-3 lines, touching only what's necessary.

One observation (non-blocking): Other tutorials in the same beginner_source/basics/ directory still use the deprecated ToTensor():

  • quickstart_tutorial.py (lines 29, 46, 54)
  • transforms_tutorial.py (lines 26, 31, 37, 42, 45)
  • data_tutorial.py (lines 51, 59, 66)

Since this PR only targets optimization_tutorial.py (matching the scope of issue #3854), this is fine — but it does create an inconsistency within the "Learn the Basics" tutorial series. A follow-up PR for the other files would be worthwhile to ensure learners see consistent patterns across the sequence.

Verdict

LGTM — The change is correct, well-scoped, and addresses the issue as described. The v2 replacement is the canonical approach recommended by torchvision.


@Kadermiyanyedi
Copy link
Copy Markdown
Author

@sekyondaMeta Hi, I saw the CI failed, I am not sure why. I just rebased again but still can’t find the problem, could you help me please?

@sekyondaMeta
Copy link
Copy Markdown
Contributor

@Kadermiyanyedi The failure seems to not be related to what you did in this PR. But taking a deeper look just tob e sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Optimization tutorial — replace deprecated ToTensor() with v2 transforms

2 participants