当前位置:网站首页>[pytorch] 2.4 convolution function nn conv2d
[pytorch] 2.4 convolution function nn conv2d
2022-07-01 09:08:00 【Enzo tried to smash the computer】
Two dimensional convolution should be the most commonly used convolution method , stay Pytorch Of nn Module , Encapsulates the nn.Conv2d() Class as an implementation of two-dimensional convolution . The usage method is the same as that of ordinary classes , Instantiate first and then use . Here is a neural network with only one layer of two-dimensional convolution , As nn.Conv2d() Introduction to the use of the method :
class Net(nn.Module):
def __init__(self):
nn.Module.__init__(self)
self.conv2d = nn.Conv2d(in_channels=3,out_channels=64,kernel_size=4,stride=2,padding=1)
def forward(self, x):
print(x.requires_grad)
x = self.conv2d(x)
return x
print(net.conv2d.weight)
print(net.conv2d.bias)
Its formal parameters are Pytorch The manual can be found , The first three parameters must be provided manually , The following ones have default values . Next, I'll introduce :
- in_channels
It's easy to understand , Is the input four-dimensional tensor [N, C, H, W] Medium C 了 , That is, the input tensor channels Count . This parameter is used to determine the weight and other learnable parameters shape Necessary .
- out_channels
It's easy to understand , Of the desired four-dimensional output tensor channels Count , Don't say more .
- kernel_size
The size of the convolution kernel , We usually use 5x5、3x3 This convolution kernel with the same number on the left and right , So in this case, just write kernel_size = 5 That's all . If the left and right numbers are different , such as 3x5 Convolution kernel , So writing kernel_size = (3, 5), Note that you need to write a tuple, Instead of writing a list (list).
- stride = 1
The interval between each translation of convolution kernel on the image window , The so-called step size . This concept and Tensorflow Other frameworks make no difference , No more words .
- padding = 0
Pytorch And Tensorflow The biggest difference in the implementation of convolution layer is padding On .
Padding The so-called image filling , hinder int The type constant represents how much is filled ( Row number 、 Number of columns ), The default is 0. It should be noted that the filling here includes the top, bottom, left and right of the image , With padding = 1 For example , If the original image size is 32x32, that padding The size of the image becomes 34x34, instead of 33x33.
Pytorch differ Tensorflow The point is ,Tensorflow What is offered is padding The pattern of , such as same、valid, And different modes correspond to different output image size calculation formulas . and Pytorch You need to manually enter padding The number of , Of course ,Pytorch The advantage of this implementation is that the calculation formula of output image size is unique , namely
- dilation = 1
This parameter determines whether to use Cavity convolution , What is void convolution , Put two pictures and you will understand . The default is 1( Do not use ).
- groups = 1
Control the connection between input and output .group=1, The output is the convolution of all the inputs ;group=2, At this time, it is equivalent to having two convolution layers side by side , Each convolution layer computes half of the input channel , And the output is half of the output channel , Then connect the two outputs .
- bias = True
That is, whether to add offset parameters as learnable parameters , The default is True.
- padding_mode = ‘zeros’
namely padding The pattern of , Zero padding is used by default .
边栏推荐
- 【检测技术课案】简易数显电子秤的设计与制作
- Databinding source code analysis
- It technology ebook collection
- Embedded Engineer Interview frequently asked questions
- Principles of Microcomputer - Introduction
- JCL 和 SLF4J
- Daily practice of C language - day 80: currency change
- Vsync+ triple cache mechanism +choreographer
- Leetcode daily question brushing record --540 A single element in an ordered array
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
猜你喜欢
Pain points and solutions of fixed assets management of group companies
Jetson Nano 安装TensorFlow GPU及问题解决
An overview of the design of royalties and service fees of mainstream NFT market platforms
Daily practice of C language - day 80: currency change
Nacos - 配置管理
Football and basketball game score live broadcast platform source code /app development and construction project
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
Nacos - 配置管理
Nacos - service discovery
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
随机推荐
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
TV size and viewing distance
In the middle of the year, where should fixed asset management go?
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
SDN_简单总结
Nacos - 配置管理
钓鱼识别app
通过 代码实例 理解 浅复制 与 深复制
Class loading
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
3D printing Arduino four axis aircraft
[MFC development (16)] tree control
Shell script -if else statement
Insert mathematical formula in MD document and mathematical formula in typora
Principles of Microcomputer - internal and external structure of microprocessor
Mysql8.0 learning record 17 -create table
Shell script - array definition and getting array elements
R language observation log (part24) -- initialization settings
Principle and application of single chip microcomputer timer, serial communication and interrupt system
毕业季,我想对你说