首页IT科技attributeerror:object has no(AttributeError: cannot assign module before Module.__init__() call)

attributeerror:object has no(AttributeError: cannot assign module before Module.__init__() call)

时间2025-08-05 06:46:29分类IT科技浏览6833
导读:诸神缄默不语-个人CSDN博文目录...

诸神缄默不语-个人CSDN博文目录

运行环境                ,报错信息和查找到的错误内容:

Linux系统

Python 3.8(使用anaconda管理的虚拟环境)

PyTorch 1.11+cudatoolkit 10.2(通过anaconda下载)

这个问题是出现在自定义PyTorch模型(torch.nn.Module)子类的运行过程中                          ,在__init__()函数中报错                。

我的错误原因是super(类名,self).__init__()忘写最后一个括号了                          。将括号加上后          ,就能正常运行了          。

这个错误的原因就在于还没有调用Module.__init__()就开始定义子模型了            ,如self.classifier=nn.Linear(input_dim,output_dim)等            。参考异常解决(二)-- AttributeError: cannot assign module before Module.__init__() call_奋斗の博客的博客-CSDN博客                         ,也可能是因为直接没写super()命令而造成的                         。加上之后就可以正常运行               。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
文稿附件目录格式(phpcms v9附件目录创建不成功怎么办)