我使用hugging face transformer库微调一款BERT模型,并在云GPU上进行训练。然后像下面这样保存模型和分词器:
model.save_pretrained('/saved_model/')
torch.save(best_model.state_dict(), '/saved_model/model')
tokenizer.save_pretrained('/saved_model/')
我在电脑上下载了
saved_model
目录。然后我在电脑上按照以下方式加载模型/令牌器。import torch
from transformers import *
tokenizer = BertTokenizer.from_pretrained('./saved_model/')
config = BertConfig('./saved_model/config.json')
model = BertModel(config)
model.load_state_dict(torch.load('./saved_model/pytorch_model.bin', map_location=torch.device('cpu')))
model.eval()
但是在model.load_state_dict
这行代码中,它会抛出下面的错误:
RuntimeError: Error(s) in loading state_dict for BertModel:
Missing key(s) in state_dict:
它列出了一堆在state_dict中显然缺失的键。
我是pytorch的新手,不确定发生了什么。很可能我没有正确保存模型。
请提供建议。