Ask Your Question
2

What is the method for executing max/mean pooling on a 2-dimensional array with the use of numpy?

asked 2023-01-24 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-12-09 00:00:00 +0000

bukephalos gravatar image

The method for executing max/mean pooling on a 2-dimensional array using numpy is:

  1. Import the numpy library:
import numpy as np
  1. Create a 2-dimensional array:
array = np.array([[1, 2, 3, 4],
                  [5, 6, 7, 8],
                  [9, 10, 11, 12],
                  [13, 14, 15, 16]])
  1. Define the pooling parameters:
pool_size = (2, 2) # Pooling window size
stride = 2 # Stride length
  1. Execute max/mean pooling:
max_pooled = np.max(pooling_array(array, pool_size, stride), axis=(2, 3))
mean_pooled = np.mean(pooling_array(array, pool_size, stride), axis=(2, 3))

Note: pooling_array is a function that returns a padded and reshaped array with the same dimensions as array and is not built-in with numpy. It can be defined as follows:

def pooling_array(array, pool_size, stride):
    # Pad array with zeros to handle border conditions
    array = np.pad(array, ((0, 0), (0, 1), (0, 1)), mode='constant')
    # Reshape array to handle window sizes
    shape = ((array.shape[0] - pool_size[0]) // stride + 1,
             (array.shape[1] - pool_size[1]) // stride + 1,
             pool_size[0], pool_size[1])
    strides = (array.strides[0] * stride, array.strides[1] * stride,
               array.strides[0], array.strides[1])
    return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-24 11:00:00 +0000

Seen: 7 times

Last updated: Dec 09 '22