P16.土堆說卷積(可選看)
16.1torch.nn.functional.conv2d的參數(shù)(官網(wǎng))
點擊查看代碼
input:input tensor of shape (minibatch,in_channels,iH,iW)
weight:filters of shape (out_channels,in_channelsgroups,kH,kW)
bias:optional bias tensor of shape (out_channels). Default: `None`
stride:the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1
padding:implicit paddings on both sides of the input. Can be a string {‘valid’, ‘same’}, single number or a tuple (padH, padW).Default: 0 `padding='valid'` is the same as no padding. `padding='same'` pads the input so the output has the same shape as the input. However, this mode doesn’t support any stride values other than 1.
16.2卷積原理

16.3根據(jù)以上圖片的tensor,寫入pycharm
代碼如下:
點擊查看代碼
import torch
#根據(jù)圖片上的輸入圖像和卷積核
input = torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]])
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
#打印input和kernel的尺寸
print(input.shape)
print(kernel.shape)
運行結(jié)果如下:
點擊查看代碼
D:\anaconda3\envs\pytorch\python.exe D:/DeepLearning/Learn_torch/P16_nnConv.py
torch.Size([5, 5])
torch.Size([3, 3])
進程已結(jié)束,退出代碼0
很明顯,他的尺寸輸出只有兩個參數(shù),他不符合torch.nn.functional.conv2d的參數(shù)input – input tensor of shape (minibatch,in_channels,iH,iW)中對于input和kernel的四個參數(shù)的要求
16.4torch.reshape函數(shù)&torch.shape函數(shù)
1.PyTorch中的torch.reshape函數(shù)
(1)官方文檔介紹:
torch.reshape(input, shape) → Tensor
Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.
(2)翻譯為大白話:
在PyTorch中,torch.reshape函數(shù)是用來改變張量(Tensor)的形狀(shape)的工具。這個函數(shù)不改變張量中的數(shù)據(jù)和元素數(shù)量,只是重新排列這些元素的形狀。當可能的時候,torch.reshape會返回原始張量的一個視圖(view),這意味著新的張量和原始張量共享相同的數(shù)據(jù),但是展示的形狀不同。如果不能返回一個視圖,那么它會返回一個數(shù)據(jù)的拷貝。
2.PyTorch中的torch.shape函數(shù)
在PyTorch中,處理圖像數(shù)據(jù)的張量默認遵循NCHW(或稱為channels_first)的格式,即:
N: Number of images in the batch (批次中的圖像數(shù)量)
C: Channels per image (每張圖像的通道數(shù))
H: Height of each image (每張圖像的高度)
W: Width of each image (每張圖像的寬度)
3.應用conv2d
代碼如下:
點擊查看代碼
import torch
import torch.nn.functional as F
#根據(jù)圖片上的輸入圖像和卷積核
input = torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]])
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
#修改尺寸:batch_size為1,channel為1,寬和高保持原來的:
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
#打印input和kernel的尺寸
print(input.shape)
print(kernel.shape)
#應用conv2d
#import torch.nn.functional as F
output = F.conv2d(input,kernel,stride=1)
print(output)
點擊查看代碼
#輸出結(jié)果如下:
D:\anaconda3\envs\pytorch\python.exe D:/DeepLearning/Learn_torch/P16_nnConv.py
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],
[18, 16, 16],
[13, 9, 3]]]])
進程已結(jié)束,退出代碼0
和圖片上我們自己計算的結(jié)果一模一樣,輸出結(jié)果為tensor類型的3*3矩陣
4.修改stride參數(shù)
代碼如下:
點擊查看代碼
#應用conv2d(其他代碼同上)
#import torch.nn.functional as F
output2 = F.conv2d(input,kernel,stride=2)
print(output2)
點擊查看代碼
#輸出結(jié)果如下:
tensor([[[[10, 12],
[13, 3]]]])
輸出結(jié)果為tensor類型的2*2矩陣
5.padding
【padding=1表示原input圖像,上下左右各添加一個空白行(列),值為0】
代碼如下:
點擊查看代碼
#應用conv2d(其他代碼同上)
#import torch.nn.functional as F
#增加padding參數(shù)
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
輸出結(jié)果如下:
點擊查看代碼
#輸出結(jié)果如下:
tensor([[[[ 1, 3, 4, 10, 8],
[ 5, 10, 12, 12, 6],
[ 7, 18, 16, 16, 8],
[11, 13, 9, 3, 4],
[14, 13, 9, 7, 4]]]])
輸出結(jié)果為tensor類型的5*5矩陣,明顯尺寸變大
如圖,原始圖像因為padding=1的設(shè)置,變成了7*7的矩陣:

浙公網(wǎng)安備 33010602011771號