解决Error(s) in loading state_dict for *** :Missing key(s) in state_dict:Unexpected key(s) in state_di

实验环境:

Linux
python3.7
pytorch1.1.0

问题:

RuntimeError: Error(s) in loading state_dict for Transformer:
        Missing key(s) in state_dict: "encoder.embed.conv.0.weight","...".
        Unexpected key(s) in state_dict: "module.encoder.embed.conv.0.weight", "...".
Segmentation fault (core dumped)

#“…”表示后面类似,因为太长,所以就不一一列出了

出现原因:

模型保存时,由于使用多GPU并行训练,而继续使用model而不是model.module保存。

解决方法:

checkpoint = torch.load(args.load_model)
model.load_state_dict({k.replace('module.',''):v for k,v in checkpoint['model'].items()})

参考:

Pytorch:Unexpected key(s) in state_dict: [CSDN]

你可能感兴趣的:(Bug,Pytorch,深度学习,bug,pytorch,神经网络)