加载中…
加载中…
bert-base-chinese 在 ChnSentiCorp(中文酒店评论情感分类)上微调的中文情感二分类模型。
NLP 课程作业产出。
| split | n | accuracy | precision | recall | F1 |
|---|---|---|---|---|---|
| validation | 1200 | 0.9450 | 0.9414 | 0.9477 | 0.9445 |
| test | 1200 | 0.9475 | 0.9431 | 0.9539 | 0.9485 |
对比:hfl/chinese-roberta-wwm-ext 同配置训练,test acc 0.9492 / F1 0.9502。
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
repo = "monica-2/chinese-sentiment-bert"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForSequenceClassification.from_pretrained(repo).eval()
text = "酒店位置很好,服务也热情,下次还会再来。"
enc = tok(text, truncation=True, max_length=256, return_tensors="pt")
with torch.no_grad():
logits = model(**enc).logits[0]
print(torch.softmax(logits, -1)) # tensor([0.0064, 0.9936]) -> 正面
仓库内含 int8 动态量化 ONNX:onnx/model_quantized.onnx(103 MB),
可在纯前端推理(无需服务器):
import { pipeline } from '@huggingface/transformers';
const clf = await pipeline('text-classification', 'monica-2/chinese-sentiment-bert', { dtype: 'q8' });
await clf('房间又小又脏,隔音差,不推荐。'); // [{label: 'negative', score: 0.94}, ...]
量化模型在 600 条验证样本上 acc 0.9467(原 PyTorch 模型 0.9450),几乎无损。
在线 Demo:https://monica-2-chinese-sentiment-demo.static.hf.space