Task-specific models offer high accuracy and efficiency for well-defined classification tasks.
Here's an example using a fine-tuned RoBERTa model for sentiment analysis.
Python code:
$ vi representation-sentiment.py
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
import numpy as np
from scipy.special import softmax
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
# load the pre-trained sentiment analysis model, tokenizer, and configuration.
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
tokenizer = AutoTokenizer.from_pretrained(MODEL)
config = AutoConfig.from_pretrained(MODEL)
# tokenize input text
encoded_input = tokenizer("The weather today is great!", return_tensors='pt')
# analyze sentiment of input text and return ranked predictions.
output = model(**encoded_input)
# extract and normalize scores
scores = output.logits[0].detach().numpy()
scores = softmax(scores)
# rank predictions by confidence
ranking = np.argsort(scores)
ranking = ranking[::-1]
for i in range(scores.shape[0]):
l = config.id2label[ranking[i]]
s = scores[ranking[i]]
print(f"{i+1}) {l} {np.round(float(s), 4)}")
Run the Python script:
$ python3 representation-sentiment.py
Output:
1) positive 0.9899
2) neutral 0.0068
3) negative 0.0033