繁体   English   中英

预训练的 BERT model 的权重未初始化

[英]Weights of pre-trained BERT model not initialized

我正在使用语言解释工具包(LIT) 加载和分析我在 NER 任务上预训练的 BERT model。

但是,当我使用传递给它的预训练 model 的路径启动 LIT 脚本时,它无法初始化权重并告诉我:

    modeling_utils.py:648] loading weights file bert_remote/examples/token-classification/Data/Models/results_21_03_04_cleaned_annotations/04.03._8_16_5e-5_cleaned_annotations/04-03-2021 (15.22.23)/pytorch_model.bin
    modeling_utils.py:739] Weights of BertForTokenClassification not initialized from pretrained model: ['bert.pooler.dense.weight', 'bert.pooler.dense.bias']
    modeling_utils.py:745] Weights from pretrained model not used in BertForTokenClassification: ['bert.embeddings.position_ids']

然后它只是使用 BERT 的bert-base-german-cased版本,它当然没有我的自定义标签,因此无法预测任何东西。 我认为这可能与 PyTorch 有关,但我找不到错误。

如果相关,这里是我如何将我的数据集加载到 CoNLL 2003 格式(在此处找到的数据加载器脚本的修改):

    def __init__(self):

        # Read ConLL Test Files

        self._examples = []

        data_path = "lit_remote/lit_nlp/examples/datasets/NER_Data"
        with open(os.path.join(data_path, "test.txt"), "r", encoding="utf-8") as f:
            lines = f.readlines()

        for line in lines[:2000]:
            if line != "\n":
                token, label = line.split(" ")
                self._examples.append({
                    'token': token,
                    'label': label,
                })
            else:
                self._examples.append({
                    'token': "\n",
                    'label': "O"
                })

    def spec(self):
        return {
            'token': lit_types.Tokens(),
            'label': lit_types.SequenceTags(align="token"),
        }

这就是我如何初始化 model 并启动 LIT 服务器(修改simple_pytorch_demo.py脚本在这里找到):

    def __init__(self, model_name_or_path):
        self.tokenizer = transformers.AutoTokenizer.from_pretrained(
            model_name_or_path)
        model_config = transformers.AutoConfig.from_pretrained(
            model_name_or_path,
            num_labels=15,  # FIXME CHANGE
            output_hidden_states=True,
            output_attentions=True,
        )
        # This is a just a regular PyTorch model.
        self.model = _from_pretrained(
            transformers.AutoModelForTokenClassification,
            model_name_or_path,
            config=model_config)
        self.model.eval()

## Some omitted snippets here

    def input_spec(self) -> lit_types.Spec:
        return {
            "token": lit_types.Tokens(),
            "label": lit_types.SequenceTags(align="token")
        }

    def output_spec(self) -> lit_types.Spec:
        return {
            "tokens": lit_types.Tokens(),
            "probas": lit_types.MulticlassPreds(parent="label", vocab=self.LABELS),
            "cls_emb": lit_types.Embeddings()

这实际上似乎是预期的行为。 GPT 模型的文档中, HuggingFace 团队写道:

这将发出有关未使用某些预训练权重和随机初始化某些权重的警告。 那是因为我们丢弃了 BERT model 的预训练头,用随机初始化的分类头替换它。

所以微调似乎不是问题。 在我上面描述的用例中,尽管有警告,它仍然有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM