Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Deep learning 在pytorch代码中,下采样如何在ResNet中工作?_Deep Learning_Conv Neural Network_Pytorch_Torchvision - Fatal编程技术网

Deep learning 在pytorch代码中,下采样如何在ResNet中工作?

Deep learning 在pytorch代码中,下采样如何在ResNet中工作?,deep-learning,conv-neural-network,pytorch,torchvision,Deep Learning,Conv Neural Network,Pytorch,Torchvision,在这个pytorch ResNet代码示例中,他们在第44行中将downsample定义为变量。第58行将其用作函数。从CNN的角度和python代码的角度来看,这个示例在这里是如何工作的 代码示例: 我搜索了downsample是否有pytorch内置函数。但事实并非如此 class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=No

在这个pytorch ResNet代码示例中,他们在第44行中将downsample定义为变量。第58行将其用作函数。从CNN的角度和python代码的角度来看,这个示例在这里是如何工作的

代码示例:

我搜索了downsample是否有pytorch内置函数。但事实并非如此

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, norm_layer=None):
        super(BasicBlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        if groups != 1:
            raise ValueError('BasicBlock only supports groups=1')
        # Both self.conv1 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = norm_layer(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = norm_layer(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

return out

我相信在这种情况下,它可以是平均池或最大池。它们都降低了维数并保留了输入的大部分属性。

如果查看原始的ResNet纸张(),它们使用跨步卷积来降低图像的采样。主路径是使用这些跨步卷积自动降采样的,就像在代码中所做的那样。剩余路径使用(a)添加零项的标识映射以不添加额外参数,或(b)使用相同步幅参数的1x1卷积

def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, norm_layer=None):
第二个选项可能如下所示:

if downsample:
    self.downsample = conv1x1(inplanes, planes, strides)

在这个ResNet示例中,当我们定义BasicBlock类时,我们将downsample作为构造函数参数传递

def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, norm_layer=None):
若我们不向类传递任何内容,那个么
downsample=None
,结果标识将不会更改

当我们将
downsample=“some convolution layer”
作为类构造函数参数传递时,它将通过传递的卷积层对标识进行向下采样,以成功执行加法。该层将通过前面提到的代码对标识进行向下采样

  if self.downsample is not None:
        identity = self.downsample(x)

除了Thomas Pinetz所说的:

resnet-50
体系结构中,这是一个下采样步骤:

downsample = nn.Sequential(conv1x1(self.inplanes, planes * block.expansion, stride),norm_layer(planes * block.expansion))

注-1*1卷积和批量标准化

嗨,托马斯,谢谢你的回答。我理解(b)使用1x1获得在剩余连接中作为输入传递所需的维度,而我不能完全理解(a)如何使用零条目进行标识映射。您能否澄清或指出一些有助于理解选项(a)的资源