[Python] numpy 만 사용해서 Gaussian Pooling을 할 수 있는 방법이 있을까요?(되도록이면 for문은 자제하고 싶습니다)

baeusa1의 이미지

https://stackoverflow.com/questions/60292174/how-to-perform-gaussian-pooling-on-a-2d-array-using-numpy

matrix:
array([[  20,  200,   -5,   23,  10, -50],
       [ -13,  134,  119,  100,  45, -79],
       [ 120,   32,   49,   25,  13,   0],
       [  40,   12,   59,   23,  32,  -1],
       [  75,  121,   69,   67,  64,  -7],
       [  39,   12,   79,   -8,  16,  -9]])
kernel:
array([[ 1/16, 1/8, 1/16],
       [  1/8, 1/4,  1/8],
       [ 1/16, 1/8, 1/16]])
soln:
array([[   87.25,   16.625],
       [ 64.8125,  29.8125]])

이런식으로 Gaussian Pooling 을 해보고 싶습니다.

baeusa1의 이미지

First transform you M x N matrix into a (M//K) x K x (N//K) x K array, then pointwise multiply with the kernel at the second and fourth dimensions, then sum at the second and fourth dimensions.

np.sum(
    matrix.reshape((
        matrix.shape[-2] // kernel.shape[-2], kernel.shape[-2],
        matrix.shape[-1] // kernel.shape[-1], kernel.shape[-1],
    ))
    * kernel[np.newaxis, :, np.newaxis, :],
    axis=(-3, -1),
)
You can also replace the pointwise-multiply-then-sum by a np.tensordot call.
 
np.tensordot(
    matrix.reshape((
        matrix.shape[-2] // kernel.shape[-2], kernel.shape[-2],
        matrix.shape[-1] // kernel.shape[-1], kernel.shape[-1],
    )),
    kernel,
    axes=(
        (-3, -1),
        (-2, -1),
    )
)