def convolve1d_2D_numpy(a, b, mode='full'):
nwords, ndim = a.shape
filter_width, ndim = b.shape
b = np.flipud(b) # flip the kernel
if mode == 'full':
pad = np.zeros((filter_width-1, ndim))
a = np.vstack([pad, a, pad])
shape = (nwords+filter_width-1, filter_width, ndim)
elif mode == 'valid':
shape = (nwords-filter_width+1, filter_width, ndim)
strides = (a.strides[0],) + a.strides
view = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
conv_out = np.einsum('kij,ij->kj', view, b)
return conv_out
评论列表
文章目录