Bird
Raised Fist0
PyTorchml~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main difference between nn.MaxPool2d and nn.AvgPool2d in PyTorch?
easy
A. nn.MaxPool2d selects the maximum value in each window, while nn.AvgPool2d computes the average value.
B. nn.MaxPool2d computes the average value, while nn.AvgPool2d selects the maximum value.
C. Both perform the same operation but on different input shapes.
D. nn.MaxPool2d increases data size, nn.AvgPool2d decreases it.

Solution

  1. Step 1: Understand pooling operations

    nn.MaxPool2d picks the highest value in each sliding window, emphasizing strong features. nn.AvgPool2d calculates the average, smoothing the features.
  2. Step 2: Compare their behavior

    Max pooling keeps the strongest signals, while average pooling provides a smoothed summary of the window.
  3. Final Answer:

    nn.MaxPool2d selects the maximum value in each window, while nn.AvgPool2d computes the average value. -> Option A
  4. Quick Check:

    MaxPool2d = max, AvgPool2d = average [OK]
Hint: MaxPool picks max; AvgPool averages values [OK]
Common Mistakes:
  • Confusing max and average operations
  • Thinking both increase data size
  • Assuming they work on different input shapes
2. Which of the following is the correct way to create a 2D max pooling layer with a kernel size of 3 and stride of 2 in PyTorch?
easy
A. nn.AvgPool2d(kernel=3, stride=2)
B. nn.MaxPool2d(kernel_size=3, stride=2)
C. nn.MaxPool2d(stride=3, kernel_size=2)
D. nn.MaxPool2d(size=3, step=2)

Solution

  1. Step 1: Check PyTorch pooling layer parameters

    The correct parameters for nn.MaxPool2d are kernel_size and stride. The order does not matter if named.
  2. Step 2: Validate each option

    nn.MaxPool2d(kernel_size=3, stride=2) uses correct parameter names and values. nn.MaxPool2d(stride=3, kernel_size=2) swaps kernel_size and stride values incorrectly. nn.AvgPool2d(kernel=3, stride=2) uses AvgPool2d instead of MaxPool2d. nn.MaxPool2d(size=3, step=2) uses invalid parameter names.
  3. Final Answer:

    nn.MaxPool2d(kernel_size=3, stride=2) -> Option B
  4. Quick Check:

    Correct params: kernel_size, stride [OK]
Hint: Use kernel_size and stride parameters exactly [OK]
Common Mistakes:
  • Using wrong parameter names like size or step
  • Confusing MaxPool2d with AvgPool2d
  • Swapping kernel_size and stride values
3. What is the output shape of the following PyTorch code snippet?
import torch
import torch.nn as nn

input_tensor = torch.randn(1, 1, 6, 6)
pool = nn.MaxPool2d(kernel_size=2, stride=2)
output = pool(input_tensor)
print(output.shape)
medium
A. torch.Size([1, 1, 2, 2])
B. torch.Size([1, 1, 6, 6])
C. torch.Size([1, 1, 4, 4])
D. torch.Size([1, 1, 3, 3])

Solution

  1. Step 1: Understand input and pooling parameters

    Input shape is (batch=1, channels=1, height=6, width=6). Kernel size and stride are both 2.
  2. Step 2: Calculate output dimensions

    Output height = floor((6 - 2) / 2) + 1 = floor(4 / 2) + 1 = 2 + 1 = 3. Similarly, output width = 3. So output shape is (1, 1, 3, 3).
  3. Final Answer:

    torch.Size([1, 1, 3, 3]) -> Option D
  4. Quick Check:

    Output size = floor((input - kernel)/stride)+1 [OK]
Hint: Output size = floor((input - kernel)/stride) + 1 [OK]
Common Mistakes:
  • Forgetting to apply floor function
  • Mixing up height and width calculations
  • Assuming output size equals input size
4. Identify the error in the following PyTorch code using nn.AvgPool2d:
import torch
import torch.nn as nn

input_tensor = torch.randn(1, 1, 5, 5)
pool = nn.AvgPool2d(kernel_size=2, stride=3)
output = pool(input_tensor)
print(output.shape)
medium
A. No error; code runs correctly
B. Kernel size must be odd
C. Stride cannot be greater than kernel size
D. Input tensor shape is invalid

Solution

  1. Step 1: Check parameter validity

    PyTorch allows stride to be different from kernel size, including stride > kernel size. Kernel size can be even or odd. Input tensor shape is valid.
  2. Step 2: Confirm code runs without error

    Running this code produces a valid output shape without errors.
  3. Final Answer:

    No error; code runs correctly -> Option A
  4. Quick Check:

    Stride can differ from kernel size [OK]
Hint: Stride can be any positive int, not limited by kernel size [OK]
Common Mistakes:
  • Assuming stride must be <= kernel size
  • Thinking kernel size must be odd
  • Believing input shape is invalid for pooling
5. You want to reduce the spatial size of a feature map from (1, 1, 10, 10) to (1, 1, 3, 3) using pooling layers. Which combination of nn.MaxPool2d or nn.AvgPool2d with kernel size and stride will achieve this output shape?
hard
A. Use nn.MaxPool2d with kernel_size=2, stride=2 twice sequentially
B. Use nn.AvgPool2d with kernel_size=4, stride=4
C. Use nn.MaxPool2d with kernel_size=3, stride=3
D. Use nn.AvgPool2d with kernel_size=5, stride=5

Solution

  1. Step 1: Calculate output size for kernel_size=3, stride=3

    Output size = floor((10 - 3)/3) + 1 = floor(7/3) + 1 = 2 + 1 = 3, matching desired size.
  2. Step 2: Check other options

    nn.AvgPool2d(kernel_size=4, stride=4): floor((10-4)/4)+1 = floor(6/4)+1 = 1 + 1 = 2 ≠ 3.
    nn.MaxPool2d(kernel_size=2, stride=2) twice: first floor((10-2)/2)+1 = 4 + 1 = 5, second floor((5-2)/2)+1 = 1 + 1 = 2 ≠ 3.
    nn.AvgPool2d(kernel_size=5, stride=5): floor((10-5)/5)+1 = 1 + 1 = 2 ≠ 3.
  3. Final Answer:

    Use nn.MaxPool2d with kernel_size=3, stride=3 -> Option C
  4. Quick Check:

    Output size = floor((input - kernel)/stride) + 1 [OK]
Hint: Output size = floor((input - kernel)/stride) + 1 [OK]
Common Mistakes:
  • Ignoring floor function in output size calculation
  • Assuming one pooling layer can't reduce to 3x3
  • Confusing stride and kernel size effects