天天看點

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的卷積函數總結(一)