Skip to content

Modernize transforms tutorial to torchvision v2 API#3861

Open
omkar-334 wants to merge 1 commit intopytorch:mainfrom
omkar-334:fix/3853-transforms-v2
Open

Modernize transforms tutorial to torchvision v2 API#3861
omkar-334 wants to merge 1 commit intopytorch:mainfrom
omkar-334:fix/3853-transforms-v2

Conversation

@omkar-334
Copy link
Copy Markdown

@omkar-334 omkar-334 commented May 5, 2026

Fixes #3853

Description

files changed - beginner_source/basics/transforms_tutorial.py

  1. Replaced deprecated torchvision.transforms.ToTensor (deprecated since torchvision 0.16) with the v2 pipeline v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]).
  2. Switched the legacy v1 transforms namespace import to from torchvision.transforms import v2.
  3. Replaced the low-level torch.zeros(...).scatter_(...) one-hot pattern with torch.nn.functional.one_hot(torch.tensor(y), num_classes=10).float(), wrapped in v2.Lambda.
  4. Updated the section heading and explanatory prose so they describe v2.ToImage() + v2.ToDtype() and F.one_hot instead of ToTensor and scatter_.

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

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 5, 2026

🔗 Helpful Links

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

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

✅ No Failures

As of commit 90cde13 with merge base 5efb99a (image):
💚 Looks good so far! There are no failures yet. 💚

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

@sekyondaMeta
Copy link
Copy Markdown
Contributor

@claude

@claude
Copy link
Copy Markdown

claude Bot commented May 5, 2026

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


Review of PR #3861

  • Gather context and read changed files
  • Review code changes for correctness and style
  • Post review feedback

Overall: This is a clean, well-scoped modernization. The changes correctly migrate from the deprecated torchvision.transforms.ToTensor to the v2 API, and the one_hot replacement is more readable than the manual scatter_ pattern. A few observations:

Looks Good

  1. v2 API usage is correctv2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) is the recommended replacement for ToTensor() per the torchvision docs.
  2. one_hot replacement is cleanertorch.nn.functional.one_hot(torch.tensor(y), num_classes=10).float() is more readable and idiomatic than torch.zeros(10).scatter_(0, torch.tensor(y), value=1).
  3. Prose is clear and concise — the explanatory text matches the code and links to correct documentation pages.
  4. Section heading updated appropriately — "ToImage() and ToDtype()" accurately describes the new content.

Minor Suggestions (non-blocking)

  1. Line 38 is long (~100 chars) — Consider breaking the target_transform lambda across lines for readability, though this is a style preference and the current form is acceptable for a tutorial:

    target_transform=v2.Lambda(
        lambda y: torch.nn.functional.one_hot(torch.tensor(y), num_classes=10).float()
    )

    Same applies to line 61 where it's repeated.

  2. import torch.nn.functional as F — The PR uses torch.nn.functional.one_hot inline (twice). A common pattern in PyTorch tutorials is import torch.nn.functional as F and then F.one_hot(...). This would shorten the lambda and match conventions used elsewhere in PyTorch docs. However, since this tutorial is minimal and doesn't use F for anything else, the fully qualified name is also fine.

  3. Further Reading link — The "Further Reading" section at line 70 still links to the general transforms page. Consider adding or replacing with a direct link to the v2 transforms getting started guide for readers who want to explore the new API further.

Verdict

The PR is correct, well-motivated, and addresses issue #3853. The suggestions above are minor polish — this is ready to merge as-is.


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 Transforms tutorial — replace deprecated ToTensor(), migrate to v2 namespace, modernize one-hot encoding

3 participants