liveBook · Manning

Stride

As explained in Grokking AI Algorithms, Second Edition

A stride is the spatial step size by which a convolutional filter moves across an input.

A stride is the number of pixels a convolutional filter jumps each time it moves across an input. A stride of 1 checks every neighboring position; a stride of 2 skips one pixel between positions. section 12.17

Halving a feature map

A downsampling layer uses a 4×4 filter with stride 2 on an 8×8 feature map. The filter jumps two pixels at a time, producing a 4×4 output and a more compressed summary of the image. Padding adds a border of blank values to help preserve edge information. section 12.17

Figure 12.35 How filters scan an image with padding
12_35

Trace how a padded filter scans an image and how stride changes the filter’s successive positions. section 12.17

def apply_padding_3d(tensor, padding): #A
    return np.pad(tensor, ((0, 0), (padding, padding),
    ➥(padding, padding)), mode='constant', constant_values=0)
 
def downsample_convolution(feature_maps_in, num_output_channels):
    input_channels = feature_maps_in.shape[0]
    input_height = feature_maps_in.shape[1]
    filter_size = 4
    stride = 2
    padding = 1
 
    padded_maps = apply_padding_3d(feature_maps_in, padding)
 
    output_height = (input_height + 2 * padding - filter_size)
    ➥// stride + 1 #B
    output_width = output_height
 
    filters = np.random.randn(num_output_channels,
    ➥input_channels, filter_size, filter_size) * 0.01 #C
    biases = np.random.randn(num_output_channels) * 0.01
 
    feature_maps_out = []
 
    for k in range(num_output_channels):
        current_bias = biases[k]
        output_map = np.zeros((output_height, output_width))
 
        for i in range(output_height):
            for j in range(output_width):
 
                accumulation_sum = 0
 
                for c in range(input_channels):
 
                    current_filter = filters[k, c]
 
                    r_start, c_start = i * stride, j * stride
                    region = padded_maps[c, r_start : r_start +
                    ➥filter_size, c_start : c_start + filter_size] #D
 
                    accumulation_sum += np.sum(region * current_filter)
 
                output_map[i, j] =
                ➥accumulation_sum + current_bias #E
 
        feature_maps_out.append(output_map)
 
    h2 = np.stack(feature_maps_out, axis=0)
 
    return h2

Inspect a NumPy-style downsampling convolution where stride, padding, filter size, and output dimensions are configured explicitly. section 12.17

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Explains filter movement, padding, and output-size calculation through an 8×8-to-4×4 example and implementation.
Examples
Uses a 4×4 filter with stride 2 to reduce eight 8×8 feature maps to 4×4 maps.
What it assumes
Assumes convolutional feature maps and introduces stride alongside padding in a downsampling layer.
What you can do after
Build it from scratch by configuring stride and calculating output dimensions in a downsampling convolution.
How it connects
Links stride to convolution, padding, feature maps, and downsampling.

Also explained in

Compare how 8 books explain stride →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.