neural-network - 卷积神经网络/CNN 中的组

标签 neural-network deep-learning conv-neural-network pytorch

我遇到了这个 PyTorch example for depthwise separable convolutions using the groups parameter :

class depthwise_separable_conv(nn.Module):
    def __init__(self, nin, nout):
        super(depthwise_separable_conv, self).__init__()
        self.depthwise = nn.Conv2d(nin, nin, kernel_size=3, padding=1, groups=nin)
        self.pointwise = nn.Conv2d(nin, nout, kernel_size=1)

    def forward(self, x):
        out = self.depthwise(x)
        out = self.pointwise(out)
        return out

我以前从未在 CNN 中看到任何组的用法。就此而言,文档也有点稀疏:

groups controls the connections between inputs and outputs. in_channels and out_channels must both be divisible by groups.

所以我的问题是:

  • CNN 中的组是什么?
  • 在什么情况下我需要使用群组?

(我这更一般,而不是 PyTorch 特定的。)

最佳答案

也许您正在查找旧版本的文档。 nn.Conv2d 的 1.0.1 文档对此进行了扩展。

Groups controls the connections between inputs and outputs. in_channels and out_channels must both be divisible by groups. For example,

At groups=1, all inputs are convolved to all outputs.

At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.

At groups= in_channels, each input channel is convolved with its own set of filters, of size: (floor(c_out / c_in))

如果您更喜欢更数学化的描述,请先考虑使用 groups=1(默认)的 1x1 卷积。它本质上是一个完整矩阵,适用于每个 (h, w) 位置的所有 channel f。将 groups 设置为更高的值会将此矩阵变成对角 block 稀疏矩阵,其 block 数等于 groups。使用 groups=in_channels 你会得到一个对角矩阵。

现在,如果内核大于 1x1,您将保留上面的 channel block 稀疏性,但允许更大的空间内核。我建议重新阅读我上面引用的文档中的 groups=2 豁免,它以另一种方式准确描述了该场景,可能有助于理解。希望这会有所帮助。

编辑:为什么有人要使用它?作为模型的约束(先验)或作为性能改进技术;有时两者。在链接线程中,想法是用 NxN, groups=n_features -> 1x1, groups=1 序列替换 NxN, groups=1 2d conv 卷积。这在数学上导致了单个卷积(因为卷积的卷积仍然是卷积),但使“乘积”卷积矩阵更加稀疏,从而减少了参数数量和计算复杂度。 This似乎是更深入地解释这一点的合理资源。

关于neural-network - 卷积神经网络/CNN 中的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123161/

相关文章:

arrays - 将一个列表(n 元组或列表)与另一个列表(也可以是数组)缩放的惯用 F# 方法是什么?

machine-learning - 无法使用神经网络近似正弦函数

tensorflow - 我正在阅读一本关于深度学习的书,但我无法理解关于 Conv2D 的这一部分

python - Theano:使用 CSV 文件中的数据训练 theano 神经网络

python - Keras 中用于图像分类的 CNN 类型是什么?

python - Keras Functional API 在每个 API 中更改层名称

python-3.x - Keras 中的 fit_generator 是否应该在每个纪元后重置生成器?

python - 在神经网络中 : accuracy improvement after each epoch is GREATER than accuracy improvement after each batch. 为什么?

deep-learning - 哪种 GAN 模型最适合使用配对数据集生成 beard 和 no_beard 图像

python - Keras 使用 ImageDataGenerator 将图像数据缩放到 -1 到 1 之间的值