Click4Ai

118.

Easy

Stride Configuration

Stride controls how the convolutional kernel moves across the input tensor. A stride of 1 means the kernel shifts one position at a time; a stride of 2 means it skips every other position. Larger strides produce smaller output feature maps, effectively performing downsampling during convolution.

Formula:

output_height = (input_height - kernel_height) / stride + 1

output_width = (input_width - kernel_width) / stride + 1

For each output position (i, j):

output[i, j] = sum(input[i*stride : i*stride+kH, j*stride : j*stride+kW] * kernel)

Example:

Input (4x4): Kernel (2x2): Stride = 2

[[1, 2, 3, 4], [[1, 1],

[5, 6, 7, 8], [1, 1]]

[9, 10, 11, 12],

[13, 14, 15, 16]]

output_height = (4 - 2) / 2 + 1 = 2

output_width = (4 - 2) / 2 + 1 = 2

Position (0,0): sum([[1,2],[5,6]] * kernel) = 1+2+5+6 = 14

Position (0,1): sum([[3,4],[7,8]] * kernel) = 3+4+7+8 = 22

Position (1,0): sum([[9,10],[13,14]] * kernel) = 46

Position (1,1): sum([[11,12],[15,16]] * kernel) = 54

Output: [[14, 22],

[46, 54]]

Strided convolutions are often used as an alternative to pooling layers for spatial downsampling. They reduce spatial dimensions while learning the downsampling parameters, unlike fixed pooling operations.

Constraints:

  • Input tensor is a 2D numpy array of shape (height, width)
  • Kernel is a 2D numpy array of shape (kernel_height, kernel_width)
  • Stride is a positive integer
  • Output dimensions must be valid integers (no partial windows)
  • Test Cases

    Test Case 1
    Input: [[1, 2], [3, 4]], [[5, 6], [7, 8]], 1
    Expected: [[19, 22], [43, 50]]
    Test Case 2
    Input: [[1, 2], [3, 4]], [[5, 6], [7, 8]], 2
    Expected: [[19]]
    + 3 hidden test cases