🚀 bert-base-uncased-emotion
本项目基于BERT模型在情感数据集上进行微调,可用于文本情感分类任务,能准确识别文本中的情感类别,在Twitter情感数据集上表现出色。
🚀 快速开始
使用以下代码示例即可快速使用该模型进行文本情感分类:
from transformers import pipeline
classifier = pipeline("text-classification",model='bhadresh-savani/bert-base-uncased-emotion', return_all_scores=True)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use", )
print(prediction)
"""
output:
[[
{'label': 'sadness', 'score': 0.0005138228880241513},
{'label': 'joy', 'score': 0.9972520470619202},
{'label': 'love', 'score': 0.0007443308713845909},
{'label': 'anger', 'score': 0.0007404946954920888},
{'label': 'fear', 'score': 0.00032938539516180754},
{'label': 'surprise', 'score': 0.0004197491507511586}
]]
"""
✨ 主要特性
- 基于BERT架构:采用Transformer双向编码器架构,在MLM(掩码语言模型)目标上进行预训练。
- 微调优化:在情感数据集上进行微调,能更好地适应文本情感分类任务。
- 多指标评估:在多个评估指标上表现良好,如准确率和F1分数。
📦 安装指南
文档未提及具体安装步骤,可参考Hugging Face相关库的安装方式。
💻 使用示例
基础用法
from transformers import pipeline
classifier = pipeline("text-classification",model='bhadresh-savani/bert-base-uncased-emotion', return_all_scores=True)
prediction = classifier("I love using transformers. The best part is wide range of support and its easy to use", )
print(prediction)
"""
output:
[[
{'label': 'sadness', 'score': 0.0005138228880241513},
{'label': 'joy', 'score': 0.9972520470619202},
{'label': 'love', 'score': 0.0007443308713845909},
{'label': 'anger', 'score': 0.0007404946954920888},
{'label': 'fear', 'score': 0.00032938539516180754},
{'label': 'surprise', 'score': 0.0004197491507511586}
]]
"""
📚 详细文档
模型描述
Bert 是一种基于Transformer的双向编码器架构,在MLM(掩码语言模型)目标上进行训练。
bert-base-uncased 使用HuggingFace的Trainer在情感数据集上进行微调,训练参数如下:
学习率 2e-5,
批量大小 64,
训练轮数 8
模型在Twitter情感数据集上的性能比较
数据集
Twitter-Sentiment-Analysis
训练过程
可参考 Colab Notebook,将模型名称从distilbert改为bert即可。
评估结果
{
'test_accuracy': 0.9405,
'test_f1': 0.9405920712282673,
'test_loss': 0.15769127011299133,
'test_runtime': 10.5179,
'test_samples_per_second': 190.152,
'test_steps_per_second': 3.042
}
参考资料
📄 许可证
本项目采用Apache-2.0许可证。