Bird
Raised Fist0
PyTorchml~20 mins

Feature map visualization 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
🎖️
Feature Map Visualization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape of feature maps after convolution
Consider a PyTorch convolutional layer defined as conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1). If the input tensor has shape (1, 3, 64, 64), what will be the shape of the output feature map after applying conv?
PyTorch
import torch
import torch.nn as nn
conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 64, 64)
output = conv(input_tensor)
print(output.shape)
Atorch.Size([1, 16, 62, 62])
Btorch.Size([1, 3, 64, 64])
Ctorch.Size([1, 16, 64, 64])
Dtorch.Size([1, 16, 66, 66])
Attempts:
2 left
💡 Hint
Remember that padding=1 keeps the spatial size same when kernel_size=3 and stride=1.
🧠 Conceptual
intermediate
1:30remaining
Purpose of feature map visualization in CNNs
Why do we visualize feature maps in convolutional neural networks (CNNs)?
ATo understand what patterns or features the network is detecting at different layers
BTo reduce the size of the dataset before training
CTo increase the number of parameters in the model
DTo convert images into grayscale
Attempts:
2 left
💡 Hint
Think about what feature maps represent inside a CNN.
Metrics
advanced
1:30remaining
Interpreting feature map activation statistics
After visualizing feature maps from a CNN layer, you notice most activations are near zero except a few very high values. What does this indicate about the layer's behavior?
AThe input images are all black
BThe layer is overfitting the training data
CThe layer has a bug causing all zeros
DThe layer is sparsely activating, focusing on specific features
Attempts:
2 left
💡 Hint
Sparse activations mean only some neurons respond strongly.
🔧 Debug
advanced
2:00remaining
Why does feature map visualization show a blank image?
You wrote code to visualize feature maps from a CNN layer but the output images are all black. Which of the following is the most likely cause?
PyTorch
import matplotlib.pyplot as plt
import torch

# feature_maps is a tensor of shape (1, 16, 64, 64)
feature_maps = torch.randn(1, 16, 64, 64) * 0.01

plt.imshow(feature_maps[0, 0].detach().numpy())
plt.show()
AThe feature map values are too small and not normalized for visualization
BMatplotlib cannot plot tensors directly
CThe tensor shape is incorrect for visualization
DThe feature maps are empty tensors
Attempts:
2 left
💡 Hint
Check the range of values before plotting images.
Model Choice
expert
2:30remaining
Choosing a layer for meaningful feature map visualization
You want to visualize feature maps that show clear edges and textures from an image classification CNN. Which layer is best to choose?
AThe final fully connected layer
BAn early convolutional layer close to the input
CA batch normalization layer
DThe output softmax layer
Attempts:
2 left
💡 Hint
Early layers detect simple features like edges.

Practice

(1/5)
1. What is a feature map in the context of convolutional neural networks (CNNs)?
easy
A. The loss value during training
B. The input image to the CNN
C. The final prediction of the model
D. The output of a convolutional layer showing detected patterns

Solution

  1. Step 1: Understand CNN layer outputs

    Convolutional layers process input images and produce outputs called feature maps that highlight detected features.
  2. Step 2: Identify feature map role

    Feature maps represent learned patterns like edges or textures, not inputs or final outputs.
  3. Final Answer:

    The output of a convolutional layer showing detected patterns -> Option D
  4. Quick Check:

    Feature map = convolution output [OK]
Hint: Feature maps are outputs of conv layers, not inputs or predictions [OK]
Common Mistakes:
  • Confusing feature maps with input images
  • Thinking feature maps are final model outputs
  • Mixing feature maps with loss values
2. Which PyTorch code snippet correctly extracts feature maps from a convolutional layer named conv1 given an input tensor x?
easy
A. feature_maps = x.conv1()
B. feature_maps = conv1(x)
C. feature_maps = conv1.forward()
D. feature_maps = conv1.output(x)

Solution

  1. Step 1: Understand PyTorch layer call

    In PyTorch, calling a layer like a function with input tensor returns its output (feature maps).
  2. Step 2: Check syntax correctness

    Using conv1(x) is correct; x.conv1() or conv1.output(x) are invalid syntax.
  3. Final Answer:

    feature_maps = conv1(x) -> Option B
  4. Quick Check:

    Call layer as function = correct [OK]
Hint: Call conv layer like a function with input tensor [OK]
Common Mistakes:
  • Trying to call layer as method on input tensor
  • Using non-existent methods like .output()
  • Calling forward() directly instead of layer call
3. Given the following PyTorch code, what is the shape of feature_maps?
import torch
import torch.nn as nn
conv = nn.Conv2d(in_channels=3, out_channels=5, kernel_size=3, padding=1)
x = torch.randn(1, 3, 32, 32)
feature_maps = conv(x)
medium
A. [1, 3, 32, 32]
B. [1, 5, 30, 30]
C. [1, 5, 32, 32]
D. [3, 5, 32, 32]

Solution

  1. Step 1: Analyze conv layer parameters

    Input has shape [1, 3, 32, 32]. Conv2d has 5 output channels, kernel size 3, padding 1.
  2. Step 2: Calculate output spatial size

    Padding 1 keeps spatial size same: 32x32. Output channels = 5, batch size = 1.
  3. Final Answer:

    [1, 5, 32, 32] -> Option C
  4. Quick Check:

    Output shape = [batch, out_channels, height, width] [OK]
Hint: Padding keeps size; output channels define depth [OK]
Common Mistakes:
  • Ignoring padding effect on output size
  • Confusing input channels with output channels
  • Mixing batch size with channel dimension
4. You try to visualize feature maps using this code but get an error:
import matplotlib.pyplot as plt
feature_maps = conv(x)
plt.imshow(feature_maps[0])
plt.show()
What is the likely cause of the error?
medium
A. feature_maps[0] has multiple channels, plt.imshow expects 2D or 3D image
B. conv(x) returns a scalar, not a tensor
C. plt.imshow cannot display tensors
D. x is not defined before conv(x)

Solution

  1. Step 1: Understand feature_maps shape

    feature_maps[0] is shape [channels, height, width], multiple channels not a single image.
  2. Step 2: plt.imshow expects 2D or 3D image

    plt.imshow needs 2D grayscale or 3D RGB image, but feature_maps[0] has multiple channels causing error.
  3. Final Answer:

    feature_maps[0] has multiple channels, plt.imshow expects 2D or 3D image -> Option A
  4. Quick Check:

    Multi-channel tensor ≠ single image [OK]
Hint: Plot one channel slice, not full multi-channel tensor [OK]
Common Mistakes:
  • Trying to plot all channels at once with plt.imshow
  • Assuming conv output is scalar
  • Not checking input tensor existence
5. You want to visualize all feature maps from a convolutional layer conv for a single input image x. Which code correctly plots each channel as a separate grayscale image using matplotlib?
hard
A. feature_maps = conv(x) for i in range(feature_maps.shape[1]): plt.subplot(1, feature_maps.shape[1], i+1) plt.imshow(feature_maps[0, i].detach().cpu(), cmap='gray') plt.show()
B. feature_maps = conv(x) plt.imshow(feature_maps.detach().cpu(), cmap='gray') plt.show()
C. feature_maps = conv(x) for i in range(feature_maps.shape[0]): plt.imshow(feature_maps[i].detach().cpu(), cmap='gray') plt.show()
D. feature_maps = conv(x) plt.imshow(feature_maps[0].detach().cpu()) plt.show()

Solution

  1. Step 1: Extract feature maps and iterate channels

    feature_maps shape is [batch, channels, height, width]. We select batch 0 and loop over channels.
  2. Step 2: Plot each channel as grayscale image

    Use plt.subplot to arrange images, plt.imshow with cmap='gray' to show each channel properly.
  3. Final Answer:

    feature_maps = conv(x) for i in range(feature_maps.shape[1]): plt.subplot(1, feature_maps.shape[1], i+1) plt.imshow(feature_maps[0, i].detach().cpu(), cmap='gray') plt.show() -> Option A
  4. Quick Check:

    Loop channels, plot each with cmap='gray' [OK]
Hint: Loop channels, plot each with grayscale colormap [OK]
Common Mistakes:
  • Plotting entire tensor at once
  • Not detaching or moving tensor to CPU
  • Ignoring batch dimension