当前位置:网站首页>Pytorch convolution operation
Pytorch convolution operation
2022-07-01 04:45:00 【booze-J】
article
pytorch Convolution operation official document
Here we use nn.conv2d To explain the convolution operation .
What is convolution ?
The convolution kernel moves on the input image , Then multiply and sum the values on the convolution kernel and the corresponding position on the input image .Stride=1 To control the moving step of convolution kernel .
Convolution operation example code :
import torch.nn.functional as F
import torch
# The input image (5X5)
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]])
# Convolution kernel (3X3)
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
# input: torch.Size([5, 5])
print("input:\n",input.shape)
# kernel:torch.Size([3, 3])
print("kernel:\n",kernel.shape)
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# input:torch.Size([1, 1, 5, 5])
print("input:\n",input.shape)
# kernel:torch.Size([1, 1, 3, 3])
print("kernel:\n",kernel.shape)
# Convolution operation Observe stride Influence on convolution results
output = F.conv2d(input,kernel,stride=1)
print('output\n',output)
output2 = F.conv2d(input,kernel,stride=2)
print('output2\n',output2)
# Perform volume and operation Expand and fill the boundary of the input image Observe padding Influence on convolution results
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print("output\n",output3)
Part of the code explanation :
1.reshape The role of
# reshape front
# input: torch.Size([5, 5]) kernel:torch.Size([3, 3])
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# reshape after
# input:torch.Size([1, 1, 5, 5]) kernel:torch.Size([1, 1, 3, 3])
Why do I need to be right input and kernel Conduct reshape This operation ?
Because use torch.nn.functional.conv2d The input parameters are limited , You can see conv2d Requirements for input parameters , requirement input The input is (minibatch,in_channels,iH,iW), among in_channels Indicates the number of channels ,iH Indicates the height of the input image ,iW Indicates the width of the input image .weigt The input is kernel( Convolution kernel ), You can see that it's right weight The parameter requirements of are similar to input, among outchannels Indicates the number of output channels ,in_channels Indicates the number of input channels (groups Default equal to 1),kH Represents the height of the convolution kernel ,kW Represents the width of the convolution kernel . So you need to input and kernel Conduct reshape operation .
2.stride Parameters
# Convolution operation Observe stride Influence on convolution results
output = F.conv2d(input,kernel,stride=1)
print('output\n',output)
output2 = F.conv2d(input,kernel,stride=2)
print('output2\n',output2)
Running results :
You can see Official documents Yes Stride The explanation of :
- stride – the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1
When stride What you enter is a number , Then this number is the horizontal and vertical moving steps of the convolution kernel , When stride When you enter a tuple , The steps of the convolution kernel moving horizontally and vertically can be set respectively .
3.padding Parameters
# Perform volume and operation Expand and fill the boundary of the input image Observe padding Influence on convolution results
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print("output\n",output3)
In the above code padding The function of parameters is equivalent to , Expand the horizontal and vertical boundaries of the input image 1 Length and fill 0, Then perform convolution operation .
You can see Official documents Yes Padding The explanation of :
- 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.
When padding What you enter is a number , Then this number is the horizontal and vertical boundary expansion filling of the image ( The default filling value is 0) The length of , When padding When you enter a tuple , You can set the length of the horizontal and vertical boundaries of the image respectively .
边栏推荐
- Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
- 如何看待智慧城市建设中的改变和机遇?
- 洗个冷水澡吧
- 分布式全局唯一ID解决方案详解
- 分布式数据库数据一致性的原理、与技术实现方案
- 分布式架构系统拆分原则、需求、微服务拆分步骤
- STM32 photoresistor sensor & two channel AD acquisition
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- RDF query language SPARQL
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
猜你喜欢

Annual inventory review of Alibaba cloud's observable practices in 2021
![[godot] unity's animator is different from Godot's animplayer](/img/51/48f40a7b6736d7f78040eabbbd3395.jpg)
[godot] unity's animator is different from Godot's animplayer

Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

扩展-Fragment
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

Maixll-Dock 快速上手

VR线上展览所具备应用及特色

2022年化工自动化控制仪表操作证考试题库及答案

Pytorch(三) —— 函数优化
随机推荐
Overview of the construction details of Meizhou veterinary laboratory
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
STM32扩展板 温度传感器和温湿度传感器的使用
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
先有网络模型的使用及修改
解决qiankun中子应用外链文件无法获取
无器械健身
Construction of Meizhou nursing laboratory: equipment configuration
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
LeetCode_53(最大子数组和)
分布式数据库数据一致性的原理、与技术实现方案
One click shell to automatically deploy any version of redis
Maixll-Dock 使用方法
OdeInt與GPU
VR线上展览所具备应用及特色
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
Extension fragment
Pytorch(一) —— 基本语法
STM32 光敏电阻传感器&两路AD采集