繁体   English   中英

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

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

我用的是python3.8,pytorch突然报错优化器TypeError,但是两周前程序还在运行。

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

似乎是 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
 

在此处输入图像描述

您收到错误是因为net.parametersOrderedDict ,然后您尝试使用net.parameters()调用它,但这是不可能的。 所以你得到一个错误。

要找出这一点,可以尝试命令

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM