Bird
Raised Fist0
PyTorchml~20 mins

Custom transforms in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Transform Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a custom PyTorch transform on a tensor
Given the following custom transform class and input tensor, what is the output tensor after applying the transform?
PyTorch
import torch
class AddScalar:
    def __init__(self, scalar):
        self.scalar = scalar
    def __call__(self, x):
        return x + self.scalar

transform = AddScalar(3)
tensor = torch.tensor([1, 2, 3])
output = transform(tensor)
print(output)
Atensor([4, 5, 6])
Btensor([3, 3, 3])
Ctensor([1, 2, 3])
Dtensor([6, 7, 8])
Attempts:
2 left
💡 Hint
Remember the transform adds the scalar 3 to each element of the tensor.
❓ Model Choice
intermediate
2:00remaining
Choosing the correct custom transform for image normalization
You want to create a custom PyTorch transform that normalizes an image tensor by subtracting the mean and dividing by the standard deviation. Which class correctly implements this?
A
class Normalize:
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std
    def __call__(self, x):
        return (x - self.mean) / self.std
B
class Normalize:
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std
    def __call__(self, x):
        return x + self.mean - self.std
C
class Normalize:
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std
    def __call__(self, x):
        return x * self.std + self.mean
D
class Normalize:
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std
    def __call__(self, x):
        return (x + self.mean) / self.std
Attempts:
2 left
💡 Hint
Normalization means subtracting mean and dividing by std deviation.
❓ Hyperparameter
advanced
2:00remaining
Effect of changing scalar in a custom transform
You have a custom transform that multiplies input tensors by a scalar value. If you increase the scalar from 2 to 5, what is the expected effect on the model training when using this transform on input data?
AThe input values become negative, causing the model to fail training.
BThe input values become smaller, which slows down training and reduces model accuracy.
CThe input values remain unchanged, so training is unaffected.
DThe input values become larger, which may cause the model to learn faster but risk numerical instability.
Attempts:
2 left
💡 Hint
Multiplying inputs by a larger scalar increases their magnitude.
🔧 Debug
advanced
2:00remaining
Identify the error in this custom transform code
What error will this custom transform code raise when applied to a tensor?
PyTorch
import torch
class Multiply:
    def __init__(self, factor):
        self.factor = factor
    def __call__(self, x):
        return x * self.factor

transform = Multiply(4)
tensor = torch.tensor([1, 2, 3])
output = transform(tensor)
print(output)
ANo error, output is tensor([4, 8, 12])
BNameError: name 'factor' is not defined
CAttributeError: 'Multiply' object has no attribute 'factor'
DTypeError: unsupported operand type(s) for *: 'int' and 'Multiply'
Attempts:
2 left
💡 Hint
Check variable usage inside the __call__ method.
🧠 Conceptual
expert
2:00remaining
Why use custom transforms instead of built-in ones?
Which of the following is the best reason to create a custom transform in PyTorch instead of using built-in transforms?
ATo avoid using PyTorch's DataLoader class.
BBecause built-in transforms are slower and less efficient.
CTo implement a unique data preprocessing step not available in built-in transforms.
DBecause custom transforms automatically improve model accuracy.
Attempts:
2 left
💡 Hint
Think about when you need something special that built-in tools don't offer.

Practice

(1/5)
1. What is the main purpose of creating a custom transform in PyTorch?
easy
A. To speed up model training by changing the optimizer
B. To save the model after training
C. To define a new neural network layer
D. To apply a specific data change not available in standard transforms

Solution

  1. Step 1: Understand what transforms do

    Transforms change input data before training to help the model learn better.
  2. Step 2: Identify the role of custom transforms

    Custom transforms let you add new ways to change data that standard tools don't have.
  3. Final Answer:

    To apply a specific data change not available in standard transforms -> Option D
  4. Quick Check:

    Custom transform = new data change [OK]
Hint: Custom transforms change data uniquely before training [OK]
Common Mistakes:
  • Confusing transforms with model layers
  • Thinking transforms speed up training directly
  • Mixing transforms with saving models
2. Which of the following is the correct way to define a custom transform class in PyTorch?
easy
A. class MyTransform: def transform(self, x): return x * 2
B. class MyTransform: def __call__(self, x): return x * 2
C. def MyTransform(x): return x * 2
D. def MyTransform: return x * 2

Solution

  1. Step 1: Recall custom transform structure

    Custom transforms are classes with a __call__ method to make them callable.
  2. Step 2: Check each option

    class MyTransform: def __call__(self, x): return x * 2 defines a class with __call__, correct syntax. Others miss class or __call__ or have wrong syntax.
  3. Final Answer:

    class MyTransform: def __call__(self, x): return x * 2 -> Option B
  4. Quick Check:

    Custom transform = class + __call__ method [OK]
Hint: Custom transform must be a class with __call__ method [OK]
Common Mistakes:
  • Defining transform as a function only
  • Using wrong method name like transform instead of __call__
  • Missing class keyword
3. Given this custom transform code, what will be the output of transform(3)?
class AddFive:
    def __call__(self, x):
        return x + 5

transform = AddFive()
medium
A. 8
B. 15
C. 3
D. Error

Solution

  1. Step 1: Understand the transform class behavior

    The __call__ method adds 5 to the input x.
  2. Step 2: Calculate transform(3)

    Input 3 plus 5 equals 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    3 + 5 = 8 [OK]
Hint: Call __call__ method adds 5 to input [OK]
Common Mistakes:
  • Confusing addition with multiplication
  • Expecting the original input unchanged
  • Thinking transform is not callable
4. Identify the error in this custom transform code:
class Multiply:
    def call(self, x):
        return x * 3

transform = Multiply()
print(transform(4))
medium
A. Method should be named __call__, not call
B. Class should inherit from nn.Module
C. Missing return statement in method
D. transform(4) is invalid syntax

Solution

  1. Step 1: Check method name for callable class

    To make an instance callable, method must be __call__, not call.
  2. Step 2: Verify other parts

    Class inheritance is optional for transforms; return is present; calling instance is valid.
  3. Final Answer:

    Method should be named __call__, not call -> Option A
  4. Quick Check:

    Callable class needs __call__ method [OK]
Hint: Callable classes need __call__, not call method [OK]
Common Mistakes:
  • Using call instead of __call__
  • Thinking inheritance from nn.Module is required
  • Missing return statement
5. You want to create a custom transform that normalizes a tensor by subtracting its mean and dividing by its standard deviation. Which code correctly implements this?
hard
A. class Normalize: def __call__(self, x): return x + x.mean() + x.std()
B. class Normalize: def __call__(self, x): return x / x.mean() - x.std()
C. class Normalize: def __call__(self, x): return (x - x.mean()) / x.std()
D. class Normalize: def __call__(self, x): return x * x.mean() / x.std()

Solution

  1. Step 1: Recall normalization formula

    Normalization means subtract mean and divide by standard deviation.
  2. Step 2: Check each option's formula

    class Normalize: def __call__(self, x): return (x - x.mean()) / x.std() matches formula: (x - mean) / std. Others do incorrect math.
  3. Final Answer:

    class Normalize: def __call__(self, x): return (x - x.mean()) / x.std() -> Option C
  4. Quick Check:

    Normalize = (x - mean) / std [OK]
Hint: Normalize = subtract mean, then divide by std [OK]
Common Mistakes:
  • Dividing before subtracting mean
  • Adding mean and std instead of subtract/divide
  • Multiplying by mean/std incorrectly
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