0
0
PyTorchml~20 mins

CNN architecture for image classification in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CNN Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape after convolution and pooling
Given the following PyTorch CNN layer definitions, what is the output shape after passing a batch of 16 RGB images of size 64x64 through the layers?
PyTorch
import torch
import torch.nn as nn

conv = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, stride=1, padding=1)
pool = nn.MaxPool2d(kernel_size=2, stride=2)

x = torch.randn(16, 3, 64, 64)
x = conv(x)
x = pool(x)
output_shape = x.shape
A(16, 8, 31, 31)
B(16, 3, 32, 32)
C(16, 8, 32, 32)
D(16, 8, 64, 64)
Attempts:
2 left
💡 Hint
Remember that padding keeps the spatial size same before pooling, and pooling halves the height and width.
Model Choice
intermediate
2:00remaining
Choosing the correct CNN layer sequence
Which of the following PyTorch CNN layer sequences correctly applies two convolutional layers followed by a max pooling layer?
Ann.Sequential(nn.Conv2d(3, 16, 3), nn.Conv2d(32, 16, 3), nn.MaxPool2d(2))
Bnn.Sequential(nn.MaxPool2d(2), nn.Conv2d(3, 16, 3), nn.Conv2d(16, 32, 3))
Cnn.Sequential(nn.Conv2d(3, 16, 3), nn.MaxPool2d(2), nn.Conv2d(16, 32, 3))
Dnn.Sequential(nn.Conv2d(3, 16, 3, padding=1), nn.Conv2d(16, 32, 3, padding=1), nn.MaxPool2d(2))
Attempts:
2 left
💡 Hint
Check the order of layers and the input/output channels for each convolution.
Hyperparameter
advanced
2:00remaining
Effect of kernel size on output dimension
If a convolutional layer has input size 28x28, stride=1, padding=0, and kernel_size=5, what will be the output spatial size?
A24x24
B28x28
C23x23
D25x25
Attempts:
2 left
💡 Hint
Use the formula: output = (input - kernel + 2*padding)/stride + 1
Metrics
advanced
2:00remaining
Interpreting CNN training accuracy and loss
During CNN training for image classification, if the training loss decreases steadily but the validation accuracy plateaus early, what does this indicate?
AThe model is overfitting the training data
BThe model is underfitting both training and validation data
CThe model is perfectly generalized
DThe training data is too small
Attempts:
2 left
💡 Hint
Think about what it means when training improves but validation does not.
🔧 Debug
expert
3:00remaining
Identifying error in CNN forward pass
What error will this PyTorch CNN forward method raise when given input of shape (10, 3, 32, 32)?
PyTorch
import torch
import torch.nn as nn

class SimpleCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, 3)
        self.pool = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(16 * 15 * 15, 10)

    def forward(self, x):
        x = self.pool(self.conv1(x))
        x = x.view(-1, 16 * 15 * 15)
        x = self.fc1(x)
        return x

model = SimpleCNN()
input_tensor = torch.randn(10, 3, 32, 32)
output = model(input_tensor)
AValueError: input tensor has wrong number of channels
BRuntimeError: shape mismatch in view
CTypeError: conv1d expected 3D input
DNo error, output shape is (10, 10)
Attempts:
2 left
💡 Hint
Check the size after conv and pooling before reshaping with view.