Sub topic 16
An iterative workflow for weak supervision projects
This chapter assumes you have read the earlier chapters. You should know how to write labeling functions that abstain, build a label matrix, read coverage, overlap and conflict, and use a label model, an end model and a gold set. Here those pieces are combined into a repeatable loop. A weak supervision project is rarely finished in one pass, so the order of the steps and the stopping rule matter as much as any single technique.
Why a workflow is needed
Weak supervision gives you many cheap knobs: new labeling functions, new thresholds, a different label model, an LLM source, a different end model. With that many knobs, you can spend a week changing things and not know which change helped. The usual causes are:
- No fixed measuring stick, so progress is judged by impression.
- Labeling functions written while looking at the data used for scoring, so the scores are inflated.
- Changes made in bulk, so a gain or a regression cannot be traced to a cause.
The workflow below addresses these problems. It fixes the data splits first, changes one thing per iteration, and logs every iteration.
The loop
Step 0: Fix the splits before writing anything
Create three sets and keep them separate for the whole project:
| Split | Labels | Used for |
|---|---|---|
| Unlabeled pool | None | Applying labeling functions, fitting the label model, training the end model |
| Development set | Hand-labeled, small (often 100 to 500 examples) | Reading errors, tuning, choosing between iterations |
| Test set | Hand-labeled, small, never inspected | One final estimate |
Two rules apply. First, deduplicate across the splits. Near-duplicate reviews, quoted replies and templated emails that appear in both the pool and the test set leak information and make the final score look better than it is. Second, hand-label the dev and test sets with a written guideline, so that "positive" means the same thing in every iteration. If two people label a sample, measure their agreement. That agreement is a rough ceiling on what any model can reach.
Step 1: Write or revise labeling functions
Start with the cheapest high-precision sources: a lexicon, a few regular expressions, a metadata field. Then read examples from the unlabeled pool, not the test set, and write functions for patterns you see. In later iterations, write functions that target the errors found in Step 6.
A labeling function should state its claim narrowly. A function that fires on "refund" and votes negative is easy to reason about. A function that tries to cover everything will fire often and be wrong often, and the label model cannot fully repair that.
Step 2: Diagnose before modeling
Apply every function to the pool and look at the per-function summary before fitting anything. Check these things:
- A function with near-zero coverage adds nothing and can be dropped or broadened.
- A function with very high coverage and many conflicts is probably too broad.
- Two functions with identical votes on almost every example are likely correlated, and the label model will count them as independent evidence.
- A class that no function ever votes for cannot be learned.
If you have a dev set, also compute each function's empirical accuracy on the dev examples where it fires. This is a diagnostic only. Do not copy those accuracies into the label model as if they were ground truth for the pool.
Step 3: Fit the label model and compare with majority vote
Fit the label model on the pool's label matrix. Keep majority vote as a permanent baseline, because it is simple and has nothing to overfit. If the label model does not beat majority vote on the dev set, the usual causes are too few functions, strongly correlated functions, or a class balance that the model estimates badly. Fix those before trying a larger model.
Step 4: Train the end model
Train the classifier on the pool using the probabilistic labels. Drop examples where every function abstained, since they carry no label signal. The end model's purpose is to generalize past the exact patterns your functions encode, so confirm that its features are not just the keywords your functions test. A model that sees only "refund" will learn nothing beyond the labeling function.
Step 5: Evaluate on the dev set
Report precision, recall and F1 on the dev set for the label model's output, for majority vote, and for the end model. Comparing all three shows where the gain comes from. If the end model scores below the label model's own labels, the features or model class are the problem, not the labels.
Step 6: Error analysis and the next change
Read the dev errors, grouped by type. Each group suggests a specific action:
| Error pattern | Likely cause | Next change |
|---|---|---|
| Gold is positive, all functions abstained | Coverage gap | Add a function for that pattern |
| Gold is negative, a function voted positive | Function too broad | Narrow its pattern or add a guard |
| Several functions agree and are wrong | Correlated sources or label bias | Remove or merge functions, check the guideline |
| Labels look right but the end model errs | Model or feature problem | Change the end model, not the labels |
| Gold label itself looks wrong | Guideline ambiguity | Fix the guideline and relabel |
Make one change, then rerun the loop from Step 2. A single change per iteration is what lets you attribute a score change to a cause.
Logging each iteration
A small harness keeps the comparison honest. The script below applies a set of labeling functions, combines them with majority vote (ties abstain), and reports coverage, precision, recall and F1 for the positive class. Abstentions count as "not positive" for precision and recall. It compares two iterations of the function set.
import re
from collections import Counter
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(t):
return POS if re.search(r'\b(great|excellent|love)\b', t, re.I) else ABSTAIN
def lf_negative_words(t):
return NEG if re.search(r'\b(refund|broken|terrible)\b', t, re.I) else ABSTAIN
def lf_returned(t):
return NEG if re.search(r'\b(returned|not worth)\b', t, re.I) else ABSTAIN
def lf_works_well(t):
return POS if re.search(r'\bworks well\b', t, re.I) else ABSTAIN
def majority_vote(row):
votes = [v for v in row if v != ABSTAIN]
if not votes:
return ABSTAIN
ranked = Counter(votes).most_common()
if len(ranked) > 1 and ranked[0][1] == ranked[1][1]:
return ABSTAIN
return ranked[0][0]
def evaluate(lfs, data):
preds = [majority_vote([lf(t) for lf in lfs]) for t, _ in data]
coverage = sum(p != ABSTAIN for p in preds) / len(data)
tp = sum(p == POS and y == POS for p, (_, y) in zip(preds, data))
fp = sum(p == POS and y != POS for p, (_, y) in zip(preds, data))
fn = sum(p != POS and y == POS for p, (_, y) in zip(preds, data))
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 {'coverage': round(coverage, 2), 'precision': round(precision, 2),
'recall': round(recall, 2), 'f1': round(f1, 2)}
dev = [
('I love this phone, great battery', POS),
('Arrived broken, I want a refund', NEG),
('Terrible screen, stopped working', NEG),
('Excellent value', POS),
('Not worth it, returned it', NEG),
('Works well and arrived early', POS),
]
iterations = {
'v1': [lf_positive_words, lf_negative_words],
'v2': [lf_positive_words, lf_negative_words, lf_returned, lf_works_well],
}
for name, lfs in iterations.items():
print(name, evaluate(lfs, dev))Version v1 covers four of six dev examples and finds two of the three positives. Version v2 covers all six, and every metric reaches 1.0.
That perfect score is a warning, not a result. The new functions were written by reading these same six dev examples, so the dev set no longer measures generalization. In a real project, write functions from the unlabeled pool and from error patterns, not from copying individual dev examples. Expect the dev score to rise less than it did here, and expect the test score to be lower still.
When to stop
Decide the stopping rule before you begin, so that it is not chosen after seeing the scores. Reasonable rules include:
- The dev F1 meets the target the downstream use needs.
- Two consecutive iterations improve dev F1 by less than the noise you measured. With a few hundred dev examples, differences of one or two points are often noise. Bootstrap the dev set to see the spread.
- The remaining errors are mostly gold-label disagreements, which more labeling functions cannot fix.
- The cost of another iteration exceeds the cost of hand-labeling more examples directly. Weak supervision is a trade, and sometimes the better choice is to label 300 more examples by hand.
When a rule triggers, evaluate on the test set once and report that number. Do not tune after seeing it.
Habits that keep the project maintainable
- Keep each labeling function in version control with a short comment on what it targets and why it votes as it does.
- Record, for each iteration, the function set, label model settings, random seed, end model settings and dev metrics. Without this, a regression cannot be traced.
- Re-run the full loop when the data changes. A labeling function that worked on last quarter's reviews can drift when product names, slang or the class balance shift.
- Track the label model's estimated class balance against the dev set's real balance. A large gap usually means a biased set of functions.
- If an LLM is one of the sources, store its model version and prompt with the iteration. The same prompt can give different labels after a model update.
Exercise
After iteration 3, your dev F1 rose from 0.71 to 0.78. You also changed the end model's learning rate and added two new labeling functions in that same iteration. A colleague asks which change helped. What can you say, and what should you do differently next time?
Show answer
You cannot say, because two kinds of change were made at once and the dev score reflects both. Next time make one change per iteration, rerun diagnostics and evaluation, and log the result. To recover now, rerun with only the new labeling functions and with only the learning rate change, and compare both to the 0.71 baseline. Also check whether the 0.07 gain exceeds the noise from bootstrapping the dev set.
Exercise
In the harness above, a teammate adds a labeling function after reading a dev example "Works well and arrived early" and copying its wording into a regex. Dev F1 goes up. Why is this not good evidence of improvement, and where should the function have come from?
Show answer
The function was built from a dev example, so the dev set now partly measures how well the function memorized that example, not how well it generalizes. The function should have come from reading unlabeled pool examples, or from error patterns grouped across many dev errors, and the final judgment should come from the untouched test set.