Views
No views yet
ckiplab/bert-base-chinese 微調的中文 BERT 模型,專門用於識別 SMS 簡訊中是否包含人名的二元分類任務。| 指標 | 數值 |
|---|---|
| 驗證準確率 | 0.9965 |
| 最佳準確率 | 0.9965 |
1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4# 載入模型和分詞器
5model_name = "renhehuang/bert-chinese-sms-name-classifierv2"
6tokenizer = BertTokenizer.from_pretrained(model_name)
7model = BertForSequenceClassification.from_pretrained(model_name)
8
9# 預測函數
10def predict_name_in_sms(text, max_length=256):
11 model.eval()
12 encoding = tokenizer.encode_plus(
13 text,
14 add_special_tokens=True,
15 max_length=max_length,
16 return_token_type_ids=False,
17 padding='max_length',
18 truncation=True,
19 return_attention_mask=True,
20 return_tensors='pt'
21 )
22
23 with torch.no_grad():
24 outputs = model(
25 input_ids=encoding['input_ids'],
26 attention_mask=encoding['attention_mask']
27 )
28
29 logits = outputs.logits
30 probabilities = torch.softmax(logits, dim=-1)
31
32 predicted_class = torch.argmax(logits, dim=-1).item()
33 confidence = probabilities[0][predicted_class].item()
34
35 return predicted_class, confidence
36
37# 使用範例
38text = "王先生您好,您的訂單已確認"
39predicted_class, confidence = predict_name_in_sms(text)
40print(f"預測類別: {predicted_class} (信心度: {confidence:.4f})")0: 不包含姓名1: 包含姓名@misc{bert-chinese-sms-name-classifier,
title={BERT Chinese SMS Name Classifier},
author={renhehuang},
year={2025},
publisher={Hugging Face},
journal={Hugging Face Model Hub},
}