本专业使用了大量的卷积运算,最近学习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、为了对比方便,分别将他们重新命名
![v2-cba972b556cd9cd37388e6c19e056933_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-cba972b556cd9cd37388e6c19e056933_b.jpg)
2、输入整型数组
输入整型的原因是:对于所有的卷积操作,浮点型都可以使用,但是整型不一定,我们后续可以看到,所以使用整型来进行测试
![v2-94aea4e3aaa9acaae38efde8f42c4571_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-94aea4e3aaa9acaae38efde8f42c4571_b.png)
结果如下:
![v2-77c1b2cbbda2dac68391958d87be7600_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-77c1b2cbbda2dac68391958d87be7600_b.png)
3、各种函数开始探索
1 一维数组
1)使用numpy自带的卷积函数
结果如下:
![v2-318d87a071dbc73aed90e7afc5d408eb_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-318d87a071dbc73aed90e7afc5d408eb_b.png)
2)使用scipy.ndimage自带的卷积函数
![v2-ac69f60785e12412b3c5880dd4509ffb_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-ac69f60785e12412b3c5880dd4509ffb_b.jpg)
![v2-e87c26362dba3dcb4c4f87b2c69f573c_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-e87c26362dba3dcb4c4f87b2c69f573c_b.jpg)
![v2-1e1e7be4a7bb08a80c94db7b7a2864be_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-1e1e7be4a7bb08a80c94db7b7a2864be_b.png)
结果如下:
![v2-f9ff3627f7265b30e853a137f999f77c_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-f9ff3627f7265b30e853a137f999f77c_b.png)
对于cupy数据,同样的接口
![v2-e6c88158fafc47642e9eb1a152fc82f2_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-e6c88158fafc47642e9eb1a152fc82f2_b.png)
结果如下:
![v2-634c77bc4954c01279dfb5cf81e42d65_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-634c77bc4954c01279dfb5cf81e42d65_b.png)
3)使用scipy.signal自带的卷积函数
![v2-671de43d12612b4cdc52a6d43c8a36f2_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-671de43d12612b4cdc52a6d43c8a36f2_b.jpg)
![v2-5a2af2ad08a774a673dc07c62cf35b73_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-5a2af2ad08a774a673dc07c62cf35b73_b.jpg)
![v2-e209c5342ab61ffb34152e3cbb8e2e36_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-e209c5342ab61ffb34152e3cbb8e2e36_b.png)
结果如下:
![v2-5cedf484f0d213af1c20ea380c60fd63_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-5cedf484f0d213af1c20ea380c60fd63_b.jpg)
可以看到fft返回值是浮点数
2 三维数组
转化为三维数组,查看运算
![v2-06f77dae280f099aa42ab28696d7abea_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-06f77dae280f099aa42ab28696d7abea_b.png)
1)使用numpy自带的卷积函数
不执行
![v2-999d1cb0be21d1dad82fa681365139a9_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-999d1cb0be21d1dad82fa681365139a9_b.png)
也就是说numpy.convolve函数,输入必须是一维数组
2)使用scipy.ndimage自带的卷积函数
![v2-286f00c2a17324fffe84258e14afc910_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-286f00c2a17324fffe84258e14afc910_b.png)
结果如下:
![v2-55d7d8670aafc775ca0728d138244307_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-55d7d8670aafc775ca0728d138244307_b.png)
对于cupy数据,同样的接口
![v2-4efba6f94f304c5e3efcd3f035ae629d_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-4efba6f94f304c5e3efcd3f035ae629d_b.png)
结果如下:
![v2-925090a8c542b3d6a38072282f5abbda_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-925090a8c542b3d6a38072282f5abbda_b.png)
3)使用scipy.signal自带的卷积函数
![v2-25f7e6370608acdad2beed9a24180670_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-25f7e6370608acdad2beed9a24180670_b.png)
结果如下:
![v2-4cf5d159866a681391847b83d02d586c_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-4cf5d159866a681391847b83d02d586c_b.png)
可以得到结果,但是有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的时候却没有问题
![v2-e5683d23fd7fbbcb7d1703fb1d1bdef7_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-e5683d23fd7fbbcb7d1703fb1d1bdef7_b.png)
结果如下:
![v2-73762cc44cea741c7ac92770d3b90954_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-73762cc44cea741c7ac92770d3b90954_b.png)
3 转化为torch
torch要求权重至少是三维,否则报错
![v2-ae29783a0ca59ef934f2d400a1b1a6b0_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-ae29783a0ca59ef934f2d400a1b1a6b0_b.png)
torch不支持浮点数计算
![v2-413134dee0b7269bb640dfb3ccdb93c7_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-413134dee0b7269bb640dfb3ccdb93c7_b.png)
![v2-a9b0ea82e4a7266ca410b9dc96a1b646_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-a9b0ea82e4a7266ca410b9dc96a1b646_b.png)
结果如下
![v2-c9c228a10d108d394b2bce01030c9145_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-c9c228a10d108d394b2bce01030c9145_b.png)
1)使用torch.nn.functional的一维卷积conv1d
![v2-2ea56e1ca018b9a5bfb8d58aaec3a3b7_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-2ea56e1ca018b9a5bfb8d58aaec3a3b7_b.jpg)
由于卷积的长度要确定padding,所以padding确定如下:
![v2-d5caccd41df1c52f0b180fbe577cdaf6_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-d5caccd41df1c52f0b180fbe577cdaf6_b.png)
卷积如下
![v2-359e883748ac8ac242303bf16d3db841_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-359e883748ac8ac242303bf16d3db841_b.png)
结果如下:
![v2-8c866a4564c3536ab7169721aea9b9c7_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-8c866a4564c3536ab7169721aea9b9c7_b.png)
我们可以看到结果与之前的不同,因为torch中的滤波器不会自动翻转,所以需要手工翻转
![v2-7fcbcf5f616e7c3d5122f2d6ea057a33_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-7fcbcf5f616e7c3d5122f2d6ea057a33_b.png)
![v2-a1a80cc8987a54d9566a2830e794e637_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-a1a80cc8987a54d9566a2830e794e637_b.png)
反转之后
![v2-ffb1c1b716cfdbdf4efd3716d4db3c78_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-ffb1c1b716cfdbdf4efd3716d4db3c78_b.png)
结果如下:
![v2-1cdaebc028faa803b51f0242dd2b2048_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-1cdaebc028faa803b51f0242dd2b2048_b.png)
我们看到和之前的结果一样
也可以让输入矩阵翻转在交换输入矩阵和滤波器的位置:
![v2-ede412461d0e5515b224515d439c020f_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-ede412461d0e5515b224515d439c020f_b.png)
结果如下:
![v2-f19eda15dbcd57f98a6c411ee332c3da_b.png](http://img-03.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-f19eda15dbcd57f98a6c411ee332c3da_b.png)
2)使用torch.nn的一维卷积Conv1d
![v2-c335771ff5c9e13b0c2a0b6bfb191f58_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-c335771ff5c9e13b0c2a0b6bfb191f58_b.jpg)
![v2-4d57963353a9c70bcaf1cd2fb3af13ad_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-4d57963353a9c70bcaf1cd2fb3af13ad_b.png)
结果如下:
![v2-bc415c87b94d3f78628d89adfefd3f6a_b.png](http://img-02.proxy.5ce.com/view/image?&type=2&guid=81c58a58-f42f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-bc415c87b94d3f78628d89adfefd3f6a_b.png)