Sub topic 3
Writing Labeling Functions That Can Abstain
Prerequisites and the labeling-function contract
This chapter assumes that you know basic Python, regular expressions, and supervised text classification. You should also know that a classifier needs examples paired with target labels.
A labeling function is a small program that examines one text example and returns one of three things:
- A class label, such as
POSorNEG. ABSTAIN, meaning that the rule does not have enough evidence.- In some systems, a numeric value representing the label.
A labeling function is a noisy source of information. It does not need to label every example, and it should not guess when its rule does not apply. Several labeling functions can cover different parts of the dataset.
import re
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(text):
"""Return POS when the text contains a strong positive word."""
if re.search(r"\b(great|excellent|love)\b", text, re.IGNORECASE):
return POS
return ABSTAIN
def lf_negative_words(text):
"""Return NEG when the text contains a strong negative word."""
if re.search(r"\b(refund|broken|terrible)\b", text, re.IGNORECASE):
return NEG
return ABSTAIN
def lf_thanks(text):
"""Return POS when the text expresses thanks."""
if "thank" in text.lower():
return POS
return ABSTAIN
texts = [
"I love this phone",
"The screen is broken",
"Thank you for the quick delivery",
"It is a phone",
]
labeling_functions = [
lf_positive_words,
lf_negative_words,
lf_thanks,
]
for text in texts:
votes = [lf(text) for lf in labeling_functions]
print(text, votes)The output contains one vote from each source for each text. A value of -1 is abstention. For example, the last text has no useful keyword, so all functions abstain.
Choosing when to abstain
A useful labeling function has a clear scope. The positive-word function covers texts with words such as “great,” but it abstains on texts that express positive sentiment in another way. This limited behavior is usually safer than assigning POS to every text that does not contain a negative word.
Consider these properties when writing a function:
- Precision: When the function votes, how often is the vote correct?
- Coverage: What fraction of examples receive a vote?
- Specificity: Does the rule identify a meaningful pattern, or does it match many unrelated texts?
- Conflict: Can the rule disagree with another useful rule?
A function can use keywords, regular expressions, metadata, or an external resource. Keep each function focused on one idea. This makes its behavior easier to inspect and revise.
Combining functions later
The outputs can be arranged in a label matrix. Rows represent texts, and columns represent labeling functions.
At this stage, do not assume that one function is always right. Later steps can compare coverage and conflicts, use majority vote as a baseline, or fit a label model that estimates source reliability.
Exercise
Exercise
What should a labeling function return for the text "The delivery was late" if it only recognizes the words "great", "excellent", and "love"?
Show answer
It should return ABSTAIN, because none of its positive rules apply and the text has not been proven negative.