NotImplementedError
1、错误提示``python`` 程序运行报错 :`` NotImplementedError ``2、错误原因python程序中,raise 可以实现报错的功能,且报错的条件和内容都是程序员自己规定的 ;在面向对象编程中,如果想在父类中预留一个方法,使该方法在子类中实现,如果子类中没有对该方法进行重写就被调用,则会报错:NotImplementError !而这边是因为没有调用 ``forwar
·
🤵 Author :Horizon John
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
🏆 神经网络篇:经典网络模型
💻 算法篇:再忙也别忘了 LeetCode
错误提示
python 程序运行报错 :NotImplementedError

错误原因
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.Module → nn.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))
🈺 喜欢的 留个 关注 、 加 点赞 哦 ~
更多推荐


所有评论(0)