AttributeError: cannot assign module before Module.__init__() call

简介: AttributeError: cannot assign module before Module.__init__() call

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

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()命令而造成的。加上之后就可以正常运行。

相关文章
AttributeError: cannot assign module before Module.__init__() call 怎么解决?
这个错误通常是由于在__init__方法之外对模块中的某些成员进行了初始化或赋值操作,导致模块还没有被完全初始化就已经被调用了。为了解决这个问题,你需要将所有的初始化或赋值操作移到__init__方法内部。 例如,如果你有一个自定义模块MyModule,并且想要在其中定义一个类变量my_var,则应该将其放在__init__方法中:
814 0
|
PyTorch 算法框架/工具
成功解决ModuleNotFoundError: No module named ‘torch._C‘
成功解决ModuleNotFoundError: No module named ‘torch._C‘
成功解决ModuleNotFoundError: No module named ‘torch._C‘
|
Python
AttributeError: 'module' object has no attribute 'main'
AttributeError: 'module' object has no attribute 'main'
238 0
AttributeError: 'module' object has no attribute 'main'
AttributeError: EfficientDet object has no attribute module
AttributeError: EfficientDet object has no attribute module
257 0
AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'
/*************************************************************************** * AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier' * 说明: * Tony在用mkdocs的时候遇到这个错误,找一些资料,解决一下。
1609 0
|
Linux
module_init和init_module的区别
今天在看CS8900的驱动时,发现其驱动的模块加载函数是init_module(),由于看到大多数的驱动用的模块加载函数大多是module_init()函数,所以一时没缓过神来,总是在找CS8900的加载函数。
1056 0
|
9月前
|
Python
AttributeError: module ‘numpy‘ has no attribute ‘int‘.
AttributeError: module ‘numpy‘ has no attribute ‘int‘.
264 0
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?
|
计算机视觉
AttributeError: module 'cv2' has no attribute 'xfeatures2d'问题解决
AttributeError: module 'cv2' has no attribute 'xfeatures2d'问题解决
3295 0
|
异构计算
ModuleNotFoundError: No module named ‘mmcv._ext‘
最近想将mmsegmentation打包成exe进行使用,但是遇到了一个问题,在打包的过程中不会显示报错信息,但是在执行exe程序之后,exe会出现ModuleNotFoundError: No module named 'mmcv._ext'的报错,导致软件崩溃。
1121 0

热门文章

最新文章