Function max_pool2d

Source
pub fn max_pool2d<F>(
    input: &ArrayView4<'_, F>,
    pool_size: (usize, usize),
    stride: (usize, usize),
    padding: (usize, usize),
) -> LinalgResult<(Array4<F>, Array4<usize>)>
Expand description

Perform max pooling operation on a 4D input tensor

Applies max pooling over a 4D tensor, which is commonly used to down-sample feature maps in convolutional neural networks.

§Arguments

  • input - Input tensor of shape (batch_size, channels, height, width)
  • pool_size - Size of the pooling window as (pool_height, pool_width)
  • stride - Stride as (stride_height, stride_width)
  • padding - Padding as (padding_height, padding_width)

§Returns

  • Output tensor of pooled values and indices of max values (for backward pass)

§Examples

use ndarray::Array4;
use scirs2_linalg::convolution::max_pool2d;

// Create a 1x1x4x4 input tensor
let mut input = Array4::<f32>::zeros((1, 1, 4, 4));
// Fill with sample data
for h in 0..4 {
    for w in 0..4 {
        input[[0, 0, h, w]] = (h * 4 + w) as f32;
    }
}

// Apply 2x2 max pooling with stride 2
let (output, indices) = max_pool2d(&input.view(), (2, 2), (2, 2), (0, 0)).unwrap();

// Resulting tensor has shape (1, 1, 2, 2)
assert_eq!(output.shape(), &[1, 1, 2, 2]);