From 39ab47d0dece397c9d517837562575b2607b5ac8 Mon Sep 17 00:00:00 2001 From: AmitChaubey Date: Sun, 12 Apr 2026 22:49:58 +0100 Subject: [PATCH 1/2] Fix Transforms tutorial: unify target_transform and ToTensor prose --- beginner_source/basics/transforms_tutorial.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/beginner_source/basics/transforms_tutorial.py b/beginner_source/basics/transforms_tutorial.py index 33076958bf5..61f813e0fd9 100644 --- a/beginner_source/basics/transforms_tutorial.py +++ b/beginner_source/basics/transforms_tutorial.py @@ -30,12 +30,18 @@ from torchvision import datasets from torchvision.transforms import ToTensor, Lambda +target_transform = Lambda( + lambda y: torch.zeros(10, dtype=torch.float32).scatter_( + dim=0, index=torch.tensor(y), value=1 + ) +) + ds = datasets.FashionMNIST( root="data", train=True, download=True, transform=ToTensor(), - target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1)) + target_transform=target_transform, ) ################################################# @@ -43,7 +49,7 @@ # ------------------------------- # # `ToTensor `_ -# converts a PIL image or NumPy ``ndarray`` into a ``FloatTensor``. and scales +# converts a PIL image or NumPy ``ndarray`` into a ``FloatTensor`` and scales # the image's pixel intensity values in the range [0., 1.] # @@ -51,14 +57,11 @@ # Lambda Transforms # ------------------------------- # -# Lambda transforms apply any user-defined lambda function. Here, we define a function -# to turn the integer into a one-hot encoded tensor. -# It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls -# `scatter_ `_ which assigns a -# ``value=1`` on the index as given by the label ``y``. - -target_transform = Lambda(lambda y: torch.zeros( - 10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1)) +# Lambda transforms apply any user-defined callable. Above, ``target_transform`` wraps a +# small lambda that turns each label ``y`` into a one-hot encoded tensor: it allocates a +# length-10 zero vector and uses +# `scatter_ `_ to write +# ``value=1`` at index ``y``. ###################################################################### # -------------- From 6db67a5eb042b6c63e06e751033da9b537c81d8c Mon Sep 17 00:00:00 2001 From: AmitChaubey Date: Mon, 13 Apr 2026 18:29:34 +0100 Subject: [PATCH 2/2] retrigger CI