RuntimeError: Error loading state dict for SrlBert Missing keys: ['bert_model.embeddings.position_ids'] Unexpected keys: []

RuntimeError: Error loading state dict for SrlBert Missing keys: ['bert_model.embeddings.position_ids'] Unexpected keys: []

我只是 NLP 的初学者,正在尝试通过实施来学习语义角色标签概念。 我试图从 allennlp 的 public 存储加载 bert-base-srl 模型。 但遇到以下错误:

from allennlp.predictors.predictor import Predictor
predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/bert-base-srl-2020.03.24.tar.gz")

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11672/96061884.py in <module>
      1 from allennlp.predictors.predictor import Predictor
----> 2 predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/bert-base-srl-2020.03.24.tar.gz")

~\anaconda3\lib\site-packages\allennlp\predictors\predictor.py in from_path(cls, archive_path, predictor_name, cuda_device, dataset_reader_to_load, frozen, import_plugins, overrides, **kwargs)
    364             plugins.import_plugins()
    365         return Predictor.from_archive(
--> 366             load_archive(archive_path, cuda_device=cuda_device, overrides=overrides),
    367             predictor_name,
    368             dataset_reader_to_load=dataset_reader_to_load,

~\anaconda3\lib\site-packages\allennlp\models\archival.py in load_archive(archive_file, cuda_device, overrides, weights_file)
    233             config.duplicate(), serialization_dir
    234         )
--> 235         model = _load_model(config.duplicate(), weights_path, serialization_dir, cuda_device)
    236 
    237         # Load meta.

~\anaconda3\lib\site-packages\allennlp\models\archival.py in _load_model(config, weights_path, serialization_dir, cuda_device)
    277 
    278 def _load_model(config, weights_path, serialization_dir, cuda_device):
--> 279     return Model.load(
    280         config,
    281         weights_file=weights_path,

~\anaconda3\lib\site-packages\allennlp\models\model.py in load(cls, config, serialization_dir, weights_file, cuda_device)
    436             # get_model_class method, that recurses whenever it finds a from_archive model type.
    437             model_class = Model
--> 438         return model_class._load(config, serialization_dir, weights_file, cuda_device)
    439 
    440     def extend_embedder_vocab(self, embedding_sources_mapping: Dict[str, str] = None) -> None:

~\anaconda3\lib\site-packages\allennlp\models\model.py in _load(cls, config, serialization_dir, weights_file, cuda_device)
    378 
    379         if unexpected_keys or missing_keys:
--> 380             raise RuntimeError(
    381                 f"Error loading state dict for {model.__class__.__name__}\n\t"
    382                 f"Missing keys: {missing_keys}\n\t"

RuntimeError: Error loading state dict for SrlBert
    Missing keys: ['bert_model.embeddings.position_ids']
    Unexpected keys: []

有人知道解决这个问题的方法吗?

如果您使用的是更高版本的 allennlp-models,您可以使用此 archive_file 代替:https://storage.googleapis.com/allennlp-public-models/structured-prediction-srl-bert.2020.12.15.tar.gz.

模型存档文件的最新版本可以在“模型卡”选项卡的演示页面上找到:https://demo.allennlp.org/semantic-role-labeling