One-dimensional convolutional neural network understanding (torch.nn.Conv1d)

Parameter introduction

torch.nn.Conv1d(in_channels,       
                out_channels,     
                kernel_size,      
                stride,        
                padding,        
                padding_mode    
                dilation,      
                groups,        
                bias,           
                )
  • in_channels : (int) The number of channels of the input data, that is, how many sets of vectors are represented by a certain piece of training data. For example, for a piece of data represented by a one-dimensional vector, the number of channels is 1; for text data, a sentence is composed of m words, then the number of channels can be m
  • out_channels : (int) The number of channels generated by convolution, which can be understood as the number of convolution kernels
  • kernel_size : (int or tuple) the size of the convolution kernel, if the parameter is a tuple, there should be only one element in the tuple
  • stride : (int or tuple, optional) The stride of the convolution. The default value is 1, if it is a tuple, there should be only one element in the tuple
  • padding : (int, tuple or str, optional) The amount of padding to add to both sides of the input. The default value is 0
  • padding_mode (string, optional), padding mode on both sides, indicating different padding methods on both sides, optional modes are "'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros'"
  • dilation : (int or tuple, optional) The spacing between kernel elements. The default value is 1, that is, each element of the convolution kernel is next to each other. This is difficult to describe. You can see the intuitive animation here
  • groups : (int, optional) Number of blocking connections from input channels to output channels. The default value is 1. The number of input channels and the number of output channels should be able to divide groups. When groups is n, it means that there are n convolution kernels, and the input data is divided into n parts to obtain the features after convolution.
  • bias : (bool, optional) If True, add a learnable bias to the output. The default is True

input Output

input – [batch_size , in_channels , i] (batch size, number of channels of input data, input data dimensions).
output – ( batch_size , out_channels , i] (batch size, number of channels of output data, dimension of data after convolution).
Calculation method of dimension after convolution : n − k + 2 × ps + 1 \frac{n-k+ 2\times{p}}{s} + 1snk+2×p+1 , (n: input data dimension, k: convolution kernel size, p: use boundary padding, s: step size).
Convolution kernel dimension: [in_channels, kernel_size, out_channels], out_channels represents the number of convolution kernels, used to extract multi-dimensional features.

for example

It is defined as follows:

  • in_channels=1, out_channels=1
  • Input channel: 1, output channel: 1, convolution kernel: 1 × \times× 3 × \times × 1, stride: 1, padding: 0
  • Batch size: 1, Number of channels of data: 1, Data length: 5
import torch
import torch.nn as nn
input = torch.randn(1, 1, 5)
conv = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, stride=1, padding=0)
out = conv(input)

Calculation process:
insert image description here
output: batch size: 1, number of data channels: 1, data length: 3
Reference: https://blog.csdn.net/xu624735206/article/details/124961990

Guess you like

Origin blog.csdn.net/qq_45097352/article/details/127278451