使用流水线API
模型下载1:distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face
模型下载2:distilbert/distilbert-base-uncased-finetuned-sst-2-english · HF Mirror
from transformers import pipeline
classifier = pipeline("sentiment-analysis", framework="pt", trust_remote_code=True)
result = classifier(
"We are very happy to introduce pipeline to the transformers repository."
)
print(result)
输出:
[{'label': 'POSITIVE', 'score': 0.9996980428695679}]
使用流水线API(多个输入)
模型下载1:distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face
模型下载2:distilbert/distilbert-base-uncased-finetuned-sst-2-english · HF Mirror
from transformers import pipeline
classifier = pipeline("sentiment-analysis", framework="pt", trust_remote_code=True)
results = classifier(
[
"We are very happy to show you the Transformers library.",
"We hope you don't hate it.",
]
)
for result in results:
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
输出:
label: POSITIVE, with score: 0.9998
label: NEGATIVE, with score: 0.5309
不使用流水线API
模型下载1:distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face
模型下载2:distilbert/distilbert-base-uncased-finetuned-sst-2-english · HF Mirror
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# 加载预训练模型和分词器
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# 准备输入文本
text = "We are very happy to introduce pipeline to the transformers repository."
# 对输入文本进行分词
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
# 获取模型预测
with torch.no_grad():
outputs = model(**inputs)
# 处理预测结果
scores = outputs.logits.softmax(dim=-1)
positive_score = scores[0][1].item()
negative_score = scores[0][0].item()
# 确定情感标签
label = "POSITIVE" if positive_score > negative_score else "NEGATIVE"
score = positive_score if label == "POSITIVE" else negative_score
result = [{"label": label, "score": score}]
print(result)
输出:
[{'label': 'POSITIVE', 'score': 0.9996980428695679}]
使用流水线API(本地模型)
模型下载1:distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face
模型下载2:distilbert/distilbert-base-uncased-finetuned-sst-2-english · HF Mirror
from transformers import (
pipeline,
DistilBertTokenizer,
DistilBertForSequenceClassification,
)
path = "./models/distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = DistilBertTokenizer.from_pretrained(path, local_files_only=True)
model = DistilBertForSequenceClassification.from_pretrained(path, local_files_only=True)
classifier = pipeline(
"sentiment-analysis", model=model, tokenizer=tokenizer, framework="pt"
)
result = classifier(
"We are very happy to introduce pipeline to the transformers repository."
)
print(result)
输出:
[{'label': 'POSITIVE', 'score': 0.9996980428695679}]
使用PyTorch
模型下载1:distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face
模型下载2:distilbert/distilbert-base-uncased-finetuned-sst-2-english · HF Mirror
import torch
from transformers import (
DistilBertTokenizer,
DistilBertForSequenceClassification,
)
path = "./models/distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = DistilBertTokenizer.from_pretrained(path, local_files_only=True)
model = DistilBertForSequenceClassification.from_pretrained(path, local_files_only=True)
inputs = tokenizer(
"We are very happy to introduce pipeline to the transformers repository.",
return_tensors="pt",
)
with torch.no_grad():
logits = model(**inputs).logits
predicted_class_id = logits.argmax().item()
result = model.config.id2label[predicted_class_id]
print(result)
输出:
POSITIVE