KeyError: ‘unexpected key “module.encoder.embedding.weight” in state_dict’
程序员文章站
2022-05-27 10:32:47
...
最近在跑模型的时候出现了KeyError: ‘unexpected key “module.encoder.embedding.weight” in state_dict’
错误。记录一下:
1。这可能是因为你使用了nn.DataParallel来存储模型module,而你现在尝试不使用加载模型DataParallel直接加载模型,所以就出现了这个bug。
解决方法:
1.你可以先将model加载到DataParallel:mdoel = torch.nn.DataParallel(model)
,然后再加载预训练权重。
2.你可以创建一个不带有module的dict来加载:
# original saved file with DataParallel
state_dict = torch.load('myfile.pth.tar')
# create new OrderedDict that does not contain `module.`
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)
参考:
这里是参考
推荐阅读
-
unexpected key "module.conv1_1.weight" in state_dict
-
unexpected key "module.encoder.embedding.weight" in state_dict解决方案
-
KeyError: ‘unexpected key “module.encoder.embedding.weight” in state_dict’
-
unexpected key “model.conv1.weight” in state_dict
-
解决Pytorch下报错Missing key(s) in state_dict: "resnet.conv1.0.weight",和 Unexpected key(s) in state_dict
-
Unexpected key(s) in state_dict: “dense_block1.denselayer1.norm.1
-
PyTorch 错误 导出onnx提示 Unexpected key(s) in state_dict: “module.classifier.0.weight
-
pytorch加载模型报错Unexpected key(s) in state_dict: module.conv1.weight, module.bn1