Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

28. Feature map dimensions

easy
GeneralGeneral
senior

Feature maps are the intermediate outputs of convolutional neural networks (CNNs), and you often need to predict their spatial dimensions to design architectures correctly. In this question, you’ll compute the output height/width of a 2D convolution feature map given input size, kernel size, stride, and padding.

The feature map size is defined as:

Hout=⌊Hin+2P−KS⌋+1,Wout=⌊Win+2P−KS⌋+1H_{out} = \left\lfloor \frac{H_{in} + 2P - K}{S} \right\rfloor + 1,\quad W_{out} = \left\lfloor \frac{W_{in} + 2P - K}{S} \right\rfloor + 1

Requirements

Implement the function

python

Rules:

  • Use the formula above with floor division behavior.
  • Return a NumPy array of shape (2,).
  • Do not use any deep learning frameworks (no PyTorch/TensorFlow).
  • You may use only NumPy and built-in Python libraries.

Example

python

Output:

python
Input Signature
ArgumentType
kint
pint
sint
h_inint
w_inint
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Return np.ndarray of shape (2,).

  • Use floor division (//).

  • No deep learning frameworks.

Hint 1

Start by writing the given formulas for both height and width; they are identical except for h_in vs w_in.

Hint 2

Use Python’s integer floor division // to match the (\lfloor\cdot\rfloor) in the formula: (h_in + 2*p - k) // s + 1.

Hint 3

Return the two computed integers as a list [h_out, w_out] and sanity-check with the example (32,32,k=3,s=1,p=1) which should return [32, 32].

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
convolution
output-dimensions
stride-padding
integer-floor-division
34 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit