torch.nn.Conv2d
In [3]: torch.nn.Conv2d?Init signature: torch.nn.Conv2d(in_channels,out_channels,kernel_size,stride=1,padding=0,dilation=1,groups=1,bias=True)Parameters:in_channels (int) – Number of channels in the input imageout_channels (int) – Number of channels produced by the convolutionkernel_size (int or tuple) – Size of the convolving kernelstride (int or tuple, optional) – Stride of the convolution. Default: 1padding (int or tuple, optional) – Zero-padding added to both sides of the input. Default: 0dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1bias (bool, optional) – If True, adds a learnable bias to the output. Default: True>>> # With square kernels and equal stride>>> m = nn.Conv2d(16, 33, 3, stride=2)>>> # non-square kernels and unequal stride and with padding>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))>>> # non-square kernels and unequal stride and with padding and dilationIn [21]: import torch.nn as nnIn [22]: m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))In [23]: input = torch.randn(20, 16, 50, 100)In [24]: output = m(input)In [25]: output.shapeOut[25]: torch.Size([20, 33, 26, 100])

C_out=out_channels (int) – Number of channels produced by the convolution
举例1
比如输入是一个32x32x3的图像,3表示RGB三通道,每个filter/kernel是5x5x3,一个卷积核产生一个feature map,下图中,有6个5x5x3的卷积核,故输出6个feature map(activation map),大小即为28x28x6。

下图中,第二层到第三层,其中每个卷积核大小为5x5x6,这里的6就是28x28x6中的6,两者需要相同,即每个卷积核的“层数”需要与输入的“层数”一致。有几个卷积核,就输出几个feature map,下图中,与第二层作卷积的卷积核有10个,故输出的第三层有10个通道。
举例2
NxN大小的输入(暂时不考虑通道数),与FxF大小的卷积核(暂时不考虑个数)做卷积,那么输出大小为多大?计算公式为:**(N - F) / **stride + 1,其中stride为做卷积是相邻卷积核的距离(步长)。
举例3
当输入为7x7大小,卷积核为3x3,stride=1,在7x7周围补上一圈0(pad=1个像素)**,那么输出大小为多大?
是7x7。**
输入为32x32x3,卷积核大小为5x5,总共有10个卷积核,做卷积的时候stride=1,pad=2,那么这一层总共含有多少参数?
每个卷积核含有的参数个数为:553 + 1 = 76**,其中1是偏置bias,由于有10个卷积核,故总参数为7610=760*。
总结
其中,卷积核的数量K一般是2的整数次幂,这是因为计算方便(计算机计算2^n比较快)
关于池化层的参数计算


In [29]: m = nn.Conv2d(16, 33, 3, stride=2)In [30]: mOut[30]: Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2))In [31]: input = torch.randn(20, 16, 50, 100)In [32]: output = m(input)In [33]: output.shapeOut[33]: torch.Size([20, 33, 24, 49])
N,C,H,W
- 20——20
- m的out_channels=33,所以16——33
- H_out = 24 = (50-3+2*0)/2+1 = 24.5 =24
- W_out = 49 = (100-3+2*0)/2+1 = 49.5 = 49
torch.nn.BatchNorm2d
torch.nn.``BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
| Parameters: | - num_features – **C from an expected input of size (N, C, H, W)(N,C,H,W)上一层的输出 - eps – a value added to the denominator for numerical stability. Default: 1e-5 - momentum – the value used for the running_mean and running_var computation. Can be set to None for cumulative moving average (i.e. simple average). Default: 0.1- affine – a boolean value that when set to True, this module has learnable affine parameters. Default: True- track_running_stats** – a boolean value that when set to True, this module tracks the running mean and variance, and when set to False, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: True |
|---|---|

torch.nn.ReLU
torch.nn.``ReLU(inplace=False)
| Parameters: | inplace – can optionally do the operation in-place. Default: False |
|---|---|

torch.nn.MaxPool2d
torch.nn.``MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
| Parameters: | - kernel_size – the size of the window to take a max over - stride – the stride of the window. Default value is kernel_size- padding – implicit zero padding to be added on both sides - dilation – a parameter that controls the stride of elements in the window - return_indices – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later- ceil_mode – when True, will use ceil instead of floor to compute the output shape |
|
|---|---|---|
torch.nn.``AvgPool2d
torch.nn.``AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
| Parameters: | - kernel_size – the size of the window - stride – the stride of the window. Default value is kernel_size- padding – implicit zero padding to be added on both sides - ceil_mode – when True, will use ceil instead of floor to compute the output shape - count_include_pad – when True, will include the zero-padding in the averaging calculation |
|---|---|

