【pytorch】collections.OrderedDict‘ object has no attribute ‘named_parameters‘

这篇具有很好参考价值的文章主要介绍了【pytorch】collections.OrderedDict‘ object has no attribute ‘named_parameters‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

背景

想要加载一个叫xxx.pth的模型,并且查看模型的各个模块

model=Model()
model=torch.load(base+'res2net50_v1b_26w_4s-3cf99910.pth')
for name, param in model.named_parameters():
    print(name)

结果提示如题错误。

解决

才发现这个pth不是模型整个,而是save了一个dict……名字具有误导性哈哈。
torch的save既可以只保存一个dict,里面是每个层的weights,那么在加载的时候需要先定义一个相同结构的model,然后再model load:

torch.save_dict(B.state_dict(),'./newbee')

A=Model() # A和B的结构是相同的
A.load_state(torch.load('newbee'))

当然除了这样节省存储的方式,也可以直接把整个模型保存
torch.save(A,'./aa')然后直接model=torch.load('aa')

其实在出现如题的错误是时候,他已经告诉你了,torch load进来的是一个OrderedDict而不是torch module。文章来源地址https://www.toymoban.com/news/detail-781462.html

到了这里,关于【pytorch】collections.OrderedDict‘ object has no attribute ‘named_parameters‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决‘function‘ object has no attribute ‘data‘

    报错1 原本代码 修改后代码 key 报错2  解决方法2.1 解决方法2.2 AttributeError: \\\'function\\\' object has no attribute \\\'data\\\' function对象没有data属性 key from sklearn import datasets cancer = datasets.load_breast_cancer() load_iris  is a function and needs to be called as such: Use  load_iris()  instead of  load_iris , like this,  

    2024年02月05日
    浏览(44)
  • AttributeError: ‘OptionEngine‘ object has no attribute ‘execute‘

    背景 pandas:1.5.2 sqlalchemy:2.0.4 报错 解决 https://github.com/pandas-dev/pandas/issues/40686 在这篇文章中看到,sqlalchemy的1.4和2.0两种不同的语法,pandas暂时还不支持2.0,所以将sqlalchemy回退到1.4.45即可。

    2024年02月16日
    浏览(29)
  • AttributeError: ‘str‘ object has no attribute ‘word‘

    各位大佬怎么搞啊这个

    2024年02月07日
    浏览(33)
  • AttributeError: ‘list‘ object has no attribute ‘seek‘

    完整的报错为: 初步断定是 torch.load 出了问题。 通过 You can only torch.load from a file that is seekable 这句话可知torch只能load那些seekable的对象,而从 \\\'list\\\' object has no attribute \\\'seek\\\' 可以看出列表是没有seek属性的,于是猜想 torch.load 中传入的参数是列表(一般是传字符串)而导致了这

    2024年02月15日
    浏览(37)
  • AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    1.图片不存在或已损坏无法打开(路径不存在, 路径包含中文无法识别 ) 2.读取的图片内容和默认读取时参数匹配不匹配。(默认读取的是3通道的彩色图)例如读取到的图片是灰度图,就会返回None。 3.也可能是路径中有中文 在采集完新数据重新训练模型时抛异常,Attribut

    2024年02月12日
    浏览(35)
  • AttributeError: ‘NoneType‘ object has no attribute ‘data‘

    在深度学习训练网络过程中,我们常遇到如下的问题:属性错误(其中非类型的对象没有属性\\\'data\\\'),解决的方法主要是查看网络构造是否出现问题。 废话不多说,实践出真知。举个轻量级神经网络训练的例子,源代码包含三部分:网络构造、数据预处理加载以及网络训练。

    2024年02月11日
    浏览(38)
  • ‘FreeTypeFont‘ object has no attribute ‘getsize‘报错解决

    运行YOLOv5时出现警告,但不影响正常训练。问题出在 库 Pillow 中的 getsize 函数,getsize已弃用,将在Pillow 10(2023-07-01)中删除。 python3-- Pillow10 ‘FreeTypeFont‘ object has no attribute ‘getsize‘报错解决_freetypefont\\\' object has no attribute \\\'getsize-CSDN博客 上述博客中第一种方法是降低 Pillow

    2024年04月29日
    浏览(38)
  • 报错AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    环境: python3.6.4 opencv3.4.1.15 运行目标跟踪object_tracking文件夹中的mean函数时报错且不显示视频结果 查找原因基本上看见三个 1.图片不存在(路径不存在, 路径包含中文无法识别) 2.读取的图片内容和默认读取时参数匹配不匹配。(默认读取的是3通道的彩色图)例如读取到的

    2023年04月27日
    浏览(35)
  • 解决AttributeError: ‘Namespace‘ object has no attribute ‘arch‘

    在运行ACmix-ResNet模型时出现问题 很简单的一个错误,没有添加参数 使用parser添加相应参数即可

    2024年02月08日
    浏览(36)
  • 解决AttributeError: ‘DataFrame‘ object has no attribute ‘append‘

    自然语言处理执行 train_data = pd.DataFrame()... contents = pd.DataFrame(content)... 再执行train_data = train_data.append(contents[:400])出现错误AttributeError: \\\'DataFrame\\\' object has no attribute \\\'append\\\' 估计是pandas版本升级弃用了 老版本\\\'DataFrame\\\'的append方法。由于pandas与众多的第三方软件包捆绑,一般不宜轻易

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包