声明类型推断验证状态待复核
训练评论分类AI需要10000条人工标注数据花3万元等3周——弱监督用15个规则函数自动标注全部数据1天完成成本近零,标注效率提升10-50倍支持多个AI项目年化节省数据标注成本20-50万元
Skill-Weak-Supervision-Data-Labeling
训练评论分类AI需要10000条人工标注数据花3万元等3周——弱监督用15个规则函数自动标注全部数据1天完成成本近零,标注效率提升10-50倍支持多个AI项目年化节省数据标注成本20-50万元
¥20-50 万
待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源
训练评论分类AI需要10000条人工标注数据花3万元等3周——弱监督用15个规则函数自动标注全部数据1天完成成本近零,标注效率提升10-50倍支持多个AI项目年化节省数据标注成本20-50万元
人工标注 vs 弱监督标注:
人工标注(传统):
10,000 条评论 × ¥3/条 = ¥30,000
时间:2-3 周
结果:干净标签,但覆盖范围有限
弱监督(Snorkel 框架):
编写 10-20 个标注函数(规则/启发式/远程监督)
→ 每个函数自动标注所有数据(可能有噪音)
→ 标签模型(Label Model)融合多个函数的投票
→ 输出概率软标签(不是0/1,而是0.8/0.2)
成本:¥0(仅工程师时间),时间:1-2天标注函数(Labeling Function, LF)类型:
# 关键词规则
def lf_noisy_pump(text):
return POSITIVE if 'noisy' in text.lower() else ABSTAIN
# 正则规则
def lf_db_mention(text):
return NEGATIVE if re.search(r'<\s*45\s*dB', text) else ABSTAIN
# 外部知识(远程监督)
def lf_return_keywords(text):
return NEGATIVE if any(w in text for w in ['returned', 'refund', 'disappointed']) else ABSTAIN
# 模型弱标签
def lf_sentiment_model(text):
score = simple_sentiment(text)
return POSITIVE if score > 0.8 else (NEGATIVE if score < 0.2 else ABSTAIN)标签模型(Label Model):
多个标注函数的输出通过生成式图模型融合:
P(Y | \lambda_1, \lambda_2, ..., \lambda_m)
模型自动学习各标注函数的准确率和相关性,输出加权融合的概率标签。
业务问题:要训练一个"母婴产品评论质量分类器"(高质量/低质量),需要 10,000 条标注数据。人工标注 ¥30,000 + 3 周时间。用弱监督:写 15 个标注函数,1 天完成标注,成本近零。
数据要求: - 未标注的评论数据(10,000+ 条) - 领域知识(用于设计标注函数)
预期产出: - 每条数据的软标签(P(高质量)=0.78) - 标注函数质量分析(哪个函数准确率最高) - 可用于训练的弱标签数据集
代码块数量:3
"""
Weak Supervision Data Labeling
弱监督数据标注:Snorkel 风格的规则函数融合
"""
import re
import numpy as np
from dataclasses import dataclass
from collections import defaultdict
# 标签常量
POSITIVE = 1
NEGATIVE = 0
ABSTAIN = -1 # 该函数不确定,放弃投票
def lf_specific_details(text: str) -> int:
"""有具体细节(数字/场景/品类词)→ 高质量"""
if len(re.findall(r'\d+', text)) >= 2 or len(text.split()) >= 50:
return POSITIVE
return ABSTAIN
def lf_empty_exclamation(text: str) -> int:
"""过多感叹号/空洞赞美 → 低质量"""
exclaim_ratio = text.count('!') / max(len(text.split()), 1)
generic = sum(1 for w in ['amazing', 'perfect', 'love it', 'great'] if w in text.lower())
if exclaim_ratio > 0.15 or (generic >= 3 and len(text.split()) < 30):
return NEGATIVE
return ABSTAIN
def lf_balanced_review(text: str) -> int:
"""同时提优点和缺点 → 高质量"""
positive_words = ['good', 'great', 'love', 'excellent', 'like', 'nice']
negative_words = ['but', 'however', 'although', 'downside', 'issue', 'problem', 'cons']
text_lower = text.lower()
has_positive = any(w in text_lower for w in positive_words)
has_negative = any(w in text_lower for w in negative_words)
if has_positive and has_negative:
return POSITIVE
return ABSTAIN
def lf_verified_purchase_proxy(text: str) -> int:
"""提到使用时长/场景 → 可能是真实用户 → 高质量"""
usage_patterns = [r'\d+\s*(month|week|day|hour)', r'(office|travel|night|morning|work)', r'(used|using) (for|it|since)']
if any(re.search(p, text.lower()) for p in usage_patterns):
return POSITIVE
return ABSTAIN
def lf_too_short(text: str) -> int:
"""过短 → 低质量"""
if len(text.split()) < 15:
return NEGATIVE
return ABSTAIN
def lf_competitor_mention(text: str) -> int:
"""提到竞品对比 → 高质量(有参照系)"""
competitors = ['medela', 'momcozy', 'spectra', 'lansinoh', 'haakaa', 'elvie']
if any(c in text.lower() for c in competitors):
return POSITIVE
return ABSTAIN
# 所有标注函数列表
LABELING_FUNCTIONS = [
lf_specific_details,
lf_empty_exclamation,
lf_balanced_review,
lf_verified_purchase_proxy,
lf_too_short,
lf_competitor_mention,
]
def apply_labeling_functions(text: str) -> list[int]:
"""应用所有标注函数,返回投票结果"""
return [lf(text) for lf in LABELING_FUNCTIONS]
def label_model_majority_vote(votes: list[int]) -> tuple[float, float]:
"""
简化版标签模型:加权多数投票
生产用: from snorkel.labeling import LabelModel
"""
positive_votes = sum(1 for v in votes if v == POSITIVE)
negative_votes = sum(1 for v in votes if v == NEGATIVE)
total_votes = positive_votes + negative_votes
if total_votes == 0:
return 0.5, 0.5 # 无法确定
p_positive = positive_votes / total_votes
p_negative = negative_votes / total_votes
return p_positive, p_negative
def analyze_lf_coverage(texts: list[str]) -> dict:
"""分析各标注函数的覆盖率和冲突率"""
lf_stats = {lf.__name__: {'pos': 0, 'neg': 0, 'abstain': 0} for lf in LABELING_FUNCTIONS}
for text in texts:
for lf in LABELING_FUNCTIONS:
vote = lf(text)
if vote == POSITIVE:
lf_stats[lf.__name__]['pos'] += 1
elif vote == NEGATIVE:
lf_stats[lf.__name__]['neg'] += 1
else:
lf_stats[lf.__name__]['abstain'] += 1
n = len(texts)
for name, stats in lf_stats.items():
stats['coverage'] = (stats['pos'] + stats['neg']) / n
stats['pos_rate'] = stats['pos'] / max(stats['pos'] + stats['neg'], 1)
return lf_stats
def weak_supervision_label(texts: list[str],
confidence_threshold: float = 0.6) -> list[dict]:
"""对文本列表进行弱监督标注"""
results = []
for text in texts:
votes = apply_labeling_functions(text)
p_pos, p_neg = label_model_majority_vote(votes)
label = 'high_quality' if p_pos > confidence_threshold else (
'low_quality' if p_neg > confidence_threshold else 'uncertain')
results.append({
'text': text[:60] + '...',
'p_positive': round(p_pos, 3),
'p_negative': round(p_neg, 3),
'label': label,
'votes': votes,
})
return results
def run_weak_supervision_demo():
print('=' * 65)
print('Weak Supervision Data Labeling — 弱监督数据标注')
print('=' * 65)
sample_reviews = [
"Absolutely perfect! Love love love it!!! Best purchase ever!!!",
"Used this for 3 months at my office. Quiet enough (under 45dB) and suction is comparable to hospital grade. However, the flanges are small - had to order size L separately.",
"ok",
"Better than Medela in terms of noise. The USB charging is convenient for travel. Motor feels a bit plasticky though.",
"Stopped working after 2 weeks. Very disappointing. The suction became weak suddenly.",
"Great for nighttime pumping. My baby doesn't wake up. Hospital strength as claimed.",
"Bought this as a gift for my sister. She loves it.",
]
print(f'\n📊 标注函数覆盖分析:')
stats = analyze_lf_coverage(sample_reviews)
print(f' {"函数名":<28} {"覆盖率":>8} {"正向率":>8}')
print(' ' + '-' * 48)
for name, s in stats.items():
print(f' {name:<28} {s["coverage"]:>8.1%} {s["pos_rate"]:>8.1%}')
print(f'\n🏷️ 弱监督标注结果:')
results = weak_supervision_label(sample_reviews)
for r in results:
icon = {'high_quality': '✅', 'low_quality': '❌', 'uncertain': '⚠️ '}[r['label']]
votes_str = str(r['votes']).replace('-1', 'A').replace('1', 'P').replace('0', 'N')
print(f' {icon} [{r["label"]}] P+={r["p_positive"]:.2f} | {r["text"][:50]}')
labeled = sum(1 for r in results if r['label'] != 'uncertain')
print(f'\n 标注覆盖率: {labeled}/{len(results)} ({labeled/len(results):.0%})')
print(f' 标注成本: 近零(vs 人工¥{len(sample_reviews)*3})')
print('\n[✓] Weak Supervision Data Labeling 测试通过')
if __name__ == '__main__':
run_weak_supervision_demo()论文链接用于核验方法来源,不自动证明本页商业结果。