torch.nn.conv2d 中参数的含义

Shu*_*tni 23 python artificial-intelligence machine-learning pytorch

在 fastai 编码人员的前沿深度学习课程第 7 课中。

 self.conv1 = nn.Conv2d(3,10,kernel_size = 5,stride=1,padding=2)
Run Code Online (Sandbox Code Playgroud)

10 是否意味着过滤器的数量或过滤器将提供的激活数量?

pro*_*sti 43

是您可能会发现的

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

参数

  • in_channels (int) – 输入图像中的通道数
  • out_channels (int) – 卷积产生的通道数
  • kernel_size (int or tuple) – 卷积核的大小
  • stride (int or tuple, optional) – 卷积的步幅。(默认:1)
  • padding (int or tuple, optional) – 在输入的两边添加零填充(默认值:0)
  • padding_mode (string, optional) – 零
  • dilation (int or tuple, optional) – 内核元素之间的间距。(默认:1)
  • 组(int,可选)– 从输入到输出通道的阻塞连接数。(默认:1)
  • 偏差(布尔,可选)– 如果为 True,则向输出添加可学习的偏差。(默认:真)

这个URL有帮助的过程可视化。

因此,in_channels对于具有 3 个通道的图像(彩色图像),一开始是 3。对于黑白图像,它应该是 1。一些卫星图像应该是 4。

out_channels就是卷积将产生的结果,所以这些是过滤器的数量。

让我们创建一个例子来“证明”这一点。

import torch
import torch.nn as nn
c = nn.Conv2d(1,3, stride = 1, kernel_size=(4,5))
print(c.weight.shape)
print(c.weight)
Run Code Online (Sandbox Code Playgroud)

出去

torch.Size([3, 1, 4, 5])
Parameter containing:
tensor([[[[ 0.1571,  0.0723,  0.0900,  0.1573,  0.0537],
          [-0.1213,  0.0579,  0.0009, -0.1750,  0.1616],
          [-0.0427,  0.1968,  0.1861, -0.1787, -0.2035],
          [-0.0796,  0.1741, -0.2231,  0.2020, -0.1762]]],


        [[[ 0.1811,  0.0660,  0.1653,  0.0605,  0.0417],
          [ 0.1885, -0.0440, -0.1638,  0.1429, -0.0606],
          [-0.1395, -0.1202,  0.0498,  0.0432, -0.1132],
          [-0.2073,  0.1480, -0.1296, -0.1661, -0.0633]]],


        [[[ 0.0435, -0.2017,  0.0676, -0.0711, -0.1972],
          [ 0.0968, -0.1157,  0.1012,  0.0863, -0.1844],
          [-0.2080, -0.1355, -0.1842, -0.0017, -0.2123],
          [-0.1495, -0.2196,  0.1811,  0.1672, -0.1817]]]], requires_grad=True)
Run Code Online (Sandbox Code Playgroud)

如果我们要改变 out_channels 的数量,

c = nn.Conv2d(1,5, stride = 1, kernel_size=(4,5))
print(c.weight.shape) # torch.Size([5, 1, 4, 5])
Run Code Online (Sandbox Code Playgroud)

我们将得到 5 个过滤器,每个过滤器 4x5,因为这是我们的内核大小。如果我们设置 2 个通道,(有些图像可能只有 2 个通道)

c = nn.Conv2d(2,5, stride = 1, kernel_size=(4,5))
print(c.weight.shape) # torch.Size([5, 2, 4, 5])
Run Code Online (Sandbox Code Playgroud)

我们的过滤器将有 2 个通道。

我认为他们有本书中的术语,因为他们没有称它为过滤器,所以他们没有使用这个术语。

所以你是对的;过滤器是卷积层正在学习的东西,过滤器的数量是输出通道的数量。它们在开始时随机设置。

激活次数是根据bs和图像尺寸计算的:

bs=16
x = torch.randn(bs, 3, 28, 28)
c = nn.Conv2d(3,10,kernel_size=5,stride=1,padding=2)
out = c(x)
print(out.nelement()) #125440 number of activations
Run Code Online (Sandbox Code Playgroud)


Cle*_*oom 2

检查文档https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d,你有3个in_channels和10个out_channels,所以这10个out_channels是@thefifthjack005过滤器,也称为特征。