模型:
FinancialBERT 是在大量金融文本语料库上预训练的BERT模型。其目的是增强金融自然语言处理(NLP)的研究和实践,希望金融从业人员和研究人员能够在不需要大量计算资源的情况下从该模型中受益。
该模型是在金融短语库数据集上进行情感分析任务的微调。实验证明,该模型优于通用BERT和其他金融领域特定的模型。
有关FinancialBERT的预训练过程的更多详细信息可在 https://www.researchgate.net/publication/358284785_FinancialBERT_-_A_Pretrained_Language_Model_for_Financial_Text_Mining 找到。
FinancialBERT模型在 Financial PhraseBank 上进行了微调,该数据集由4840条金融新闻按情感(负面、中性、正面)分类。
使用的评估指标为精确率、召回率和F1-score。以下是测试集的分类报告。
| sentiment | precision | recall | f1-score | support |
|---|---|---|---|---|
| negative | 0.96 | 0.97 | 0.97 | 58 |
| neutral | 0.98 | 0.99 | 0.98 | 279 |
| positive | 0.98 | 0.97 | 0.97 | 148 |
| macro avg | 0.97 | 0.98 | 0.98 | 485 |
| weighted avg | 0.98 | 0.98 | 0.98 | 485 |
感谢Transformers情感分析管道,可以使用该模型。
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline
model = BertForSequenceClassification.from_pretrained("ahmedrachid/FinancialBERT-Sentiment-Analysis",num_labels=3)
tokenizer = BertTokenizer.from_pretrained("ahmedrachid/FinancialBERT-Sentiment-Analysis")
nlp = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
sentences = ["Operating profit rose to EUR 13.1 mn from EUR 8.7 mn in the corresponding period in 2007 representing 7.7 % of net sales.",
"Bids or offers include at least 1,000 shares and the value of the shares must correspond to at least EUR 4,000.",
"Raute reported a loss per share of EUR 0.86 for the first half of 2009 , against EPS of EUR 0.74 in the corresponding period of 2008.",
]
results = nlp(sentences)
print(results)
[{'label': 'positive', 'score': 0.9998133778572083},
{'label': 'neutral', 'score': 0.9997822642326355},
{'label': 'negative', 'score': 0.9877365231513977}]