0
0
PyTorchml~20 mins

nn.MaxPool2d and nn.AvgPool2d in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pooling Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of MaxPool2d with stride and kernel size
What is the output tensor shape after applying nn.MaxPool2d with kernel_size=2 and stride=2 on a tensor of shape (1, 1, 4, 4)?
PyTorch
import torch
import torch.nn as nn

input_tensor = torch.arange(16, dtype=torch.float32).reshape(1, 1, 4, 4)
pool = nn.MaxPool2d(kernel_size=2, stride=2)
output = pool(input_tensor)
output.shape
Atorch.Size([1, 1, 2, 2])
Btorch.Size([1, 1, 3, 3])
Ctorch.Size([1, 1, 4, 4])
Dtorch.Size([1, 1, 1, 1])
Attempts:
2 left
💡 Hint
MaxPool2d reduces spatial dimensions by sliding a window and taking the max value.
Predict Output
intermediate
2:00remaining
Output values of AvgPool2d on a simple tensor
What is the output tensor after applying nn.AvgPool2d with kernel_size=2 and stride=2 on the following tensor? [[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]]
PyTorch
import torch
import torch.nn as nn

input_tensor = torch.tensor([[[[1, 2, 3, 4],
                               [5, 6, 7, 8],
                               [9, 10, 11, 12],
                               [13, 14, 15, 16]]]], dtype=torch.float32)
pool = nn.AvgPool2d(kernel_size=2, stride=2)
output = pool(input_tensor)
output
A[[[[3.5, 5.5], [11.5, 13.5]]]]
B[[[[4.0, 6.0], [12.0, 14.0]]]]
C[[[[1.5, 3.5], [9.5, 11.5]]]]
D[[[[6.0, 7.0], [14.0, 15.0]]]]
Attempts:
2 left
💡 Hint
Average pooling computes the mean of each 2x2 block.
Model Choice
advanced
2:00remaining
Choosing pooling layer for noise reduction
You want to reduce noise in image features while keeping the strongest signals. Which pooling layer is best?
Ann.AvgPool2d with kernel_size=1 to keep original features
Bnn.MaxPool2d with stride=1 to keep all details
Cnn.MaxPool2d because it keeps the strongest activation in each region
Dnn.AvgPool2d because it smooths out activations by averaging
Attempts:
2 left
💡 Hint
Think about which pooling keeps the highest values.
Hyperparameter
advanced
2:00remaining
Effect of stride on output size in MaxPool2d
If you apply nn.MaxPool2d with kernel_size=3 and stride=1 on an input tensor of shape (1, 1, 7, 7), what will be the output shape?
Atorch.Size([1, 1, 3, 3])
Btorch.Size([1, 1, 4, 4])
Ctorch.Size([1, 1, 7, 7])
Dtorch.Size([1, 1, 5, 5])
Attempts:
2 left
💡 Hint
Output size = (input_size - kernel_size) / stride + 1
🔧 Debug
expert
3:00remaining
Identifying error in pooling layer usage
What error will this code raise? import torch import torch.nn as nn input_tensor = torch.randn(1, 3, 32, 32) pool = nn.AvgPool2d(kernel_size=5, stride=2, padding=3) output = pool(input_tensor)
PyTorch
import torch
import torch.nn as nn

input_tensor = torch.randn(1, 3, 32, 32)
pool = nn.AvgPool2d(kernel_size=5, stride=2, padding=3)
output = pool(input_tensor)
ANo error, runs successfully
BRuntimeError: padding must be less than or equal to half of kernel size
CTypeError: padding argument must be int or tuple
DValueError: stride must be less than kernel size
Attempts:
2 left
💡 Hint
Check the relationship between padding and kernel size in pooling layers.