[PyTorch] Tutorial: torch.nn.Conv2d

Conv2d

CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)

2D convolution

o u t ( N i , C o u t j ) = b i a s ( C o u t j ) + ∑ k = 0 C i n − 1 W e i g h t ( C o u t j , k ) ∗ i n p u t ( N i , k ) out(N_i, C_{out_j})=bias(C_{out_j})+\sum_{k=0}^{C_{in}-1}Weight(C_{out_j},k)*input(N_i, k) out(Ni,Coutj)=bias(Coutj)+k=0Cin1Weight(Coutj,k)input(Ni,k)

N N N : batch size
CCC : Number of channels
HHH : input high
WWW : input width

  • weight ([ Tensor ]) – module learnable weights, of shape ( out_chanels \text{out\_chanels}out_chanels, in_channels groups \frac{\text{in\_channels}}{\text{groups}} groupsin_channels, kernel_size[0] \text{kernel\_size[0]} kernel_size[0], kernel_size[1] \text{kernel\_size[1]} kernel_size[1] ). U ( − k , k ) \mathcal{U}(-\sqrt{k}, \sqrt{k})U(k ,k ) , wherek = groups C in ∗ ∏ i = 0 1 kernel_size [ i ] k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel \_size}[i]}k=Cini=01kernel_size[i]groups
  • bias ([ Tensor ] ) – module learnable weights, of shape (out_channels). If biasis True, from U ( − k , k ) \mathcal{U}(-\sqrt{k}, \sqrt{k})U(k ,k ) 采样,其中 k = g r o u p s C in ∗ ∏ i = 0 1 kernel_size [ i ] k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]} k=Cini=01kernel_size[i]groups
  • parameter
  • in_channels ([ int ]) – number of channels of the input image
  • out_channels ([ int ]) - number of output channels
  • kernel_size ([ int ] or *[ tuple ]) - kernel size
  • stride ([ int ] or [ tuple ], optional ) - stride, default is 1
  • padding ([ int ] [ tuple ] or [ str ] optional ) - padding, default is 0
  • padding_mode ([ str ] optional ) - zeros, reflect, replicate, default is zeros.
  • dilation ([ int ] [ tuple ] optional ) - dilation kernel, defaults to 1
  • groups ([ int ] optional ) - grouping, default 1
  • bias ([ bool ] optional ) - if set to true, add the learned bias

insert image description here

import torch
import torch.nn as nn 

# With square kernels and equal stride
m1 = nn.Conv2d(16, 33, 3, stride=2)
# non-square kernels and unequal stride and with padding
m2 = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
# non-square kernels and unequal stride and with padding and dilation
m3 = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))

input = torch.randn(20, 16, 50, 100)

output1 = m1(input)  # torch.Size([20, 33, 24, 49])
output2 = m2(input)  # torch.Size([20, 33, 28, 100])
output3 = m3(input)  # torch.Size([20, 33, 26, 100])

【reference】

Conv2d — PyTorch 1.13 documentation

Guess you like

Origin blog.csdn.net/zhoujinwang/article/details/129270062