【报错记录】AttributeError: ‘xxx‘ object has no attribute ‘module‘


问题描述

在跑代码时,报出 AttributeError: 'InpaintGenerator' object has no attribute 'module' 的错误,如下图所示:
在这里插入图片描述

经过一通Debug,定位到是模型保存的位置出错,在检查完路径等没有错误之后。去网上搜寻了一翻资料。终于在一篇博客里找到了解决方案,此处进行记录,以后遇到方便查看。

问题分析与解决

原来这是别人多GPU跑的模型,用的方法是:

torch.save(self.netG.module.state_dict(),   # state_dict变量存放训练过程中需要学习的权重和偏执系数
                os.path.join(self.args.save_dir, f'G{str(self.iteration).zfill(7)}.pt'))

可以看到第一个参数是 netG.module.state_dict(),而我的电脑是单GPU,因此只需要把.module去掉就好了。

即将代码改为:

torch.save(self.netG.state_dict(),   # state_dict变量存放训练过程中需要学习的权重和偏执系数
                os.path.join(self.args.save_dir, f'G{str(self.iteration).zfill(7)}.pt'))

此时再次运行代码就不再报错了。

总结

在保存模型时,如果电脑是单GPU,报 AttributeError: 'xxx' object has no attribute 'module' 的错误的话,只需要把 torch.save() 中第一个参数的.moudle去掉即可。

参考资料

[1] https://blog.csdn.net/qq_45860671/article/details/124118416

  • 32
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
这个错误信息"AttributeError: WebDriver object has no attribute press"表明在使用WebDriver对象时,没有名为press的属性。根据引用中提到的错误分析,这可能是由于前端传递给后端的参数数据类型不正确导致的。在这种情况下,建议与后端的同事共同分析日志,以了解具体出错原因并进行修复。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python脚本报错AttributeError: ‘moduleobject has no attributexxx’解决方法](https://blog.csdn.net/qq_32907349/article/details/52881608)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [python报错: list object has no attribute shape的解决](https://download.csdn.net/download/weixin_38748721/13708867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [AttributeErrorobject has no attribute 报错及解决](https://blog.csdn.net/weixin_41697143/article/details/85212334)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

半岛铁子_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值