金融文本情感分析演示平台
项目概述
- LLaMA-Factory针对ERNIE-4.5进行微调,使其能分析财经新闻或社交媒体文本的情感或市场观点。
- 收集财经新闻标题或微博股票评论,对其进行情感标注(正面/中性/负面),用ERNIE-4.5-0.3B-pt进行微调
- 目标:
- 使模型更敏感于金融术语和行业情绪。
- 模型输出情感分类。
- 锻炼ERNIE在中文财经领域的表现,贴近实际应用场景。
文件结构说明
- 项目基于LLaMA-Factory,保留原先项目文件结构
- 与本次Challenge相关的代码在 0.my_code 文件夹下
- 本项目参与任务类别是:Model-Building Tasks
项目思路
1. 数据集准备
此部分代码在:0.my_code\process\data_preparation.ipynb
- 数据集:Maciel/FinCUGE-Instruction:专门为 中文金融大语言模型(Financial LLM)设计的指令微调数据集。它旨在通过高质量的中文金融指令数据,提升模型在金融专业场景下的理解、推理和生成能力。
- 使用其中FINFE类的数据集,保存至 0.my_code\process\data\fin_sentiment.json 目录下
- 转换数据集格式以适配LLaMA-Factory的sft训练,同时调整模型的output为 <'POS'>, <'NEG'>, <'NEU'> 标签
- 保持原始数据标签分布的条件下,划分 训练集/验证集/测试集
- 将 训练集/验证集/测试集 复制到data目录下
- 修改 data\dataset_info.json 文件
json "finance_sentiment": { "file_name": "fin_sentiment_sft.jsonl", "columns": { "prompt": "instruction", "query": "input", "response": "label" } }, "finance_sentiment_val": { "file_name": "fin_sentiment_sft_val.jsonl", "columns": { "prompt": "instruction", "query": "input", "response": "label" } },
2. tokenizer添加special tokens
此部分代码在:0.my_code\process\data_preparation.ipynb
因为模型最后的输出结果需要是情感分类,通过将 <'POS'>, <'NEG'>, <'NEU'>这三个标签添加到tokenizer中,避免模型在输出时因为分词问题导致logits概率不正确的问题
- 加载baidu/ERNIE-4.5-0.3B-PT的tokenizer模型
- 通过add_special_tokens方法添加标签
- 保存tokenizer
- 验证标签在tokenizer中是否为单token(训练时要确保使用添加过标签的tokenizer进行训练)
3. 配置训练文件
文件路径:examples\train_lora\ernie_4_5_0.3b_sft.yaml
- 使用full模式进行全量监督微调
- 使用lora模式时,0.3B的模型无法学习到文本和标签之间的关系,最终采用全量sft ```yaml ### model model_name_or_path: "D:/code/LLaMA-Factory/0.my_code/process/ernie-sentiment-init" trust_remote_code: true
stage
stage: sft do_train: true
训练方式
finetuning_type: full
dataset
dataset: finance_sentiment # 训练集 eval_dataset: finance_sentiment_val # 验证集 template: ernie_nothink cutoff_len: 2048 max_samples: 10000 overwrite_cache: true preprocessing_num_workers: 16 dataloader_num_workers: 0
training (核心)
train_on_prompt: false # ⭐⭐⭐ 必须 per_device_train_batch_size: 4 gradient_accumulation_steps: 1 learning_rate: 5e-5 # ⭐ 进一步提高学习率(解决loss不下降) num_train_epochs: 5 lr_scheduler_type: cosine # ⭐ 添加学习率调度 warmup_ratio: 0.1 # ⭐ 添加warmup warmup_steps: 50 # ⭐ 明确指定warmup步数 logging_steps: 10 save_steps: 250 # ⭐ 保存checkpoint以便分析
eval
do_eval: true eval_strategy: steps eval_steps: 250 per_device_eval_batch_size: 4
output
output_dir: ./output/finance-sentiment overwrite_output_dir: true metric_for_best_model: eval_loss
### 4. 开始训练
llamafactory-cli train examples\train_lora\ernie_4_5_0.3b_sft.yaml
### 5.验证训练结果
使用 0.my_code\process\data_preparation.ipynb 中 模型评估 部分的代码 <br>
输出样例:
```json
{
"overall_metrics": {
"accuracy": 0.807914262159934,
"accuracy_percentage": 80.7914262159934,
"f1_macro": 0.7893991934186833,
"f1_weighted": 0.8068937528266235,
"total_samples": 2426,
"valid_predictions": 2426
},
"per_class_metrics": {
"positive": {
"precision": 0.8671390611160319,
"recall": 0.89,
"f1_score": 0.8784208165096455,
"support": 1100
},
"negative": {
"precision": 0.7891737891737892,
"recall": 0.7869318181818182,
"f1_score": 0.7880512091038406,
"support": 704
},
"neutral": {
"precision": 0.7176470588235294,
"recall": 0.6864951768488746,
"f1_score": 0.7017255546425637,
"support": 622
}
},
"confusion_matrix": {
"labels": [
"positive",
"negative",
"neutral"
],
"matrix": [
[
979,
51,
70
],
[
52,
554,
98
],
[
98,
97,
427
]
],
"matrix_dict": {
"positive": {
"positive": 979,
"negative": 51,
"neutral": 70
},
"negative": {
"positive": 52,
"negative": 554,
"neutral": 98
},
"neutral": {
"positive": 98,
"negative": 97,
"neutral": 427
}
}
},
"model_info": {
"base_model_path": "D:/code/LLaMA-Factory/0.my_code/process/ernie-sentiment-init",
"finetuned_model_path": "D:/code/LLaMA-Factory/output/finance-sentiment/",
"val_file": "./data/fin_sentiment_sft_test.jsonl"
}
}
6. 启动前端页面以及后端服务
- 启动nextjs前端
bash cd 0.my_code\frontend pnpm install pnpm dev - 启动fastapi后端
bash cd 0.my_code\backend python app.py - 浏览器打开 http://localhost:3000 查看效果
Built With
- fastapi
- llama-factory
- nextjs
Log in or sign up for Devpost to join the conversation.