pytorch学习002- -debug(load_state_dict() missing 1 required positional argument: ‘state_dict‘)

Debug

savept.py文件用来将gpu上训练的模型转换为cpu上推理可用的pt文件
以下为部分代码:

import torch

from model import NET

print(torch.__version__)  # 1.10.1+cpu

model = NET
device = torch.device('cpu')
state_dict = torch.load(PATH, map_location=device)

model.load_state_dict(state_dict, strict=False)

具体的报错内容
model.load_state_dict(state_dict, strict=False)
TypeError: load_state_dict() missing 1 required positional argument: ‘state_dict’

=打印一下state_dict:在这里插入图片描述
可以看到的是正常的,一个关键在于实例化*
参考
在这里插入图片描述
修改:model= NET()
这个地方是属于实例化错误,以后的学习过程中应当重视基础。
补充:
在测试过程中遇到了一个“Unexpected key(s) in state_dict”的问题
解决方案:CSDN博客

posted @ 2022-01-13 16:18  Keep_Silent  阅读(78)  评论(0编辑  收藏  举报