【发布时间】:2021-11-08 13:24:54
【问题描述】:
我想将 BERT 的权重(或任何转换器)加载到 DPRQuestionEncoder 架构中,这样我就可以使用 HuggingFace save_pretrained 方法并将保存的模型插入到 RAG architecture to do end-to-end fine-tuning 中。
from transformers import DPRQuestionEncoder
model = DPRQuestionEncoder.from_pretrained('bert-base-uncased')
但我收到以下错误
You are using a model of type bert to instantiate a model of type dpr. This is not supported for all configurations of models and can yield errors.
NotImplementedErrorTraceback (most recent call last)
<ipython-input-27-1f1b990b906b> in <module>
----> 1 model = DPRQuestionEncoder.from_pretrained(model_name)
2 # https://github.com/huggingface/transformers/blob/41cd52a768a222a13da0c6aaae877a92fc6c783c/src/transformers/models/dpr/modeling_dpr.py#L520
/opt/conda/lib/python3.8/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
1211 )
1212
-> 1213 model, missing_keys, unexpected_keys, error_msgs = cls._load_state_dict_into_model(
1214 model, state_dict, pretrained_model_name_or_path, _fast_init=_fast_init
1215 )
/opt/conda/lib/python3.8/site-packages/transformers/modeling_utils.py in _load_state_dict_into_model(cls, model, state_dict, pretrained_model_name_or_path, _fast_init)
1286 )
1287 for module in unintialized_modules:
-> 1288 model._init_weights(module)
1289
1290 # copy state_dict so _load_from_state_dict can modify it
/opt/conda/lib/python3.8/site-packages/transformers/modeling_utils.py in _init_weights(self, module)
515 Initialize the weights. This method should be overridden by derived class.
516 """
--> 517 raise NotImplementedError(f"Make sure `_init_weigths` is implemented for {self.__class__}")
518
519 def tie_weights(self):
NotImplementedError: Make sure `_init_weigths` is implemented for <class 'transformers.models.dpr.modeling_dpr.DPRQuestionEncoder'>
我正在使用最新版本的变形金刚。
【问题讨论】:
-
我认为实现这一目标的最快方法是编写自己的继承自
DPRQuestionEncoder的类。 -
这样做有意义吗(即在 DPRQuestionEncoder 中加载 BERT 的权重)?或者有没有办法用我的 BERT 代替 DPR 的 BERT 模型?
标签: python nlp huggingface-transformers bert-language-model transformer