[英]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
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.parameters
是OrderedDict
,然后您尝试使用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.