unexpected key "module.encoder.embedding.weight" in state_dict解決方案

  • 問題:unexpected key “module.encoder.embedding.weight” in state_dict
  • 原因:保存模型用nn.DataParallel,使用該方法保存模型會用module,但你加載模型時未用nn.DataParallel
  • 解決方案:
    方法1:用nn.DataParallel方法載入模型
    方法2:創建一個新的不包含module前綴的ordered dict載入模型

例子:

# original saved file with DataParallel
state_dict = torch.load('myfile.pth')
# create new OrderedDict that does not contain 'module'.
from collections import OrderedDict
new_state_dict = OrderDict()
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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章