Pytorch之卷積層的使用詳解
1.簡介(torch.nn下的)
卷積層主要使用的有3類,用於處理不同維度的資料
引數 Parameters:
in_channels(int) – 輸入訊號的通道
out_channels(int) – 卷積產生的通道
kerner_size(int or tuple) - 卷積核的尺寸
stride(int or tuple,optional) - 卷積步長
padding (int or tuple,optional)- 輸入的每一條邊補充0的層數
dilation(int or tuple,`optional``) – 卷積核元素之間的間距
groups(int,optional) – 從輸入通道到輸出通道的阻塞連線數
bias(bool,optional) - 如果bias=True,新增偏置
class torch.nn.Conv1d(in_channels,out_channels,kernel_size,stride=1,padding=0,dilation=1,groups=1,bias=True)
一維卷積層。用於計算ECG等一維資料。
input: (N,C_in,L_in) N為批次,C_in即為in_channels,即一批內輸入一維資料個數,L_in是是一維資料基數
output: (N,C_out,L_out) N為批次,C_in即為out_channels,即一批內輸出一維資料個數,L_out是一維資料基數
class torch.nn.Conv2d(in_channels,bias=True)
二維卷積層。用於計算CT斷層或MR斷層,或二維超聲影象,自然影象等二維資料。
self.conv1 = nn.Conv2d( # 1*28*28 -> 32*28*28 in_channels=1,out_channels=32,kernel_size=5,padding=2 #padding是需要計算的,padding=(stride-1)/2 )
input: (N,H_in,W_in) N為批次,C_in即為in_channels,即一批內輸入二維資料個數,H_in是二維資料行數,W_in是二維資料的列數
output: (N,H_out,W_out) N為批次,C_out即為out_channels,即一批內輸出二維資料個數,H_out是二維資料行數,W_out是二維資料的列數
con2 = nn.Conv2d(1,16,5,1,2) # con2(np.empty([1,28,28])) 只能接受tensor/variable con2(torch.Tensor(1,28)) con2(Variable(torch.Tensor(1,28)))
class torch.nn.Conv3d(in_channels,bias=True)
三維卷積層。用於計算CT或MR等容積資料,視訊資料等三維資料。
input: (N,D_in,W_in)
output: (N,D_out,W_out)
2.簡介(torch.nn.functional下的)
在torch.nn.functional下也有卷積層,但是和torch.nn下的卷積層的區別在於,functional下的是函式,不是實際的卷積層,而是有卷積層功能的卷積層函式,所以它並不會出現在網路的圖結構中。
torch.nn.functional.conv1d(input,weight,bias=None,groups=1)
引數:
- input – 輸入張量的形狀 (minibatch x in_channels x iW)
- weight – 過濾器的形狀 (out_channels,in_channels,kW)
- bias – 可選偏置的形狀 (out_channels)
- stride – 卷積核的步長,預設為1
>>> filters = autograd.Variable(torch.randn(33,3)) >>> inputs = autograd.Variable(torch.randn(20,50)) >>> F.conv1d(inputs,filters)
torch.nn.functional.conv2d(input,groups=1)
>>> # With square kernels and equal stride >>> filters = autograd.Variable(torch.randn(8,4,3,3)) >>> inputs = autograd.Variable(torch.randn(1,5)) >>> F.conv2d(inputs,filters,padding=1)
torch.nn.functional.conv3d(input,groups=1)
>>> filters = autograd.Variable(torch.randn(33,50,10,20)) >>> F.conv3d(inputs,filters)
以上這篇Pytorch之卷積層的使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。