Sub topic 3
Using a Pretrained Transformer Model
This chapter assumes you can write basic Python, install packages with pip, and read a classifier's output. You should also know the idea of train and test splits, and have run a lexicon or TF-IDF baseline.
A pretrained transformer is a neural network that someone else has already trained on large amounts of text. You load it and call it. Transformers read words in context, so they often handle negation better than word-count methods.
Loading and running a model
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, "->", result["label"], round(result["score"], 3))The score is the model's confidence in its label. It is not accuracy.
Why negation, sarcasm, and slang cause errors
Negation flips meaning: "not bad" is mildly positive, but a model may weigh "bad" heavily. Sarcasm says the opposite of what it means: "Great, another outage" is negative. Slang can reverse usual polarity: "this phone slaps" is praise. The model learns patterns from training text, so it misreads these cases when the cues are subtle or rare in that training text.
When a model trained on reviews meets social posts
This problem is called domain shift. Reviews are longer and more formal. Posts are short, full of abbreviations, and full of replies to other people. A model trained on one kind of text can score the other kind poorly without warning. Before you trust a model on new data, label a small sample from your target domain by hand and measure the results.
Fine-tuning on your own labeled data
Fine-tuning means continuing to train the pretrained model on your own labeled examples. It is worth the effort when the model misses your label scheme or your domain, and when you have a few hundred labeled examples from that domain. Hold out a test set, use a small learning rate such as 2e-5, and compare the result with the untuned model on that same test set.
Going beyond one label: aspect-based sentiment
"The battery is great but the screen is awful" has two opinions with opposite polarity. One label per post hides this. Aspect-based sentiment assigns a polarity to each aspect, such as battery or screen. It needs aspect labels in your data and usually a model built for the task.
Reading your errors and using results responsibly
Sort misclassified posts into groups such as negation, sarcasm, domain mismatch, and wrong labels. Report precision and recall for each class, not only overall accuracy. Do not use sentiment scores alone to make decisions about specific people, such as customers or employees, without human review. Model scores reflect the biases in the training data.
Exercise
A model labels "Oh great, the app crashed again" as positive. Which error type is this, and what would you check first?
Show answer
This is sarcasm. Check whether the errors cluster on sarcastic or ironic posts. Then label more examples of that type from your domain before deciding whether to fine-tune.