NotImplementedError


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


错误提示

python 程序运行报错 :
NotImplementedError

error


错误原因

python程序中,raise 可以实现报错的功能,且报错的条件和内容都是程序员自己规定的 ;

在面向对象编程中,如果想在父类中预留一个方法,使该方法在子类中实现,如果子类中没有对该方法进行重写就被调用,则会报错:NotImplementError !

而这边是因为没有调用 forward() 方法


解决方案

方法一:

调用 forward() 方法

解决前:

class Transition_Layer(nn.Module):
    def __init__(self, in_features, out_features):
        super(Transition_Layer, self).__init__()
        self.conv: nn.Conv2d
        self.add_module('conv', nn.Conv2d(in_features, out_features,
                                          kernel_size=1, stride=1, bias=False))

解决后:

class Transition_Layer(nn.Module):
    def __init__(self, in_features, out_features):
        super(Transition_Layer, self).__init__()
        self.conv: nn.Conv2d
        self.add_module('conv', nn.Conv2d(in_features, out_features,
                                          kernel_size=1, stride=1, bias=False))
   def forward(self, x):
        out = self.conv(x)
        return out

方法二:

更换父类 nn.Modulenn.Sequential

解决前:

class Transition_Layer(nn.Module):
    def __init__(self, in_features, out_features):
        super(Transition_Layer, self).__init__()
        self.conv: nn.Conv2d
        self.add_module('conv', nn.Conv2d(in_features, out_features,
                                          kernel_size=1, stride=1, bias=False))

解决后:

class Transition_Layer(nn.Sequential):
    def __init__(self, in_features, out_features):
        super(Transition_Layer, self).__init__()
        self.conv: nn.Conv2d
        self.add_module('conv', nn.Conv2d(in_features, out_features,
                                          kernel_size=1, stride=1, bias=False))

🈺 喜欢的 留个 关注 、 加 点赞 哦 ~



  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值