Sub topic 9
From votes to probabilistic labels
A majority vote returns one class per example, or an abstention. It throws away how many sources voted, how reliable they are, and how much they disagreed. This chapter shows how to keep that information as a probability for each class, and how to train on it.
This chapter assumes you can read the label matrix from the earlier chapters (rows are examples, columns are labeling functions, -1 means abstain). It also assumes you know conditional probability and independence, and have trained a text classifier with scikit-learn.
Why hard labels lose information
Take the four reviews from the majority-vote example. The vote gives [1, 0, -1, -1]. Two different situations both become -1:
- "Thank you, but it is terrible": two sources voted and disagreed.
- "It is a phone": no source voted.
The first example carries weak evidence for each side. The second carries none. A hard label hides this difference. Even the examples that did get a label look the same to the classifier. One backed by three agreeing sources and one backed by a single weak source both become a 1.
A probabilistic label (also called a soft label) is a vector of class probabilities for one example, such as [0.61, 0.39] for [NEG, POS]. It records both the best guess and the confidence in it.
A simple way to get probabilities
The easiest approach is the vote share: the fraction of non-abstaining sources that voted for each class. It is cheap, but it treats a source with 95% accuracy the same as one with 55%. It also gives certainty ([0.0, 1.0]) whenever a single source votes.
A label model does better by weighting each vote by the source's estimated accuracy. Under a simplifying assumption, this is naive Bayes. Assume that, given the true class, the sources vote independently. Assume source j is correct with probability a_j whenever it does not abstain. For two classes, the posterior for an example is:
P(y = c | votes) ∝ P(y = c) × ∏ over non-abstaining j of (aj if votej = c, else 1 − a_j)
Abstaining sources contribute nothing. You then normalize so the two scores sum to 1. A label model such as Snorkel's estimates the accuracies a_j from the agreement patterns in the label matrix, without ground truth. Here we plug in assumed accuracies so you can see the mechanism.
ABSTAIN, NEG, POS = -1, 0, 1
# Rows: the four texts. Columns: lf_positive_words, lf_negative_words, lf_thanks.
L = [
[POS, ABSTAIN, ABSTAIN], # I love this phone, great battery
[ABSTAIN, NEG, ABSTAIN], # Arrived broken, I want a refund
[ABSTAIN, NEG, POS], # Thank you, but it is terrible
[ABSTAIN, ABSTAIN, ABSTAIN], # It is a phone
]
accs = [0.8, 0.7, 0.6] # assumed accuracy of each function when it votes
def posterior_pos(row, accs, prior_pos=0.5):
pos, neg = prior_pos, 1 - prior_pos
for vote, acc in zip(row, accs):
if vote == ABSTAIN:
continue
pos *= acc if vote == POS else 1 - acc
neg *= acc if vote == NEG else 1 - acc
return pos / (pos + neg)
for row in L:
p = posterior_pos(row, accs)
print([round(1 - p, 3), round(p, 3)])
# [0.2, 0.8]
# [0.7, 0.3]
# [0.609, 0.391]
# [0.5, 0.5]The tied example now has an answer: the lf_negative_words function (70% accurate) outweighs lf_thanks (60%), so the label leans negative at 0.609. The all-abstain row falls back to the prior, which says only that the label is unknown.
| Text | Majority vote | Soft label [NEG, POS] |
|---|---|---|
| I love this phone, great battery | POS | [0.20, 0.80] |
| Arrived broken, I want a refund | NEG | [0.70, 0.30] |
| Thank you, but it is terrible | ABSTAIN | [0.61, 0.39] |
| It is a phone | ABSTAIN | [0.50, 0.50] |
Note that a single vote from an 80%-accurate source gives 0.8, not 1.0. The probability reflects the source's error rate.
Training on soft labels
Two standard ways to use probabilistic labels:
- Soft cross-entropy. The loss for one example is −Σc pc log q_c, where p is the soft label and q is the classifier's predicted distribution. Neural frameworks support this directly by accepting a probability vector as the target.
- Weighted duplication. Many scikit-learn estimators accept only hard labels. You can add each example once per class, labeled with that class and weighted by its probability. This minimizes the same expected loss.
The second approach looks like this:
import numpy as np
from scipy.sparse import vstack
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
texts = [
'I love this phone, great battery',
'Arrived broken, I want a refund',
'Thank you, but it is terrible',
'It is a phone',
]
probs = np.array([[0.2, 0.8], [0.7, 0.3], [0.609, 0.391], [0.5, 0.5]])
# Drop rows where the label model has no information.
keep = np.abs(probs[:, 1] - 0.5) > 0.01
texts = [t for t, k in zip(texts, keep) if k]
probs = probs[keep]
X = TfidfVectorizer().fit_transform(texts)
n = X.shape[0]
X_dup = vstack([X, X]) # every example appears twice
y_dup = np.array([0] * n + [1] * n) # once as NEG, once as POS
w_dup = np.concatenate([probs[:, 0], probs[:, 1]])
clf = LogisticRegression().fit(X_dup, y_dup, sample_weight=w_dup)
print(clf.predict_proba(X).round(2))With only three examples the output is not meaningful. The point is the pattern: one weight per (example, class) pair, taken straight from the label model.
Hard labels from soft ones
Sometimes you need hard labels, for example to hand a dataset to a tool that accepts only a class. Take the argmax, and decide separately what to do with uncertain rows. Common choices:
- Keep only rows where the top probability is at least a threshold such as 0.7. This gives cleaner training data but fewer rows, and the kept rows skew toward examples that the labeling functions cover well.
- Keep all rows with argmax labels. This keeps coverage but injects every error the label model makes.
Soft training avoids this trade-off, because uncertain rows get low weight instead of being thrown out or fully trusted.
Common errors
- Training on rows nobody voted on. An all-abstain row has probability equal to the prior. If you give it a hard label, you add a coin flip, or a bias toward the majority class. Drop these rows or weight them near zero.
- Reading the probabilities as calibrated. The posterior is only as accurate as the independence assumption and the accuracy estimates. Three labeling functions that all fire on the word "great" are not three independent witnesses, so the model counts the same evidence three times and the probability comes out too high. Check calibration on the gold set, for example by comparing average predicted confidence to observed accuracy in bins.
- Ignoring the class prior. If 90% of your data is positive, a uniform prior of 0.5 shifts every posterior toward the wrong class. Estimate the prior from a gold sample or the label model, and pass it in.
- Confusing accuracy with coverage. A source that rarely votes can have a high estimated accuracy from few votes. That estimate is noisy. Check how many votes each accuracy rests on.
- Using the soft labels as the evaluation target. Probabilistic labels are training targets. Measure quality only against hand labels.
Key points
- A hard vote cannot tell "sources disagree" from "no source voted". A probability can.
- A label model weights each vote by estimated source accuracy and combines votes with a class prior. The independence assumption is its main weakness.
- Train with a soft loss, or with weighted duplication when the library accepts only hard labels.
- Drop or down-weight rows where every source abstained.
- Probabilities are not automatically calibrated. Check them against a gold set.
Exercise
Using the accuracies 0.8, 0.7 and 0.6 and a uniform prior, a review gets votes POS from lfpositivewords, NEG from lfnegativewords, and POS from lf_thanks. What is P(POS)? Does it beat a majority vote's answer?
Show answer
POS score = 0.5 × 0.8 × (1 − 0.7) × 0.6 = 0.072. NEG score = 0.5 × (1 − 0.8) × 0.7 × (1 − 0.6) = 0.028. P(POS) = 0.072 / (0.072 + 0.028) = 0.72. The majority vote also says POS (two votes to one), but the soft label adds the confidence of 0.72 instead of 1.0. It also reflects that the NEG source is fairly reliable. Note that if the three functions are correlated, 0.72 may be overstated.
Exercise
A team trains a classifier on argmax labels for all rows, including the 30% of rows where no labeling function voted. Their score on the weak labels is 0.94, but on a gold set it is 0.71. Name two problems and one fix for each.
Show answer
First, the all-abstain rows received arbitrary labels (the prior's argmax), which adds noise. Fix: drop those rows or give them near-zero weight. Second, the 0.94 score was measured against weak labels, so it reflects agreement with the label model, not accuracy. Fix: report only the gold-set score, and compare against a majority-vote baseline on the same gold set.