Flatten Feature Map
Implement a function to transform a 3D feature map into a 1D vector, a crucial step in Convolutional Neural Networks (CNNs). This process enables the connection of convolutional layers to fully connected layers.
In CNNs, a feature map is a 3D array of size C×H×W, where C is the number of channels, H is the height, and W is the width. To feed this data into a fully connected layer, it must be flattened into a 1D vector.
Here are the steps to achieve this:
- Iterate through each channel in the feature map.
- For each channel, iterate through each row.
- For each row, iterate through each column, appending the values to the result vector.
This technique is widely used in image classification tasks.
Example:
flatten([[[1,2],[3,4]], [[5,6],[7,8]]])
[1,2,3,4,5,6,7,8]
- Input structure: The 3D feature map has shape C×H×W = 2×2×2, representing 2 channels, each with a 2×2 spatial grid
- Iterate through dimensions: Process channels sequentially (C=0, then C=1), and within each channel, traverse rows (H) then columns (W) in order
- Extract values: Channel 0 yields [1,2,3,4], Channel 1 yields [5,6,7,8]
- Concatenate into 1D vector: Combine all extracted values sequentially to produce the flattened output [1,2,3,4,5,6,7,8]
Constraints:
- Return flattened list in C, H, W order
1. Background Knowledge
In Convolutional Neural Networks (CNNs), convolutional layers extract hierarchical feature maps from input images. These are typically 3D tensors with dimensions C × H × W, where C is the number of channels (feature depth), H is height, and W is width. Each channel captures specific patterns like edges or textures, preserving spatial relationships critical for tasks like object detection.
Flattening transforms this 3D structure into a 1D vector of size C × H × W, enabling connection to fully connected (FC) layers for classification. This bridge is essential in classic CNN architectures (e.g., AlexNet, VGG), as FC layers perform global reasoning but require fixed-size inputs. Modern alternatives like Global Average Pooling reduce parameters, but flattening remains fundamental for understanding CNN-FC transitions.
2. Algorithm/Approach
The core pattern is tensor reshaping: treat the 3D feature map as a contiguous block of elements and collapse spatial (H, W) and channel (C) dimensions into a single vector. This is a view operation in frameworks like PyTorch or TensorFlow, preserving all values without copying data.
# Conceptual PyTorch pattern (not exact solution)
feature_map = torch.randn(C, H, W) # 3D input
flattened = feature_map.view(-1) # Shape: (C*H*W,)
Maintain memory layout (e.g., channel-major or row-major) to ensure correct indexing for downstream FC weights.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.