Files
2026-07-13 21:37:14 +08:00

9.0 KiB
Raw Permalink Blame History

name, description, metadata
name description metadata
metric-selection-guide-llm-evaluation LLM 评估指标选择指南
type
reference

LLM 评估指标选择指南

本文档为不同评估场景下的指标选择提供参考。

指标类别

分类指标

适用于二元或多分类评估任务(通过/不通过,正确/不正确)。

精确率

Precision = True Positives / (True Positives + False Positives)

解释:在被评判为「好」的所有回答中,实际好的比例是多少?

适用场景:误报(假阳性)代价较高时(例如批准不安全的内容)

def precision(predictions, ground_truth):
    true_positives = sum(1 for p, g in zip(predictions, ground_truth) if p == 1 and g == 1)
    predicted_positives = sum(predictions)
    return true_positives / predicted_positives if predicted_positives > 0 else 0

召回率

Recall = True Positives / (True Positives + False Negatives)

解释:在所有实际好的回答中,评判者识别出了多少?

适用场景:漏报(假阴性)代价较高时(例如过滤中遗漏了优质内容)

def recall(predictions, ground_truth):
    true_positives = sum(1 for p, g in zip(predictions, ground_truth) if p == 1 and g == 1)
    actual_positives = sum(ground_truth)
    return true_positives / actual_positives if actual_positives > 0 else 0

F1 分数

F1 = 2 * (Precision * Recall) / (Precision + Recall)

解释:精确率与召回率的调和平均数

适用场景:需要一个能平衡两者的单一数值时

def f1_score(predictions, ground_truth):
    p = precision(predictions, ground_truth)
    r = recall(predictions, ground_truth)
    return 2 * p * r / (p + r) if (p + r) > 0 else 0

一致性指标

适用于比较自动化评估与人工判断。

Cohen's Kappa(κ 系数)

κ = (Observed Agreement - Expected Agreement) / (1 - Expected Agreement)

解释:排除了随机一致性的实际一致性

  • κ > 0.8:几乎完全一致
  • κ 0.60.8:高度一致
  • κ 0.40.6:中等一致
  • κ < 0.4:一致性较差

适用场景:二元或分类判断

def cohens_kappa(judge1, judge2):
    from sklearn.metrics import cohen_kappa_score
    return cohen_kappa_score(judge1, judge2)

加权 Kappa

适用于不一致严重程度有差别的有序量表:

def weighted_kappa(judge1, judge2):
    from sklearn.metrics import cohen_kappa_score
    return cohen_kappa_score(judge1, judge2, weights='quadratic')

解释:对较大分歧的惩罚力度高于小分歧

相关性指标

适用于有序/连续评分。

Spearman 秩相关系数(ρ)

解释:衡量排名之间的相关性,而非绝对值

  • ρ > 0.9:非常强的相关性
  • ρ 0.70.9:强相关性
  • ρ 0.50.7:中等相关性
  • ρ < 0.5:弱相关性

适用场景:顺序比具体数值更重要时

def spearmans_rho(scores1, scores2):
    from scipy.stats import spearmanr
    rho, p_value = spearmanr(scores1, scores2)
    return {'rho': rho, 'p_value': p_value}

Kendall 秩相关系数(τ)

解释:与 Spearman 类似,但基于配对一致性

适用场景:数据中存在大量并列值时

def kendalls_tau(scores1, scores2):
    from scipy.stats import kendalltau
    tau, p_value = kendalltau(scores1, scores2)
    return {'tau': tau, 'p_value': p_value}

Pearson 相关系数(r

解释:评分之间的线性相关性

适用场景:评分的实际数值有意义,而不仅仅是排序时

def pearsons_r(scores1, scores2):
    from scipy.stats import pearsonr
    r, p_value = pearsonr(scores1, scores2)
    return {'r': r, 'p_value': p_value}

成对比较指标

一致率

Agreement = (Matching Decisions) / (Total Comparisons)

解释:简单的百分比一致性

def pairwise_agreement(decisions1, decisions2):
    matches = sum(1 for d1, d2 in zip(decisions1, decisions2) if d1 == d2)
    return matches / len(decisions1)

位置一致性

Consistency = (Consistent across position swaps) / (Total comparisons)

解释:交换位置后,判断结果发生变化的频率有多高?

def position_consistency(results):
    consistent = sum(1 for r in results if r['position_consistent'])
    return consistent / len(results)

选择决策树

评估任务的类型是什么?
│
├── 二元分类(通过/不通过)
│   └── 使用:Precision、Recall、F1、Cohen's κ
│
├── 有序量表(1–5 评分)
│   ├── 与人工判断比较?
│   │   └── 使用:Spearman's ρ、Weighted κ
│   └── 比较两个自动化评判?
│       └── 使用:Kendall's τ、Spearman's ρ
│
├── 成对偏好
│   └── 使用:一致率、位置一致性
│
└── 多标签分类
    └── 使用:Macro-F1、Micro-F1、逐标签指标

按使用场景选择指标

使用场景 1:验证自动化评估

目标:确保自动化评估与人工判断相关

推荐指标

  1. 主要指标:Spearman's ρ(有序量表)或 Cohen's κ(分类)
  2. 次要指标:逐标准一致性
  3. 诊断指标:混淆矩阵(用于系统误差分析)
def validate_automated_eval(automated_scores, human_scores, criteria):
    results = {}
    
    # Overall correlation
    results['overall_spearman'] = spearmans_rho(automated_scores, human_scores)
    
    # Per-criterion agreement
    for criterion in criteria:
        auto_crit = [s[criterion] for s in automated_scores]
        human_crit = [s[criterion] for s in human_scores]
        results[f'{criterion}_spearman'] = spearmans_rho(auto_crit, human_crit)
    
    return results

使用场景 2:比较两个模型

目标:判断哪个模型产出更优

推荐指标

  1. 主要指标:胜率(来自成对比较)
  2. 次要指标:位置一致性(偏差检查)
  3. 诊断指标:逐标准细分
def compare_models(model_a_outputs, model_b_outputs, prompts):
    results = []
    for a, b, p in zip(model_a_outputs, model_b_outputs, prompts):
        comparison = await compare_with_position_swap(a, b, p)
        results.append(comparison)
    
    return {
        'a_wins': sum(1 for r in results if r['winner'] == 'A'),
        'b_wins': sum(1 for r in results if r['winner'] == 'B'),
        'ties': sum(1 for r in results if r['winner'] == 'TIE'),
        'position_consistency': position_consistency(results)
    }

使用场景 3:质量监控

目标:追踪评估质量随时间的变化

推荐指标

  1. 主要指标:与人工抽检的滚动一致性
  2. 次要指标:评分分布稳定性
  3. 诊断指标:偏差指标(位置、长度)
class QualityMonitor:
    def __init__(self, window_size=100):
        self.window = deque(maxlen=window_size)
    
    def add_evaluation(self, automated, human_spot_check=None):
        self.window.append({
            'automated': automated,
            'human': human_spot_check,
            'length': len(automated['response'])
        })
    
    def get_metrics(self):
        # Filter to evaluations with human spot-checks
        with_human = [e for e in self.window if e['human'] is not None]
        
        if len(with_human) < 10:
            return {'insufficient_data': True}
        
        auto_scores = [e['automated']['score'] for e in with_human]
        human_scores = [e['human']['score'] for e in with_human]
        
        return {
            'correlation': spearmans_rho(auto_scores, human_scores),
            'mean_difference': np.mean([a - h for a, h in zip(auto_scores, human_scores)]),
            'length_correlation': spearmans_rho(
                [e['length'] for e in self.window],
                [e['automated']['score'] for e in self.window]
            )
        }

解读指标结果

良好评估系统的指标参考

指标 良好 可接受 值得关注
Spearman's ρ > 0.8 0.60.8 < 0.6
Cohen's κ > 0.7 0.50.7 < 0.5
位置一致性 > 0.9 0.80.9 < 0.8
长度相关性 < 0.2 0.20.4 > 0.4

预警信号

  1. 一致性高但相关性低:可能存在校准问题
  2. 位置一致性低:位置偏差正在影响结果
  3. 长度相关性高:长度偏差拉高了评分
  4. 逐标准方差大:某些标准的定义可能不够清晰

报告模板

## 评估系统指标报告

### 与人工的一致性
- Spearman's ρ:0.82p < 0.001
- Cohen's κ:0.74
- 样本量:500 次评估

### 偏差指标
- 位置一致性:91%
- 长度与评分相关性:0.12

### 各标准表现
| 标准 | Spearman's ρ | κ |
|-----------|--------------|---|
| 准确性 | 0.88 | 0.79 |
| 清晰度 | 0.76 | 0.68 |
| 完整性 | 0.81 | 0.72 |

### 建议
- 所有指标均在可接受范围内
- 关注「清晰度」标准——一致性较低可能表明需要优化评分细则