Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement 2D pooling (max and average) over a single-channel image, a common downsampling step in computer vision models. You’ll take an input image as a NumPy array of shape and return the pooled output as a NumPy array.
Pooling is defined over each window as:
Implement the function
Rules:
mode="max" must compute max-pooling; mode="avg" must compute average-pooling.Output:
| Argument | Type |
|---|---|
| mode | str |
| image | np.ndarray |
| stride | list |
| kernel_size | list |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Valid pooling only; no padding.
Return NumPy array.
Use NumPy + built-ins only.
Compute output shape for valid pooling: out_h = (H-k_h)//s_h + 1, out_w = (W-k_w)//s_w + 1.
For each output (i,j), slice the window with top-left (i*s_h, j*s_w) and shape (k_h,k_w); apply max or mean.
Input image is np.ndarray. Initialize output with np.zeros. Check mode is {max, avg}.