Does the program support num_labels = 2?

#4
by ewmiao - opened

Thank you for the excellent work!! Is there a way to set num_labels = 2? I tried to modify the codes here
https://huggingface.co/ahmedrachid/FinancialBERT-Sentiment-Analysis
in order to accommodate 2 labels (positive, negative) rather than 3 (positive, neutral, negative) as follows:
"model = BertForSequenceClassification.from_pretrained("ahmedrachid/FinancialBERT-Sentiment-Analysis",num_labels=2)" but I got an error message.

Hello @ewmiao , sorry for the late response.. just noticed your comment. It's possible for sure ! You need to fine-tune the model for your needs if you have num_labels = 2, you can do transfer learning and freeze all layers before last Dense layer

Load the pre-trained FinancialBERT model

model = BertForSequenceClassification.from_pretrained("ahmedrachid/FinancialBERT-Sentiment-Analysis", num_labels=3)

Adjusting the model for binary classification (positive, negative)

model.classifier = torch.nn.Linear(model.classifier.in_features, 2)

Training setup

trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
)

Train the model

trainer.train()

Sign up or log in to comment