解决AttributeError: ‘model‘ object has no attribute ‘copy‘

错误

AttributeError: 'model' object has no attribute 'copy'

原因

原因是没有使用model.state_dict()进行模型保存,但是使用了model.load_state_dict()加载模型。这两者要配套使用

解决方案

使用一下的方式保存、加载模型

#保存模型
torch.save( my_resnet.state_dict(),"model.pth")
#加载模型
model.load_state_dict(torch.Load("model.pth"))
  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用中提到了出现了 AttributeError: 'str' object has no attribute 'copy' 错误,并给出了解决方案,即使用 model.load_state_dict(state_dict)。而引用和引用中提到了出现这个错误的具体文件和行数。根据这些信息,可以判断这个错误是由在加载模型时的代码中出现的。这个错误的原因是在使用 copy 模块进行拷贝时,字符串对象没有 copy 属性。可以尝试检查代码中是否有将字符串对象用作 copy 的参数,如果有的话,需要更改代码以确保传递正确的对象给 copy。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: ‘str‘ object has no attributecopy](https://blog.csdn.net/weixin_42727538/article/details/123198712)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【已解决Python中使用xlutils的copy出错:AttributeError: ‘str’ object has no attribute ‘datemode...](https://blog.csdn.net/weixin_28963467/article/details/116166066)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AiCharm

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

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

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

打赏作者

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

抵扣说明:

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

余额充值