简体   繁体   English

pytorch 优化器 TypeError 'collections.OrderedDict' 对象不可调用

[英]pytorch optimizer TypeError 'collections.OrderedDict' object is not callable

I used python3.8, pytorch suddenly reported an error optimizer TypeError, but the program was still running two weeks ago.我用的是python3.8,pytorch突然报错优化器TypeError,但是两周前程序还在运行。

net = Net(num_classes=7)
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=1e-4)

It seems to be net.parameters() cannot return the correct result?似乎是 net.parameters() 不能返回正确的结果?

Traceback (most recent call last):
   File "C:/Users/usr/Desktop/jaffeAttention/jaffe.py", line 190, in <module>
       main()   
   File "C:/Users/usr/Desktop/jaffeAttention/jaffe.py", line 87, in main
      optimizer = optim.Adam(net.parameters(), lr=1e-4)
TypeError: 'collections.OrderedDict' object is not callable
 

enter image description here在此处输入图像描述

You get the error because net.parameters is an OrderedDict , and you then try to call it with net.parameters() , but that is not possible.您收到错误是因为net.parametersOrderedDict ,然后您尝试使用net.parameters()调用它,但这是不可能的。 So you get an error.所以你得到一个错误。

To find this out, could try the commands要找出这一点,可以尝试命令

net = Net(num_classes=7)
net.to(device)
criterion = nn.CrossEntropyLoss()
params= net.parameters
print(type(params)) # verify this is indeed an OrderedDict
optimizer = optim.Adam(params, lr=1e-4) # notice there is no () when passing the params to Adam

You can read about OrderedDict in the python documentation.您可以在 python 文档中阅读有关 OrderedDict 的信息。 https://docs.python.org/3/library/collections.html#collections.OrderedDict https://docs.python.org/3/library/collections.html#collections.OrderedDict

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytorch:无法加载CNN模型并进行预测TypeError:“ collections.OrderedDict”对象不可调用 - pytorch: can't load CNN model and do prediction TypeError: 'collections.OrderedDict' object is not callable Django:'collections.OrderedDict' object 不可调用 - Django : 'collections.OrderedDict' object is not callable “collections.OrderedDict”对象没有属性 - 'collections.OrderedDict' object has no attribute (1)TypeError: 不能连接类型为'的object<class 'collections.ordereddict'> '; 只有系列和 DataFrame 对象有效 (2)</class> - (1)TypeError: cannot concatenate object of type '<class 'collections.OrderedDict'>'; only Series and DataFrame objs are valid (2) Django AttributeError: &#39;collections.OrderedDict&#39; 对象没有属性 &#39;pk&#39; - Django AttributeError: 'collections.OrderedDict' object has no attribute 'pk' AttributeError: 'collections.OrderedDict' object 没有属性 'value_counts' - AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts' AttributeError: &#39;collections.OrderedDict&#39; 对象没有属性 &#39;split&#39; - AttributeError: 'collections.OrderedDict' object has no attribute 'split' AttributeError: &#39;collections.OrderedDict&#39; 对象没有属性 &#39;iloc&#39; - AttributeError: 'collections.OrderedDict' object has no attribute 'iloc' Django - &#39;collections.OrderedDict&#39; 对象没有属性 &#39;headers&#39; - Django - 'collections.OrderedDict' object has no attribute 'headers' &#39;collections.OrderedDict&#39; 对象没有属性 &#39;pk&#39; - django rest 框架 - 'collections.OrderedDict' object has no attribute 'pk' - django rest framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM