小编hap*_*bit的帖子

如何加载部分预训练的 pytorch 模型?

我正在尝试让 pytorch 模型在句子分类任务上运行。在处理医疗笔记时,我正在使用 ClinicalBert ( https://github.com/kexinhuang12345/clinicalBERT ) 并希望使用其预先训练的权重。不幸的是,ClinicalBert 模型只将文本分类为 1 个二进制标签,而我有 281 个二进制标签。因此,我试图实现此代码https://github.com/kaushaltrivedi/bert-toxic-comments-multilabel/blob/master/toxic-bert-multilabel-classification.ipynb,其中 bert 之后的最终分类器长度为 281。

如何在不加载分类权重的情况下从 ClinicalBert 模型加载预训练的 Bert 权重?

天真地尝试从预训练的 ClinicalBert 权重中加载权重,我收到以下错误:

size mismatch for classifier.weight: copying a param with shape torch.Size([2, 768]) from checkpoint, the shape in current model is torch.Size([281, 768]).
size mismatch for classifier.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([281]).
Run Code Online (Sandbox Code Playgroud)

我目前尝试从 pytorch_pretrained_bert 包中替换 from_pretrained 函数,并像这样弹出分类器权重和偏差:

def from_pretrained(cls, pretrained_model_name, state_dict=None, cache_dir=None, *inputs, **kwargs):
    ...
    if …
Run Code Online (Sandbox Code Playgroud)

python machine-learning pre-trained-model pytorch spacy-transformers

5
推荐指数
1
解决办法
4326
查看次数