Bird
Raised Fist0
PyTorchml~20 mins

Data transforms in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Transform Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output shape after applying this transform?
Consider a PyTorch tensor representing an image of shape (3, 64, 64). We apply the following transform:

transform = torchvision.transforms.Compose([torchvision.transforms.Resize(32), torchvision.transforms.CenterCrop(28)])

What will be the shape of the resulting tensor after applying transform?
PyTorch
import torch
import torchvision.transforms as T

img = torch.randn(3, 64, 64)
transform = T.Compose([T.Resize(32), T.CenterCrop(28)])
result = transform(img)
print(result.shape)
Atorch.Size([3, 64, 64])
Btorch.Size([3, 32, 32])
Ctorch.Size([1, 28, 28])
Dtorch.Size([3, 28, 28])
Attempts:
2 left
💡 Hint
Resize changes the smaller edge to 32, then CenterCrop cuts a 28x28 square from the center.
❓ Model Choice
intermediate
2:00remaining
Which transform is best to normalize image pixels to [0,1] range?
You have image tensors with pixel values from 0 to 255. You want to normalize them to the range [0,1] before feeding into a model. Which PyTorch transform should you use?
Atorchvision.transforms.Normalize(mean=[0.5], std=[0.5])
Btorchvision.transforms.ToTensor()
Ctorchvision.transforms.Lambda(lambda x: x / 255)
Dtorchvision.transforms.Resize(256)
Attempts:
2 left
💡 Hint
One transform converts PIL images or numpy arrays to tensors and scales pixels to [0,1].
❓ Hyperparameter
advanced
2:00remaining
Choosing mean and std for Normalize transform
You want to normalize RGB images using torchvision.transforms.Normalize. Which values for mean and std are correct if your images have pixel values in [0,1] and you want zero mean and unit variance per channel?
Amean=[0, 0, 0], std=[1, 1, 1]
Bmean=[1, 1, 1], std=[0, 0, 0]
Cmean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
Dmean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]
Attempts:
2 left
💡 Hint
Zero mean and unit variance means subtract 0 and divide by 1.
🔧 Debug
advanced
2:00remaining
Why does this transform code raise an error?
This code raises an error when applied to a tensor image. Why?

transform = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()])

Assume input is a PyTorch tensor image.
PyTorch
import torch
import torchvision.transforms as T
img = torch.randn(3, 64, 64)
transform = T.Compose([T.RandomHorizontalFlip(), T.ToTensor()])
result = transform(img)
ARandomHorizontalFlip expects a PIL image or numpy array, not a tensor
BToTensor must be applied before RandomHorizontalFlip
CRandomHorizontalFlip requires the tensor to be 4D, but input is 3D
DThe code runs without error
Attempts:
2 left
💡 Hint
Check the expected input types for RandomHorizontalFlip.
🧠 Conceptual
expert
2:00remaining
Why use data transforms during training but not always during testing?
In machine learning, why do we often apply random data transforms (like flips, crops) during training but use fixed transforms during testing?
ARandom transforms are only compatible with GPUs, fixed transforms work on CPUs
BRandom transforms reduce training time, fixed transforms speed up testing
CRandom transforms increase training data diversity to improve generalization, while testing uses fixed transforms for consistent evaluation
DRandom transforms normalize data, fixed transforms augment data
Attempts:
2 left
💡 Hint
Think about how randomness affects learning and evaluation.

Practice

(1/5)
1. What is the main purpose of using data transforms in PyTorch?
easy
A. To prepare and modify raw data before feeding it to a model
B. To create new neural network layers
C. To evaluate model accuracy
D. To save the trained model to disk

Solution

  1. Step 1: Understand the role of data transforms

    Data transforms modify raw data to make it suitable for model input, such as resizing or normalizing images.
  2. Step 2: Differentiate from other tasks

    Creating layers, evaluating accuracy, or saving models are separate tasks unrelated to data transforms.
  3. Final Answer:

    To prepare and modify raw data before feeding it to a model -> Option A
  4. Quick Check:

    Data transforms = prepare data [OK]
Hint: Transforms prepare data, not model or evaluation tasks [OK]
Common Mistakes:
  • Confusing transforms with model building
  • Thinking transforms evaluate model performance
  • Mixing transforms with saving models
2. Which of the following is the correct way to chain multiple transforms using PyTorch's torchvision?
easy
A. transforms.Sequence(transforms.Resize(256), transforms.ToTensor())
B. transforms.Chain([transforms.Resize(256), transforms.ToTensor()])
C. transforms.Pipeline(transforms.Resize(256), transforms.ToTensor())
D. transforms.Compose([transforms.Resize(256), transforms.ToTensor()])

Solution

  1. Step 1: Recall the correct class for chaining transforms

    PyTorch uses transforms.Compose to chain multiple transforms in a list.
  2. Step 2: Check syntax correctness

    Only transforms.Compose accepts a list of transforms; others like Sequence, Pipeline, Chain do not exist in torchvision.
  3. Final Answer:

    transforms.Compose([transforms.Resize(256), transforms.ToTensor()]) -> Option D
  4. Quick Check:

    Use Compose for chaining transforms [OK]
Hint: Remember Compose takes a list of transforms [OK]
Common Mistakes:
  • Using non-existent classes like Sequence or Pipeline
  • Passing transforms as separate arguments instead of a list
  • Confusing Compose with other transform functions
3. What will be the output shape of the tensor after applying the following transform to a 3-channel image of size 100x100?
transform = transforms.Compose([transforms.Resize(50), transforms.ToTensor()])
medium
A. [1, 50, 50]
B. [50, 50, 3]
C. [3, 50, 50]
D. [50, 3, 50]

Solution

  1. Step 1: Understand Resize effect

    transforms.Resize(50) resizes the image to 50x50 pixels, keeping the number of channels unchanged (3 channels).
  2. Step 2: Understand ToTensor output shape

    transforms.ToTensor() converts the image to a tensor with shape [channels, height, width], so shape is [3, 50, 50].
  3. Final Answer:

    [3, 50, 50] -> Option C
  4. Quick Check:

    Tensor shape = [channels, height, width] [OK]
Hint: Tensor shape is channels first after ToTensor [OK]
Common Mistakes:
  • Assuming channels last format
  • Confusing Resize parameter as channels
  • Mixing height and width order
4. Identify the error in this transform code snippet:
transform = transforms.Compose([
    transforms.Resize(128),
    transforms.Normalize(mean=[0.5], std=[0.5]),
    transforms.ToTensor()
])
medium
A. ToTensor should be first
B. Normalize should come after ToTensor
C. Resize must be after Normalize
D. No error, code is correct

Solution

  1. Step 1: Check order of transforms

    transforms.Normalize expects a tensor input, but here it is before ToTensor, which converts image to tensor.
  2. Step 2: Correct order

    ToTensor must come before Normalize to convert image to tensor first, then normalize it.
  3. Final Answer:

    Normalize should come after ToTensor -> Option B
  4. Quick Check:

    Normalize after ToTensor [OK]
Hint: Normalize needs tensor input, so place after ToTensor [OK]
Common Mistakes:
  • Placing Normalize before ToTensor
  • Assuming Resize order matters with Normalize
  • Thinking ToTensor can be anywhere
5. You want to apply random horizontal flip and then normalize an image tensor. Which of the following transform sequences is correct to ensure proper data augmentation and normalization?
hard
A. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])])
B. transforms.Compose([transforms.ToTensor(), transforms.RandomHorizontalFlip(), transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])])
C. transforms.Compose([transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]), transforms.RandomHorizontalFlip(), transforms.ToTensor()])
D. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]), transforms.ToTensor()])

Solution

  1. Step 1: Understand transform order for augmentation and normalization

    RandomHorizontalFlip works on PIL images, so it must come before ToTensor.
  2. Step 2: Normalize after converting to tensor

    Normalize requires tensor input, so it must come after ToTensor.
  3. Final Answer:

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])]) -> Option A
  4. Quick Check:

    Flip before ToTensor, Normalize after ToTensor [OK]
Hint: Augment on images, normalize on tensors [OK]
Common Mistakes:
  • Applying Normalize before ToTensor
  • Applying RandomHorizontalFlip after ToTensor
  • Mixing order of augmentation and normalization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard