许可证: mit
语言:
- 英文
评估指标:
- 准确率
基础模型:
- google-bert/bert-base-uncased
管道标签: 文本分类
标签:
- 文本分类
- 垃圾短信
- 英文
基于BERT-base-uncased预训练模型微调的垃圾短信分类模型
评估结果日志请查看Github: https://github.com/fzn0x/bert-sms-classification
这是我在自然语言处理(NLP)领域的第二个项目,通过微调bert-base-uncased模型实现了垃圾短信分类功能。相较于之前的项目(https://github.com/fzn0x/bert-indonesian-english-hate-comments)有了显著改进。
如何使用该模型?
from transformers import BertTokenizer, BertForSequenceClassification
import torch
tokenizer = BertTokenizer.from_pretrained('fzn0x/bert-spam-classification-model')
model = BertForSequenceClassification.from_pretrained('fzn0x/bert-spam-classification-model')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
def model_predict(text: str):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=1).item()
return '垃圾短信' if prediction == 1 else '正常短信'
def predict():
text = "你好,你知道这种加密货币能让你暴富吗?联系号码88888"
predicted_label = model_predict(text)
print(f"1. 预测类别: {predicted_label}")
text = "理查德,帮帮我!"
predicted_label = model_predict(text)
print(f"2. 预测类别: {predicted_label}")
text = "只需100美元即可购买loopstation,访问buyloopstation.com"
predicted_label = model_predict(text)
print(f"3. 预测类别: {predicted_label}")
text = "兄弟,我打你电话没人接,你在哪?"
predicted_label = model_predict(text)
print(f"4. 预测类别: {predicted_label}")
if __name__ == "__main__":
predict()
📚 引用文献
若使用本仓库或相关创意,请引用以下文献:
完整BibTeX条目请参见citations.bib
文件
- Wolf等, Transformers: 最先进的自然语言处理技术, EMNLP 2020. ACL文献集
- Pedregosa等, Scikit-learn: Python机器学习库, JMLR 2011.
- Almeida & Gómez Hidalgo, 短信垃圾收集数据集v.1, UCI机器学习库(2011). Kaggle链接
🧠 致谢与使用库