Это сверточное задание.Вход:
a = np.array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
conv_filter = np.array([[1,1,1],
[1,1,1],
[1,1,1]])
Код:
import numpy as np
def conv2d(a, f):
b = np.zeros([a.shape[0]+int(f.shape[0]/2)*2,a.shape[1]+int(f.shape[0]/2)*2])
for i in range(1,b.shape[0]-int(f.shape[0]/2)):
for j in range(1,b.shape[1]-int(f.shape[0]/2)):
b[i][j] = a[i-1][j-1]
s = f.shape + tuple(np.subtract(b.shape, f.shape) + 1)
strd = np.lib.stride_tricks.as_strided
subM = strd(b, shape = s, strides = b.strides * 2)
return np.einsum('ij,ijkl->kl', f, subM)
conv2d(a,conv_filter)
Выход:
array([[ 12., 21., 27., 33., 24.],
[ 33., 54., 63., 72., 51.],
[ 63., 99., 108., 117., 81.],
[ 93., 144., 153., 162., 111.],
[ 72., 111., 117., 123., 84.]])