简体   繁体   English

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

[英]Problem loading pre-trained FinBERT model

I am trying to load a pre-trained FinBERT model in python but am getting an error simply trying to load it.我正在尝试在 python 中加载预训练的 FinBERT model,但只是尝试加载它时出现错误。 I am following the GitHub docs: https://github.com/ProsusAI/finBERT我正在关注 GitHub 文档: https://github.com/ProsusAI/finBERT

Following the tutorial, I have created a directory called bert , downloaded the model pytorch_model.bin and config.json file and dropped them in the folder.按照教程,我创建了一个名为bert的目录,下载了 model pytorch_model.binconfig.json文件并将它们放入文件夹中。

I try and call the model using the following code:我尝试使用以下代码调用 model:

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')

The error trying to load is as follows:尝试加载的错误如下:

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]).

My knowledge of this is very basic and if anyone understands what is going on here it would be greatly appreciated.我对此的了解非常基础,如果有人了解这里发生的事情,将不胜感激。

Thanks谢谢

All you need to do is specify the number of labels:您需要做的就是指定标签的数量:

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

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

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