加载预训练的 FinBERT 模型时出现问题

时间:2021-02-01 12:28:36

标签: python pytorch

我正在尝试在 python 中加载一个预先训练的 FinBERT 模型,但只是尝试加载它时出现错误。我正在关注 GitHub 文档:https://github.com/ProsusAI/finBERT

按照教程,我创建了一个名为 bert 的目录,下载了模型 pytorch_model.binconfig.json 文件并将它们放入文件夹中。

我尝试使用以下代码调用模型:

import os
os.chdir(r'bert')

from transformers import BertTokenizer, BertForSequenceClassification
import torch

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

model = BertForSequenceClassification.from_pretrained(r'bert\pytorch_model.bin',
                                                      config = r'bert\config.json')

尝试加载的错误如下:

RuntimeError: Error(s) in loading state_dict for BertForSequenceClassification:
    size mismatch for classifier.weight: copying a param with shape torch.Size([3, 768]) from checkpoint, the shape in current model is torch.Size([2, 768]).
    size mismatch for classifier.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([2]).

我对此的了解非常基础,如果有人了解这里发生的事情,将不胜感激。

谢谢

0 个答案:

没有答案