Tokymin's picture
编写了数据处理已经预训练的基础代码
1f4f3bd
raw
history blame
761 Bytes
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# 指定预训练模型
model_name = "bert-base-uncased"
# 加载分词器和模型
tokenizer = AutoTokenizer.from_pretrained(model_name,force_download=True, resume_download=False)
model = AutoModelForSequenceClassification.from_pretrained(model_name,force_download=True, resume_download=False)
# 要进行分类的文本
text = "I love using transformers for natural language processing."
# 使用分词器处理文本
inputs = tokenizer(text, return_tensors="pt")
# 使用模型进行预测
with torch.no_grad():
logits = model(**inputs).logits
# 解析预测结果
predicted_class_id = logits.argmax().item()
print(f"Predicted class id: {predicted_class_id}")