Pytorch实用教程:super(MLP,self).__init__()和super().__init__()有什么区别?

本文探讨了Python中super()函数在初始化PyTorch的MLP类子类时的区别,主要集中在功能差异、多重继承场景及推荐的使用方式。在Python 3中,不带参数的super().__init__()成为首选,因为它简洁且适用于大部分继承情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 Python 中,super() 函数用于调用父类(超类)的方法。它的使用在继承关系中非常常见,特别是在初始化继承自父类的属性时。在 PyTorch 中,这通常见于初始化 nn.Module 类的子类。super() 的两种调用方式有微妙的差别,但在 Python 3 中常常用来达到同样的目的。

代码:

# 定义网络结构
class MLP(nn.Module):
    def __init__(self, input_size, hidden_size
__all__ = ( "DFL", "HGBlock", "HGStem", "SPP", "SPPF", "C1", "C2", "C3", "C2f", "C2fAttn", "ImagePoolingAttn", "ContrastiveHead", "BNContrastiveHead", "C3x", "C3TR", "C3Ghost", "GhostBottleneck", "Bottleneck", "BottleneckCSP", "Proto", "RepC3", "ResNetLayer", "RepNCSPELAN4", "ELAN1", "ADown", "AConv", "SPPELAN", "CBFuse", "CBLinear", "C3k2", "C2fPSA", "C2PSA", "RepVGGDW", "CIB", "C2fCIB", "Attention", "PSA", "SCDown", "TorchVision", "S2DP_C3K", ) __all__ = ( "Conv", "Conv2", "LightConv", "DWConv", "DWConvTranspose2d", "ConvTranspose", "Focus", "GhostConv", "ChannelAttention", "SpatialAttention", "CBAM", "Concat", "RepConv", "Index", ) 上面的是已经有的模块声明,请你据此来进行修改完善一下下面的代码,将整理好的代码写出来: class BaseModule(nn.Module): """基础模块:处理LayerNorm残差连接""" def __init__(self, embed_dim): super().__init__() self.norm = nn.LayerNorm(embed_dim) # 层归一化层 def forward_with_residual(self, x, sublayer): """残差连接:输入x与子层处理后的结果相加""" return x + sublayer(self.norm(x)) # 先归一化,再通过子层,最后残差连接 class LinearAttention(BaseModule): def __init__(self, embed_dim, num_heads): super().__init__(embed_dim) self.attention = nn.MultiheadAttention(embed_dim, num_heads) def forward(self, x): attn_output, _ = self.attention(x, x, x) return self.forward_with_residual(x, lambda _: attn_output) class DepthwiseSeparableFFN(BaseModule): def __init__(self, embed_dim, expansion_ratio=4): super().__init__(embed_dim) expanded_dim = embed_dim * expansion_ratio self.ffn = nn.Sequential( nn.Linear(embed_dim, expanded_dim), nn.GELU(), nn.Linear(expanded_dim, embed_dim) ) def forward(self, x): return self.forward_with_residual(x, self.ffn) class EfficientTransformerLayer(nn.Module): def __init__(self, embed_dim, num_heads, expansion_ratio=4): super().__init__() self.attention = LinearAttention(embed_dim, num_heads) # 注意力子层 self.ffn = DepthwiseSeparableFFN(embed_dim, expansion_ratio) # 前馈子层 def forward(self, x): x = self.attention(x) # 先通过注意力层 x = self.ffn(x) # 再通过前馈网络层 return x class EfficientTransformer(nn.Module): def __init__(self, num_layers, embed_dim, num_heads, expansion_ratio=4): super().__init__() self.layers = nn.ModuleList([ EfficientTransformerLayer(embed_dim, num_heads, expansion_ratio) for _ in range(num_layers) ]) def forward(self, x): for layer in self.layers: x = layer(x) return x
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若北辰

谢谢鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值