Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem loading pre-trained FinBERT model

Tags:

python

pytorch

I am trying to load a pre-trained FinBERT model in python but am getting an error simply trying to load it. I am following the GitHub docs: 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.

I try and call the model using the following code:

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

like image 365
gerardcslabs Avatar asked Jun 20 '26 22:06

gerardcslabs


1 Answers

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)
like image 180
user17728093 Avatar answered Jun 23 '26 11:06

user17728093



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!