关于Class继承nn.Module和nn.Sequential的区别

18 篇文章 0 订阅
3 篇文章 0 订阅

不是很清楚这两个有什么区别,最近写shufflenet时候试着写了一下
发现三种写法,都是可以正常运行的,在Sequential中他写了相应的forward函数,我们如果不需要重写的话,就可以只写相应的层,不去重写对应的forward函数,感觉这可能是他的一个区别?

下面的三种写法

class conv3x3(nn.Sequential):
    def __init__(self, in_channel, stride, bias=False):
        super(conv3x3, self).__init__(

            nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
                      kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
            nn.BatchNorm2d(in_channel)
        )

class conv3x3(nn.Sequential):
    def __init__(self, in_channel, stride, bias=False):
        super(conv3x3, self).__init__()
        self.conv3x3 = nn.Sequential(
            nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
                      kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
            nn.BatchNorm2d(in_channel)
        )

class conv3x3(nn.Module):
    # 3x3卷积中的输入通道和输出通道一致,且使用dw卷积,也就是group=channel,都不使用Relu,stride有两种取值,2只在每个stage的第一个block

    def __init__(self, in_channel, stride, bias=False):
        super(conv3x3, self).__init__()
        self.conv3x3 = nn.Sequential(
            nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
                      kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
            nn.BatchNorm2d(in_channel)
        )

    def forward(self, x):
        return self.conv3x3(x)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值