下载的NER模型在读取时报错 疑似缺少某些参数 想请教如何解决
Necolizer opened this issue · 2 comments
Necolizer commented
您好,我遇到了这样的报错:
Traceback (most recent call last):
File "medical_ner.py", line 184, in <module>
res = my_pred.predict_sentence(sentence)
File "medical_ner.py", line 103, in predict_sentence
self.model.load_state_dict(torch.load(self.NEWPATH, map_location=device))
File "/home/amax/.conda/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1407, in load_state_dict
self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for BERT_LSTM_CRF:
Missing key(s) in state_dict: "word_embeds.embeddings.position_ids".
我做的操作是这样的:
- git clone这个仓库
- 配置环境和依赖库
- 下载NER模型(链接:https://pan.baidu.com/s/16TPSMtHean3u9dJSXF9mTw )后解压压缩包
- 修改medical_ner.py中的这几行中的路径,使之指向我服务器上正确的路径:
self.NEWPATH = '/Users/yangyf/workplace/model/medical_ner/model.pkl'
self.vocab = load_vocab('/Users/yangyf/workplace/model/medical_ner/vocab.txt')
self.vocab_reverse = {v: k for k, v in self.vocab.items()}
self.model = BERT_LSTM_CRF('/Users/yangyf/workplace/model/medical_ner', tagset_size, 768, 200, 2,
dropout_ratio=0.5, dropout1=0.5, use_cuda=use_cuda)
- 跑
medical_ner.py
- 最后就出现上述那个错误
我检查了一下,当前这个模型【需要】以下这些参数:
word_embeds.embeddings.position_ids torch.Size([1, 512])
word_embeds.embeddings.word_embeddings.weight torch.Size([21128, 768])
word_embeds.embeddings.position_embeddings.weight torch.Size([512, 768])
word_embeds.embeddings.token_type_embeddings.weight torch.Size([2, 768])
word_embeds.embeddings.LayerNorm.weight torch.Size([768])
word_embeds.embeddings.LayerNorm.bias torch.Size([768])
word_embeds.encoder.layer.0.attention.self.query.weight torch.Size([768, 768])
word_embeds.encoder.layer.0.attention.self.query.bias torch.Size([768])
word_embeds.encoder.layer.0.attention.self.key.weight torch.Size([768, 768])
word_embeds.encoder.layer.0.attention.self.key.bias torch.Size([768])
word_embeds.encoder.layer.0.attention.self.value.weight torch.Size([768, 768])
word_embeds.encoder.layer.0.attention.self.value.bias torch.Size([768])
word_embeds.encoder.layer.0.attention.output.dense.weight torch.Size([768, 768])
...省略...
我猜测load进来的checkpoint中(也就是model.pkl
中),可能没有word_embeds.embeddings.position_ids
这项。劳烦您能否拨冗查看一下,是我的执行步骤有误?还是训练好的模型checkpoint有问题?谢谢!
glxyyn commented
您好,我遇到了这样的报错:
Traceback (most recent call last): File "medical_ner.py", line 184, in <module> res = my_pred.predict_sentence(sentence) File "medical_ner.py", line 103, in predict_sentence self.model.load_state_dict(torch.load(self.NEWPATH, map_location=device)) File "/home/amax/.conda/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1407, in load_state_dict self.__class__.__name__, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for BERT_LSTM_CRF: Missing key(s) in state_dict: "word_embeds.embeddings.position_ids".
我做的操作是这样的:
- git clone这个仓库
- 配置环境和依赖库
- 下载NER模型(链接:https://pan.baidu.com/s/16TPSMtHean3u9dJSXF9mTw )后解压压缩包
- 修改medical_ner.py中的这几行中的路径,使之指向我服务器上正确的路径:
self.NEWPATH = '/Users/yangyf/workplace/model/medical_ner/model.pkl' self.vocab = load_vocab('/Users/yangyf/workplace/model/medical_ner/vocab.txt') self.vocab_reverse = {v: k for k, v in self.vocab.items()} self.model = BERT_LSTM_CRF('/Users/yangyf/workplace/model/medical_ner', tagset_size, 768, 200, 2, dropout_ratio=0.5, dropout1=0.5, use_cuda=use_cuda)
- 跑
medical_ner.py
- 最后就出现上述那个错误
我检查了一下,当前这个模型【需要】以下这些参数:
word_embeds.embeddings.position_ids torch.Size([1, 512]) word_embeds.embeddings.word_embeddings.weight torch.Size([21128, 768]) word_embeds.embeddings.position_embeddings.weight torch.Size([512, 768]) word_embeds.embeddings.token_type_embeddings.weight torch.Size([2, 768]) word_embeds.embeddings.LayerNorm.weight torch.Size([768]) word_embeds.embeddings.LayerNorm.bias torch.Size([768]) word_embeds.encoder.layer.0.attention.self.query.weight torch.Size([768, 768]) word_embeds.encoder.layer.0.attention.self.query.bias torch.Size([768]) word_embeds.encoder.layer.0.attention.self.key.weight torch.Size([768, 768]) word_embeds.encoder.layer.0.attention.self.key.bias torch.Size([768]) word_embeds.encoder.layer.0.attention.self.value.weight torch.Size([768, 768]) word_embeds.encoder.layer.0.attention.self.value.bias torch.Size([768]) word_embeds.encoder.layer.0.attention.output.dense.weight torch.Size([768, 768]) ...省略...
我猜测load进来的checkpoint中(也就是
model.pkl
中),可能没有word_embeds.embeddings.position_ids
这项。劳烦您能否拨冗查看一下,是我的执行步骤有误?还是训练好的模型checkpoint有问题?谢谢!
这个代码使用transformers加载的模型,这个错误应该是你transformers版本的问题,你版本太高了。你版本回滚一下吧
pip install transformers==3.0.2
这个是我的版本,我加载的是分词模型,没有报错
Necolizer commented
好像确实是transformers版本问题,回退版本后能正常输出预测结果了,谢谢!