|
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}") |
|
|