AppReviews AI BERT Fine-tuned Model
Collection
2 items
•
Updated
這是一個基於 BERT 的中文情感分析模型,可用於判斷文本的情感傾向(正面、負面或中性)。
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
# 載入模型和分詞器
model = AutoModelForSequenceClassification.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")
tokenizer = AutoTokenizer.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")
# 準備輸入
text = "這個App使用體驗很差!"
inputs = tokenizer(text, return_tensors="pt")
# 進行預測
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
# 獲取預測結果
label_names = ["負面", "正面", "中性"]
predicted_class = torch.argmax(predictions, dim=1).item()
print(f"預測類別: {label_names[predicted_class]}")
print(f"預測分數: {predictions[0][predicted_class].item():.4f}")
# 顯示所有類別的分數
for i, label in enumerate(label_names):
print(f"{label} 分數: {predictions[0][i].item():.4f}")
Base model
google-bert/bert-base-chinese