PIXELBANKv9.1.0
Menu

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×WC \times H \times W, where CC is the number of channels, HH is the height, and WW 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:

  1. Iterate through each channel in the feature map.
  2. For each channel, iterate through each row.
  3. For each row, iterate through each column, appending the values to the result vector.
Flattened Vector=[x1,1,1,x1,1,2,...,xC,H,W]\text{Flattened Vector} = [x_{1,1,1}, x_{1,1,2},..., x_{C,H,W}]

This technique is widely used in image classification tasks.

Example:

Input:
flatten([[[1,2],[3,4]], [[5,6],[7,8]]])
Output:
[1,2,3,4,5,6,7,8]
Reasoning:
  • Input structure: The 3D feature map has shape C×H×WC \times H \times W = 2×2×22 \times 2 \times 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
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
Flatten Feature Map - Easy | PixelBank