我遇到了这个 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
andout_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 序列替换
卷积。这在数学上导致了单个卷积(因为卷积的卷积仍然是卷积),但使“乘积”卷积矩阵更加稀疏,从而减少了参数数量和计算复杂度。 This似乎是更深入地解释这一点的合理资源。NxN, groups=1
2d conv
关于neural-network - 卷积神经网络/CNN 中的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123161/