Sub topic 10
Training a Classifier on Weak Labels
What changes when labels are weak?
Assume you know basic Python, supervised text classification, precision, recall, F1, probability, and regular expressions.
A label model produces a probability distribution for each example. For binary sentiment classification, one review might receive:
| Text | Negative probability | Positive probability |
|---|---|---|
| “The screen is bright and clear” | 0.08 | 0.92 |
You can convert this distribution into a training label by selecting the class with the highest probability. This is called a hard pseudo-label. You can also keep the confidence, such as 0.92, and give highly confident examples more influence during training.
The classifier still learns from text features. Weak supervision only changes how the training targets are created.
A confidence-weighted training workflow
The example below uses a TF-IDF classifier. TF-IDF, or term frequency–inverse document frequency, represents a document by giving higher values to words that are frequent in that document but less common across documents.
The weak_probs array represents output from a label model. Each row contains probabilities for the negative and positive classes.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
weak_texts = [
"I love the excellent battery life",
"The phone arrived broken",
"Great camera and clear display",
"Terrible build quality",
"It works as expected",
"I want a refund for this defective item",
]
# These probabilities could come from a label model.
# Columns are [negative, positive].
weak_probs = [
[0.05, 0.95],
[0.96, 0.04],
[0.10, 0.90],
[0.93, 0.07],
[0.45, 0.55],
[0.88, 0.12],
]
gold_texts = [
"Excellent screen and fast performance",
"The hinge broke after one day",
"This product is acceptable",
"I love how reliable it is",
]
gold_labels = [1, 0, 1, 1] # 0 = negative, 1 = positive
vectorizer = TfidfVectorizer(ngram_range=(1, 2), lowercase=True)
X_weak = vectorizer.fit_transform(weak_texts)
X_gold = vectorizer.transform(gold_texts)
# Turn probabilities into pseudo-labels and confidence weights.
weak_labels = [max(range(2), key=row.__getitem__) for row in weak_probs]
confidence = [max(row) for row in weak_probs]
classifier = LogisticRegression(max_iter=1000)
classifier.fit(X_weak, weak_labels, sample_weight=confidence)
predictions = classifier.predict(X_gold)
print(classification_report(
gold_labels,
predictions,
target_names=["negative", "positive"],
zero_division=0,
))The uncertain example, “It works as expected,” receives a confidence of 0.55. It contributes less to the loss than an example with confidence 0.95. This does not make the label correct, but it reduces the effect of uncertain pseudo-labels.
Hard versus soft targets
Hard pseudo-labels discard information. Probabilities of [0.51, 0.49] and [0.99, 0.01] both become the negative class, even though they express very different certainty.
A model that supports soft-target training can minimize cross-entropy directly against the probability vector. Another practical approach is to keep only examples above a confidence threshold, such as 0.8, and use hard labels for the rest. Compare these choices on the same gold set rather than assuming that more weakly labeled data is always better.
Exercise
Why should a low-confidence weak label usually have less influence during classifier training?
Show answer
It is more likely to be wrong, so weighting it less reduces the damage caused by noisy supervision while preserving useful training data.