Pytorch学习 - Task5 PyTorch卷积层原理和使用
Pytorch學習 - Task5 PyTorch卷積層原理和使用
- 1. 卷積層
- (1)介紹 (torch.nn下的)
- 1) class torch.nn.Conv1d() 一維卷積層
- 2) class torch.nn.Conv2d() 二維卷積層
- 卷積尺寸的計算
- padding的兩種方式
- 空洞卷積
- 簡化版尺寸計算公式:
- 完整版尺寸計算公式:
- 3) class torch.nn.Conv3d() 三維卷積層
- (2) 介紹 (torch.nn.functional下的)
- 區別:
- 1)torch.nn.functional.conv1d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
- 2)torch.nn.functional.conv2d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
- 3) torch.nn.functional.conv3d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
- 轉置卷積
- 尺寸計算公式
- 2. 卷積網絡示例
- 3. 任務
參考鏈接1:
參考鏈接2:
參考鏈接3:
1. 卷積層
(1)介紹 (torch.nn下的)
主要有三種:1. class torch.nn.Conv1d() 一維卷積層,用于計算一維數據2. class torch.nn.Conv2d() 二維卷積層3. class torch.nn.Conv3d() 三維卷積層參數及含義 in_channels(int) 輸入信號的通道 out_channels(int) 卷積產生的通道 kernel_size(int, 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,添加偏置1) class torch.nn.Conv1d() 一維卷積層
class torch.nn.Conv1d(in_channels,out_channels,kernel_size,stride=1,padding=0dilation=1,groups=1,bias=True)
input(N,C_in,L_in) N為batch_size,C_in為in_channels,即一批內輸入一維數據個數,L_in是一維數據基數
output(N,C_out,L_out) N為batch_size,C_out為out_channels,即一批內輸出一維數據個數,L_out是一維數據基數
圖示:
2) class torch.nn.Conv2d() 二維卷積層
圖示:
卷積尺寸的計算
padding的兩種方式
1)Valid 是丟棄的方法,例如input_width=7,kernel_width=5,stride=3,只允許滑動一次,多余的元素丟掉。
2)Same是補全的方法,例如下圖允許滑動3次但是要補4個元素,左邊2個0,右邊2個0,這種方法不會拋棄邊緣信息。【如何計算填充數量呢?】
注:輸入的數據為圖片時,通常為彩色圖片(RGB三通道),也就是說輸入的維度是[圖片數,圖片高,圖片寬,通道數]
空洞卷積
空洞卷積是在卷積過程中,在卷積核元素之間塞入空格,如下圖所示:
這里引入了一個新的超參數d,(d-1)的值則為塞入的空格數,假定原來的卷積核大小為k,那么塞入了(d-1)個空格后的卷積核大小n為 n=k+(k-1)(d-1)
簡化版尺寸計算公式:
這里不考慮空洞卷積,設圖片大小為i*i ,卷積核大小為f,stride為s,padding的像素數為p,圖片經過卷積的尺寸是
- 當填充方式為 VALID 時,p 值等于 0,代入相應的 i,f,p,s 就可以相應的計算出 o 值了。
- 當填充方式為 SAME 時,步長 s 為 1 時,輸出的 o == i,我們則可以計算出相應的 P 值為 p = (f-1) / 2
完整版尺寸計算公式:
完整的計算包括了空洞卷積
進而,假定輸入空洞卷積的大小為i,步長為s,空洞卷積后特征圖大小o的計算公式為
3) class torch.nn.Conv3d() 三維卷積層
圖示:
(2) 介紹 (torch.nn.functional下的)
區別:
torch.nn.functional下的卷積層和torch.nn下的卷積層的區別在于,functional下的是函數,不是實際的卷積層,而是有卷積層功能的卷積層函數,所以它并不會出現在網絡的圖結構中。
1)torch.nn.functional.conv1d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
參數: input -輸入張量的形狀(minibatch x in_channels ) weight - 過濾器的形狀(out_channels,in_channels,kw) bias - 可選偏置的形狀 out_channels stride -卷積核的步長,默認為1 用法: filters = autograd.Variable(torch.randn(33,16,3)) inputs = autograd.Variable(torch.randn(20,16,50)) F.conv1d(inputs,filters)2)torch.nn.functional.conv2d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
filters = autograd.Variable(torch.randn(8,4,3,3)) inputs = autograd.Variables(torch.randn(1,4,5,5)) F.conv2d(inputs,filters,padding=1)3) torch.nn.functional.conv3d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1)
filters = autograd.Variable(torch.randn(33,16,3,3,3)) inputs = autograd.Variables(torch.randn(20,16,50,10,20)) F.conv2d(inputs,filters)轉置卷積
圖示:
尺寸計算公式
參考鏈接4:
2. 卷積網絡示例
3. 任務
步驟2:計算下如下卷積層的參數量
nn.Conv2d(in_channels=1,out_channels=32,kernel_size=5,stride=1,padding=2)輸出圖片大小 o = (1-32)/1 + 1
參數量:5 * 5 * 32 * 1 + 32
總結
以上是生活随笔為你收集整理的Pytorch学习 - Task5 PyTorch卷积层原理和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pytorch学习-tensorboar
- 下一篇: 如何判断强化学习训练是否在收敛?