File size: 2,886 Bytes
38dc36b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | # Emotion Detection Model - LongEmotion
情感检测模型,基于BERT的中文情感分类器
## 📊 模型信息
- **基础模型**: bert-base-chinese
- **任务类型**: 6分类情感检测
- **验证准确率**: 91.47%
- **框架**: PyTorch + Transformers
## 🏷️ 情感类别
模型可以识别以下6种情感:
- `sadness` (悲伤)
- `joy` (快乐)
- `love` (爱)
- `anger` (愤怒)
- `fear` (恐惧)
- `surprise` (惊讶)
## 📁 文件说明
```
detection_hug/
├── model.pt # 模型权重文件
├── config.json # 模型配置
├── tokenizer_config.json # 分词器配置
├── vocab.txt # 词表
├── special_tokens_map.json # 特殊符号映射
├── detection_model.py # 模型定义
├── inference_example.py # 推理示例
└── README.md # 本文件
```
## 🚀 快速开始
### 环境要求
```bash
pip install torch transformers
```
### 基本使用
```python
import torch
from transformers import BertTokenizer
from detection_model import EmotionDetectionModel
# 1. 加载分词器
tokenizer = BertTokenizer.from_pretrained(".")
# 2. 加载模型
model = EmotionDetectionModel(
model_name="bert-base-chinese",
num_emotions=6
)
checkpoint = torch.load("model.pt", map_location="cpu")
model.load_state_dict(checkpoint)
model.eval()
# 3. 预测
text = "我今天很开心!"
encoding = tokenizer(text, return_tensors='pt', max_length=512, truncation=True, padding=True)
outputs = model(**encoding)
predicted_emotion = torch.argmax(outputs['logits'], dim=-1).item()
# 情感映射
emotions = ["sadness", "joy", "love", "anger", "fear", "surprise"]
print(f"预测情感: {emotions[predicted_emotion]}")
```
### 使用推理脚本
```bash
python inference_example.py
```
## 📈 模型性能
- **数据集**: dair-ai/emotion (中文情感数据)
- **验证准确率**: 91.47%
- **平均置信度**: 89.27%
- **最大序列长度**: 512 tokens
## 🔧 技术细节
### 模型架构
```
EmotionDetectionModel
├── BERT Encoder (bert-base-chinese)
│ └── 768-dim hidden states
├── Dropout (p=0.1)
├── Linear Layer (768 → 384)
├── ReLU Activation
├── Dropout (p=0.1)
└── Output Layer (384 → 6)
```
### 训练参数
- **优化器**: AdamW
- **学习率**: 2e-5
- **批次大小**: 16
- **最大长度**: 512
## 📝 引用
如果您使用此模型,请注明:
```
LongEmotion Detection Model
- 基于 bert-base-chinese
- 训练于 dair-ai/emotion 数据集
```
## 📧 联系方式
如有问题,请通过项目仓库联系。
---
**License**: 遵循 bert-base-chinese 的许可协议
**Created**: 2025
|