天天看点

python interpreter 中没有torch_关于python的卷积函数总结(一)

本专业使用了大量的卷积运算,最近学习python,python里面的库比较多,不同的库中有不同的运算,现在将一维的总结如下,之后累计可能更新。

2010年1月16

对比的函数如下:

---------------------------------------------------------------------

numpy库: numpy.convolve

---------------------------------------------------------------------

scipy库:scipy.signal.convolve、scipy.signal.fftconvolve、scipy.ndimage.convolve、

---------------------------------------------------------------------

cupy库:cupyx.scipy.ndimage.convolve

---------------------------------------------------------------------

torch库:torch.nn.conv1d、torch.nn.functional.conv1d

一维卷积测试:

1、为了对比方便,分别将他们重新命名

python interpreter 中没有torch_关于python的卷积函数总结(一)

2、输入整型数组

输入整型的原因是:对于所有的卷积操作,浮点型都可以使用,但是整型不一定,我们后续可以看到,所以使用整型来进行测试

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

3、各种函数开始探索

1 一维数组

1)使用numpy自带的卷积函数

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

2)使用scipy.ndimage自带的卷积函数

python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

对于cupy数据,同样的接口

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

3)使用scipy.signal自带的卷积函数

python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

可以看到fft返回值是浮点数

2 三维数组

转化为三维数组,查看运算

python interpreter 中没有torch_关于python的卷积函数总结(一)

1)使用numpy自带的卷积函数

不执行

python interpreter 中没有torch_关于python的卷积函数总结(一)

也就是说numpy.convolve函数,输入必须是一维数组

2)使用scipy.ndimage自带的卷积函数

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

对于cupy数据,同样的接口

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

3)使用scipy.signal自带的卷积函数

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

可以得到结果,但是有warning,我们来看下:

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result. return x[reverse].conj()

不建议使用非元组序列进行多维索引

;使用“arr[tuple(seq)]”而不是“arr[seq]”。在未来这将被解释为一个数组索引,' arr[np.array(seq)] ',这将导致一个错误或一个不同的结果。

而在使用fft的时候却没有问题

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

3 转化为torch

torch要求权重至少是三维,否则报错

python interpreter 中没有torch_关于python的卷积函数总结(一)

torch不支持浮点数计算

python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下

python interpreter 中没有torch_关于python的卷积函数总结(一)

1)使用torch.nn.functional的一维卷积conv1d

python interpreter 中没有torch_关于python的卷积函数总结(一)

由于卷积的长度要确定padding,所以padding确定如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

卷积如下

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

我们可以看到结果与之前的不同,因为torch中的滤波器不会自动翻转,所以需要手工翻转

python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)

反转之后

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

我们看到和之前的结果一样

也可以让输入矩阵翻转在交换输入矩阵和滤波器的位置:

python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)

2)使用torch.nn的一维卷积Conv1d

python interpreter 中没有torch_关于python的卷积函数总结(一)
python interpreter 中没有torch_关于python的卷积函数总结(一)

结果如下:

python interpreter 中没有torch_关于python的卷积函数总结(一)