我试着用for循环实现2D阵列的跨步卷积,即
arr = np.array([[2,3,7,4,6,2,9],
[6,6,9,8,7,4,3],
[3,4,8,3,8,9,7],
[7,8,3,6,6,3,4],
[4,2,1,8,3,4,6],
[3,2,4,1,9,8,3],
[0,1,3,9,2,1,4]])
arr2 = np.array([[3,4,4],
[1,0,2],
[-1,0,3]])
def stride_conv(arr1,arr2,s,p):
beg = 0
end = arr2.shape[0]
final = []
for i in range(0,arr1.shape[0]-1,s):
k = []
for j in range(0,arr1.shape[0]-1,s):
k.append(np.sum(arr1[beg+i : end+i, beg+j:end+j] * (arr2)))
final.append(k)
return np.array(final)
stride_conv(arr,arr2,2,0)
这将生成3*3数组:
array([[ 91, 100, 88],
[ 69, 91, 117],
[ 44, 72, 74]])
是否有numpy函数或scipy函数来执行相同的操作?我的方法不太好。我如何将其矢量化?
忽略padding参数和后面的窗口,这些窗口的长度不足以对第二个数组进行卷积,下面是一种使用
np.lib.stride_tricks.as_strided
-或者,我们可以使用内置的scikit图像^{} 来获得那些优雅的窗口,比如-
这是一种基于O(N^d(logn)^d)fft的方法。其思想是将两个操作数在所有偏移量的模步长处分割成步长间隔的网格,在相应偏移量的网格之间进行传统的fft卷积,然后逐点求和。指数有点重,但恐怕也没办法:
结果:
从^{} 中使用^{} 怎么样?
我的方法类似于Jason的方法,但是使用索引。
注意,内核必须反转。有关详细信息,请参见讨论here和here。否则使用^{} 。
示例:
相关问题 更多 >
编程相关推荐