Sub topic 9
Using a Pretrained Transformer Model
This chapter assumes basic Python, pip, and supervised learning concepts such as training examples, labels, and predictions. You should also know that earlier methods represent text with word counts or TF-IDF values. A pretrained transformer offers a different approach: it uses a language model that has already learned patterns from large amounts of text.
Why use a transformer?
A transformer is a neural network architecture that reads tokens while considering how they relate to one another. A token may be a word, part of a word, or punctuation. The model uses attention, a mechanism that assigns different importance to other tokens when interpreting each token.
For example, in “The camera is small but surprisingly powerful,” the model can connect “surprisingly” and “powerful” even though other words occur between them. A TF-IDF model sees features such as small and powerful, but it does not naturally represent their full relationship.
A pretrained model has already learned general language patterns. A sentiment model has also been trained or adapted to predict sentiment labels. You can use it immediately without training on your own dataset.
Running a pretrained model
The Hugging Face transformers library provides a pipeline interface for common tasks. The following model was trained for sentiment analysis on social media text.
from transformers import pipeline
classifier = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
)
posts = [
"Just got my order and it is exactly what I wanted",
"Third outage this week. Unbelievable.",
]
for post, result in zip(posts, classifier(posts)):
print(post)
print(" ->", result["label"], round(result["score"], 3))The first execution downloads model files and may take time. Later executions can reuse the local cache. Each result contains a label and a score, which is the model's confidence for that label. Treat confidence as model evidence, not as a probability that is guaranteed to be correct.
Transformer versus simpler baselines
| Method | Strength | Limitation |
|---|---|---|
| Lexicon, such as VADER | Fast and easy to inspect | Depends on known words and rules |
| TF-IDF plus logistic regression | Simple, cheap, and strong for stable domains | Limited understanding of context |
| Pretrained transformer | Captures context and informal language better | Requires more memory, time, and careful validation |
A transformer is useful when wording, context, or social-media language matters. It may be unnecessary for a small, stable classification problem where a linear baseline already meets the accuracy requirement.
Gotchas and validation
Check the model's label meanings before using its output in an application. Some models use labels such as LABEL_0; others use names such as negative, neutral, and positive. Confirm this in the model documentation or by inspecting representative predictions.
Evaluate the model on a held-out sample from your own domain. Compare precision, recall, and F1 with your TF-IDF baseline. Inspect errors involving negation, sarcasm, mixed opinions, emojis, and very short posts. If the model consistently fails on your labels or domain, fine-tuning may be worthwhile, but it requires enough representative labeled data.
Exercise
Why might a pretrained social-media sentiment model be a better choice than a TF-IDF model for short posts, and what must you check before deploying it?
Show answer
It may capture context, slang, hashtags, and relationships between words more effectively. Check its label meanings, evaluate it on representative data from your domain, and inspect its errors rather than trusting confidence alone.