A custom transform is a user-defined operation that modifies data (like images or tensors) before feeding it into a model. It helps prepare or augment data in ways not covered by built-in transforms.
Click to reveal answer
beginner
How do you create a custom transform class in PyTorch?
You create a class with a __call__ method that takes an input (like an image) and returns the transformed output. This class can then be used like a function in a transform pipeline.
Click to reveal answer
intermediate
Why use custom transforms instead of built-in ones?
Custom transforms let you apply specific changes tailored to your data or experiment, such as unique augmentations or preprocessing steps that built-in transforms don’t provide.
Click to reveal answer
beginner
What method must a PyTorch custom transform implement to be used in torchvision transforms?
It must implement the __call__ method, which allows the transform to be called like a function on the input data.
Click to reveal answer
intermediate
How can you combine a custom transform with other transforms in PyTorch?
You can include your custom transform class inside torchvision.transforms.Compose along with other transforms to create a pipeline that applies all transformations sequentially.
Click to reveal answer
What is the purpose of the __call__ method in a PyTorch custom transform?
ATo save the transform to disk
BTo make the transform callable like a function
CTo initialize the transform parameters
DTo visualize the transform effect
✗ Incorrect
The __call__ method allows the transform object to be used like a function, applying the transformation to input data.
Which PyTorch module is commonly used to combine multiple transforms including custom ones?
Atorch.optim.Adam
Btorch.nn.Sequential
Ctorch.utils.data.DataLoader
Dtorchvision.transforms.Compose
✗ Incorrect
torchvision.transforms.Compose chains multiple transforms together, including custom transforms.
Why might you write a custom transform instead of using a built-in one?
ATo apply a unique data modification not available in built-in transforms
BTo change the model architecture
CTo increase model size
DTo reduce training time
✗ Incorrect
Custom transforms allow you to apply specific data changes that built-in transforms do not support.
What type of input does a PyTorch custom transform usually take?
AModel weights
BTraining labels
CData samples like images or tensors
DOptimizer parameters
✗ Incorrect
Transforms operate on data samples such as images or tensors before they are fed into the model.
How do you apply a custom transform to a dataset in PyTorch?
APass it as the transform argument when creating the dataset
BAdd it to the optimizer
CInclude it in the model definition
DUse it to initialize the DataLoader
✗ Incorrect
You pass the custom transform to the dataset’s transform parameter so it applies to each data sample.
Explain how to create and use a custom transform in PyTorch for image data.
Think about making a callable class and integrating it with existing transform pipelines.
You got /4 concepts.
Describe why custom transforms are important in machine learning workflows.
Consider how data preparation affects model performance.
You got /4 concepts.
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
Step 1: Understand what transforms do
Transforms change input data before training to help the model learn better.
Step 2: Identify the role of custom transforms
Custom transforms let you add new ways to change data that standard tools don't have.
Final Answer:
To apply a specific data change not available in standard transforms -> Option D
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
Step 1: Recall custom transform structure
Custom transforms are classes with a __call__ method to make them callable.
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.
Final Answer:
class MyTransform: def __call__(self, x): return x * 2 -> Option B
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
Step 1: Understand the transform class behavior
The __call__ method adds 5 to the input x.
Step 2: Calculate transform(3)
Input 3 plus 5 equals 8.
Final Answer:
8 -> Option A
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
Step 1: Check method name for callable class
To make an instance callable, method must be __call__, not call.
Step 2: Verify other parts
Class inheritance is optional for transforms; return is present; calling instance is valid.
Final Answer:
Method should be named __call__, not call -> Option A
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
Step 1: Recall normalization formula
Normalization means subtract mean and divide by standard deviation.