返回模型
说明文档
TinyBERT 金融新闻情感分析
一个轻量级 TinyBERT 模型,针对金融新闻情感分析进行了微调,在模型大小小于 60MB、CPU 推理延迟小于 50ms 的条件下实现了 89% 的准确率。
模型详情
- 模型类型: 文本分类(情感分析)
- 架构: TinyBERT(4 层,312 隐藏层维度)
- 预训练基座:
huawei-noah/TinyBERT_General_4L_312D - 微调数据集: 带有情感标签的金融新闻标题
- 输入: 金融新闻文本(最多 128 个 token)
- 输出: 情感分类(负面/中性/正面)
性能指标
| 指标 | 数值 |
|---|---|
| 准确率 | 89.2% |
| F1 分数 | 0.87 |
| 模型大小 | 54.84MB |
| CPU 延迟 | 28ms |
| 量化后大小 | 5.3MB |
使用方法
使用 Pipeline 直接推理
from transformers import pipeline
classifier = pipeline(
\"text-classification\",
model=\"mikeysharma/finance-sentiment-analysis\"
)
result = classifier(\"$TSLA - Morgan Stanley upgrades Tesla to Overweight\")
print(result)
直接使用模型和分词器
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained(\"mikeysharma/finance-sentiment-analysis\")
model = AutoModelForSequenceClassification.from_pretrained(\"mikeysharma/finance-sentiment-analysis\")
inputs = tokenizer(
\"$BYND - JPMorgan cuts Beyond Meat price target\",
return_tensors=\"pt\",
truncation=True,
max_length=128
)
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
print(predictions)
ONNX Runtime(生产环境最佳选择)
from optimum.onnxruntime import ORTModelForSequenceClassification
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(\"mikeysharma/finance-sentiment-analysis\")
model = ORTModelForSequenceClassification.from_pretrained(\"mikeysharma/finance-sentiment-analysis\")
inputs = tokenizer(
\"Cemex shares fall after Credit Suisse downgrade\",
return_tensors=\"pt\",
truncation=True,
max_length=128
)
outputs = model(**inputs)
训练数据
该模型在包含三种情感类别的金融新闻标题数据集上进行了微调:
- 负面:看跌情绪、评级下调、亏损
- 中性:事实性报道,无明显情感倾向
- 正面:看涨情绪、评级上调、盈利
示例样本:
$AAPL - Apple hits record high after earnings beat (Positive)
$TSLA - Tesla misses Q2 delivery estimates (Negative)
$MSFT - Microsoft announces new Azure features (Neutral)
预处理
文本预处理包括:
- 转换为小写
- 股票代码标准化($AAPL → AAPL)
- 移除 URL
- 清理特殊字符
- 截断至 128 个 token
部署
对于生产环境部署,我们推荐:
- ONNX Runtime 用于 CPU 优化推理
- FastAPI 用于 REST API 服务
- Docker 容器化
示例 Dockerfile:
FROM python:3.8-slim
WORKDIR /app
COPY . .
RUN pip install transformers optimum[onnxruntime] fastapi uvicorn
CMD [\"uvicorn\", \"api:app\", \"--host\", \"0.0.0.0\", \"--port\", \"8000\"]
局限性
- 主要基于英文金融新闻训练
- 在非金融文本上性能可能下降
- 短文本(标题)效果最佳
- 可能无法识别含蓄的讽刺/反语
伦理考量
虽然该模型对市场分析有帮助,但不应作为投资决策的唯一依据。请始终结合人工判断和其他数据来源。
引用
如果您在研究中使用此模型,请引用:
@misc{tinybert-fin-sentiment,
author = {Mikey Sharma},
title = {Lightweight Financial News Sentiment Analysis with TinyBERT},
year = {2023},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/mikeysharma/finance-sentiment-analysis}}
}
license: mit
mikeysharma/finance-sentiment-analysis
作者 mikeysharma
text-classification
↓ 1
♥ 0
创建时间: 2025-07-01 14:32:25+00:00
更新时间: 2025-07-01 17:31:23+00:00
在 Hugging Face 上查看文件 (8)
.gitattributes
README.md
config.json
model.onnx
ONNX
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.txt