不是很清楚这两个有什么区别,最近写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)