Sub topic 8
Label models: learning source accuracies without ground truth
Majority vote treats every labeling function (LF) as equally reliable. A label model drops that assumption. It estimates how accurate each source is from the way the sources agree and disagree, then uses those estimates to weight their votes. This chapter explains the idea, builds a small label model from scratch, and lists the assumptions it relies on.
This chapter assumes you can read a label matrix with abstentions (see the earlier chapters), know basic probability (conditional probability, independence), and can read NumPy code.
Why majority vote is not enough
Suppose three LFs vote on one example. LF A is 90% accurate and votes positive. LFs B and C are 55% accurate and both vote negative. Majority vote returns negative, although the one reliable source said positive. Majority vote also gives you no confidence value. A 3-to-0 vote and a 2-to-1 vote both just become a label.
You cannot fix this by measuring each LF's accuracy directly, because you have no labels. That is the reason you wrote LFs in the first place. The label model gets around this by treating the true label as a hidden variable.
The generative model
The label model describes how the votes were produced. For each example there is a true label y that you never observe. Each LF j then behaves like this:
- It abstains on some examples. Abstention carries no information about
y. - When it votes, it is correct with probability
a_j(its accuracy).
The model also assumes the LFs are conditionally independent given y. Once you know the true label, one LF's error tells you nothing about another LF's error.
With this model, the posterior for a binary task has a simple form. Define a weight w_j = log(a_j / (1 - a_j)). The log-odds of y = 1 are the prior log-odds plus the sum, over the LFs that voted, of +w_j for a vote of 1 and -w_j for a vote of 0. This is a weighted vote.
| Accuracy `a_j` | Weight `w_j` | Effect |
|---|---|---|
| 0.90 | about 2.2 | Strong influence |
| 0.60 | about 0.4 | Weak influence |
| 0.50 | 0 | Ignored |
| 0.30 | about -0.85 | Vote is flipped |
How accuracies are learned without ground truth
The observed agreement between two conditionally independent LFs depends only on their two accuracies. Accurate LFs agree with each other often. Inaccurate LFs agree rarely. With three or more LFs you can solve for each accuracy from the pairwise agreement rates alone. Snorkel's LabelModel does this by fitting the agreement statistics with a matrix-completion objective. A simpler route is expectation-maximization (EM), which repeats two steps:
- E-step. Given the current accuracies, compute the posterior
P(y = 1 | votes)for every example. - M-step. Given those posteriors, re-estimate each accuracy as the expected fraction of that LF's non-abstaining votes that were correct.
A label model in 40 lines
The code below simulates four LFs with known accuracies, fits the EM model, and compares it with majority vote. Because the data are simulated, you can check the learned accuracies against the true ones. With real data you cannot, which is why the gold set in a later chapter matters.
import numpy as np
ABSTAIN = -1
rng = np.random.default_rng(0)
n = 5000
y = rng.integers(0, 2, n)
def make_lf(acc, coverage):
fires = rng.random(n) < coverage
correct = rng.random(n) < acc
votes = np.where(correct, y, 1 - y)
return np.where(fires, votes, ABSTAIN)
true_acc = [0.90, 0.70, 0.60, 0.60]
coverage = [0.40, 0.80, 0.90, 0.90]
L = np.column_stack([make_lf(a, c) for a, c in zip(true_acc, coverage)])
def fit_label_model(L, n_iter=100):
mask = L != ABSTAIN
acc = np.full(L.shape[1], 0.7) # start above 0.5: sources beat chance
for _ in range(n_iter):
log1 = np.zeros(len(L))
log0 = np.zeros(len(L))
for j in range(L.shape[1]):
log1 += np.where(mask[:, j], np.where(L[:, j] == 1, np.log(acc[j]), np.log(1 - acc[j])), 0.0)
log0 += np.where(mask[:, j], np.where(L[:, j] == 0, np.log(acc[j]), np.log(1 - acc[j])), 0.0)
p1 = 1.0 / (1.0 + np.exp(log0 - log1)) # uniform class prior
p_correct = np.where(L == 1, p1[:, None], 1 - p1[:, None])
acc = (p_correct * mask).sum(axis=0) / mask.sum(axis=0)
acc = np.clip(acc, 0.01, 0.99)
return acc, p1
def majority_vote(L):
pos = (L == 1).sum(axis=1)
neg = (L == 0).sum(axis=1)
return np.where(pos > neg, 1, np.where(neg > pos, 0, ABSTAIN))
acc, p1 = fit_label_model(L)
print("true accuracies: ", true_acc)
print("learned accuracies:", acc.round(2))
mv = majority_vote(L)
decided = mv != ABSTAIN
print("majority vote accuracy:", (mv[decided] == y[decided]).mean().round(3))
print("label model accuracy: ", ((p1[decided] > 0.5).astype(int) == y[decided]).mean().round(3))The learned accuracies should land close to the true values. On the examples where majority vote makes a decision, the label model should be at least as accurate, because it can side with the strong LF against two weak ones. Rows where every LF abstained get p1 = 0.5, which correctly says "no information".
What the label model needs from you
Three assumptions drive the result.
Conditional independence. If two LFs make the same mistakes, the model reads their agreement as evidence that both are accurate. It then overweights them. To see this, append a copy of the third LF to the matrix and refit with np.column_stack([L, L[:, 2]]). The duplicated source gains influence it did not earn. Common real causes are near-duplicate regexes, two LFs built on the same lexicon, and several LFs that read the same feature. Snorkel can model known dependencies if you declare them, but you must know about them first.
Better-than-random sources. The equations are symmetric. A model in which every LF is mostly right fits the data as well as one in which every LF is mostly wrong, and the labels are swapped. Starting accuracies above 0.5 picks the first solution. If most of your LFs are worse than chance, the model can converge to the flipped one.
Enough overlap. Accuracy is estimated from agreement, so an LF that never overlaps with any other LF cannot be estimated. Very low coverage also makes the estimate noisy. Check the coverage, overlap and conflict numbers before fitting.
Using the Snorkel LabelModel
Snorkel's LabelModel takes the same label matrix, with -1 for abstain, and fits by gradient descent on the agreement objective. Its useful arguments are:
cardinality: the number of classes.n_epochs,lr,l2: optimizer settings. If the fit looks unstable, lowerlrfirst.class_balance: the class prior, such as[0.8, 0.2]. Without it the model assumes a uniform prior, which hurts on skewed tasks like spam or toxicity.seed: for reproducible fits.
After fitting, predict_proba(L) returns an (n, cardinality) array of probabilistic labels. predict(L, tie_break_policy="abstain") returns hard labels and leaves ties unlabeled instead of picking a class at random. The next chapter shows how to train on the probabilities directly.
Majority vote or label model?
| Situation | Better choice |
|---|---|
| Fewer than three LFs, or very little overlap | Majority vote. There is too little signal to estimate accuracies. |
| LFs of very different quality | Label model. |
| Many near-duplicate LFs | Majority vote or a label model with declared dependencies, after you check correlation. |
| Skewed class balance | Label model with `class_balance` set. |
| You need a quick baseline | Majority vote. Compare the label model against it. |
If the label model does not beat majority vote on your gold set, the extra machinery is not paying for itself, and you should stay with the simpler method.
Exercise
Three LFs vote on one example. LF A (accuracy 0.9) votes positive. LFs B and C (accuracy 0.55 each) vote negative. Using the weights w = log(a / (1 - a)) with a uniform prior, does the label model predict positive or negative? Use log(9) ≈ 2.20 and log(0.55 / 0.45) ≈ 0.20.
Show answer
Positive. The log-odds of positive are +2.20 from A, -0.20 from B and -0.20 from C, which sums to +1.80. That is a posterior of about 0.86 for positive. Majority vote would have returned negative.
Exercise
You add a fourth LF that is an exact copy of your strongest LF. Why does the EM label model become overconfident, and how can you detect the problem before training?
Show answer
The model assumes the LFs are conditionally independent, so the copy's agreement with the original is counted as extra independent evidence. The model then multiplies the same evidence twice and overweights it. You can detect it by computing pairwise agreement or correlation between LF columns, where a copy shows near-perfect agreement on shared coverage. You can also check whether the label model's accuracy on a gold set drops when you remove the suspected duplicate.