Sub topic 3
Evaluating Weak Labels with a Small Gold Set
This chapter assumes you can train a text classifier, compute precision, recall and F1, and write simple regular expressions. You will also need the label matrix and label model from earlier chapters.
Evaluating with a small hand-labeled gold set
A gold set is a small group of examples that a person has labeled by hand. Weak labels are noisy, so you measure the final classifier on the gold set, never on the weak labels themselves. Keep the gold set out of all training and tuning steps. Even 100 to 200 carefully labeled examples can reveal whether the pipeline works.
def precision_recall_f1(gold, pred, positive='positive'):
tp = sum(g == p == positive for g, p in zip(gold, pred))
fp = sum(p == positive and g != positive for g, p in zip(gold, pred))
fn = sum(g == positive and p != positive for g, p in zip(gold, pred))
precision = tp / (tp + fp) if tp + fp else 0.0
recall = tp / (tp + fn) if tp + fn else 0.0
f1 = 2 * precision * recall / (precision + recall) if precision + recall else 0.0
return precision, recall, f1
gold = ['positive', 'negative', 'positive', 'negative']
pred = ['positive', 'positive', 'positive', 'negative']
print(precision_recall_f1(gold, pred)) # (0.667, 1.0, 0.8)Using an LLM as a labeling source
A large language model (LLM) can act as one more labeling source. Ask it the same question several times and count the answers. Agreement across samples signals a label you can trust more.
from collections import Counter
def llm_label(text, ask, k=5):
prompt = 'Classify sentiment as positive or negative. One word.\n\nReview: ' + text
answers = [ask(prompt).strip().lower().rstrip('.') for _ in range(k)]
valid = [a for a in answers if a in ('positive', 'negative')]
if not valid:
return None, 0.0
label, count = Counter(valid).most_common(1)[0]
return label, count / kFiltering synthetic labels by confidence and agreement
Keep a label only when its agreement reaches a threshold, such as 0.8. Discarding uncertain labels lowers coverage but raises label quality. Check the threshold against the gold set, not against your intuition.
Generating synthetic training text
Synthetic text comes from an LLM asked to write new examples for a given label. Generated text can fill gaps in rare classes. Pass every generated example through the same confidence filter as LLM labels before you use it.
Failure modes: correlated sources, bias, leakage and feedback loops
- Correlated sources: Two labeling functions that test the same keyword count as two votes, but they carry one opinion. The label model then becomes overconfident.
- Bias: A labeling function or LLM may favor certain dialects, topics or groups. Check error rates by subgroup on the gold set.
- Leakage: If gold examples appear in the training data or influenced the labeling function design, the scores look better than the real performance.
- Feedback loops: A model trained on LLM labels, which then generates synthetic text for the same model, can drift away from real language.
> The Kiro note below is a callout.
An iterative workflow for weak supervision projects
Start with a few labeling functions and a gold set. Measure coverage, overlap and conflict, then fix the weakest source. Repeat until the gold set score stops improving. Keep a log of each change so you can trace any regression.
Exercise
A labeling function fires on 90 percent of examples but agrees with the gold labels only 55 percent of the time. What should you do first?
Show answer
Inspect its errors on the gold set, then fix or remove it. Its high coverage does not help if its labels are mostly wrong, and a label model would give it weight it has not earned.