【发布时间】:2023-03-12 00:10:01
【问题描述】:
我使用拥抱人脸转换器库对 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 的新手,不知道发生了什么。很可能我没有以正确的方式保存模型。
请提出建议。
【问题讨论】:
标签: machine-learning deep-learning pytorch transfer-learning