上篇(三)我把 clarify 改成了一道选择题:活动表进会话、viewport 自动注入,六种问法收成一条 ask()。改完那版我自己用着挺顺,但心里没底——那几天我的“评测”是:改一处代码,手动点三遍,凭记忆判断“好像比之前好”。
这不是评测,是手感。手感有两个毛病:换个问法就翻车,而且说不出翻在哪。
这篇只做一件事:把评测做成一条命令。读完你能拿到能手算自检的指标实现、能直接跑的评测脚本,和一张按题型拆开的报表。
先给结果:脚本重算第八、九篇那组配置,unicode61 的 R@1 0.6930、R@5 0.7895、R@10 0.8070、MRR 0.7349,跟当时存下的 fusion.json 一位不差。分题型一摊开,struct_cell 那 20 题 R@1 还是 0。数字对得上说明脚本没算错;但“总分 0.807”这个数,也确实靠另外 94 道题撑着。
一、评测集一行一题,只放三样东西
评测集是 JSONL,一行一题,字段压到不能再少:
{"qid":"q001","type":"flat","query":"表格里,「版式」为「文档」的那一行,「页数」列的值是多少?","gold_pid":"p001","gold_cell":[0,1],"answer":"5","locatable":true}
- qid:稳定编号。题面可以改,编号不能动,否则历史结果对不上。
- type:分题型报表的唯一依据,flat / struct_col_lookup / struct_cell / struct_header / struct_colmax / struct_rowmax。
- gold_pid:检索指标只认这一个字段,多 gold 就存数组。
- answer / gold_cell:留给答案层和结构通道,这篇不算它们。
- locatable:出题阶段那条
usable_table规则筛完,gold 表还能不能在库里定位到。它是分母开关,不是难度标签。
121 题的构成是这样,locatable 那列就是分母:
| 题型 | 题数 | 可定位 | 问的是什么 |
|---|---|---|---|
| flat | 43 | 41 | 行内取值,标签和值在同一行 |
| struct_col_lookup | 39 | 37 | 按列名找某行的值 |
| struct_cell | 23 | 20 | 第 X 行第 Y 列,坐标定位 |
| struct_header | 13 | 13 | 某个值属于哪一列 |
| struct_colmax | 2 | 2 | 一列里数值最大的那行 |
| struct_rowmax | 1 | 1 | 一行里数值最大的那列 |
| 合计 | 121 | 114 | — |
locatable=false 的那 7 道,3 道 struct_cell、2 道 flat、2 道 col_lookup,全部来自被 CSS 藏了列或者表头重复的表。不是检索器不行,是那题本来就没有可对的答案。第九篇把它们判了死刑,这篇让程序也认这个账。
二、三个指标,几十行代码
三个指标我都手写,不用 sklearn——不是逞能,是口径细节太多,自己写一遍才知道哪里能糊弄。
# -*- coding: utf-8 -*-
"""检索指标:Recall@k(命中口径)、MRR、nDCG@k。只用标准库。"""
import math
def recall_at_k(ranked, gold, k):
"""top-k 命中任意一条 gold 记 1。多 gold 时仍是 0/1,不按比例给分。"""
return 1.0 if set(ranked[:k]).intersection(gold) else 0.0
def mrr(ranked, gold):
"""第一条 gold 出现在第几位,就取 1/位次;没出现记 0。"""
g = set(gold)
for i, d in enumerate(ranked, 1):
if d in g:
return 1.0 / i
return 0.0
def ndcg_at_k(ranked, gold, k):
"""二值相关性:命中记 1,未命中记 0;IDCG 按 min(|gold|, k) 截断。"""
g = set(gold)
dcg = sum(1.0 / math.log2(i + 1)
for i, d in enumerate(ranked[:k], 1) if d in g)
idcg = sum(1.0 / math.log2(i + 1) for i in range(1, min(len(g), k) + 1))
return dcg / idcg if idcg else 0.0
def macro(rows, k):
"""对一批明细行做宏平均;rows 为空返回 None,不返回 0。"""
n = len(rows)
if not n:
return None
return {
"n": n,
"R@1": sum(recall_at_k(r["ranked"], r["gold"], 1) for r in rows) / n,
"R@5": sum(recall_at_k(r["ranked"], r["gold"], 5) for r in rows) / n,
"R@%d" % k: sum(recall_at_k(r["ranked"], r["gold"], k) for r in rows) / n,
"MRR": sum(mrr(r["ranked"], r["gold"]) for r in rows) / n,
"nDCG@%d" % k: sum(ndcg_at_k(r["ranked"], r["gold"], k) for r in rows) / n,
}
def selftest():
"""用能口算的例子把三个指标钉一遍。"""
rk, gold = ["x", "a", "b"], {"a"}
assert recall_at_k(rk, gold, 1) == 0.0
assert recall_at_k(rk, gold, 2) == 1.0
assert abs(mrr(rk, gold) - 0.5) < 1e-9
assert abs(ndcg_at_k(rk, gold, 3) - 1 / math.log2(3)) < 1e-9
# 两条 gold、只命中一条:Recall 仍记 1,nDCG 的 IDCG 按 2 条算
assert recall_at_k(["a"], {"a", "b"}, 5) == 1.0
assert abs(ndcg_at_k(["a"], {"a", "b"}, 5) - 1 / (1 + 1 / math.log2(3))) < 1e-9
print("metrics selftest: PASS")
if __name__ == "__main__":
selftest()
三个地方值得单独说:
- Recall 我按 0/1 写,不按“命中数 / gold 数”给比例。我的 gold 基本只有一页,按比例算跟历史数字对不上;多 gold 时还会把“召回一页”记成 0.5,等于偷偷改口径。这是选择,写清楚比“更正确”重要。
- IDCG 必须按 min(|gold|, k) 截断。我第一版写成
range(1, k+1),两条 gold 的题 IDCG 虚高,nDCG 永远到不了 1,而且看不出来——排序本身没变。 - 宏平均遇到空集返回 None,不返回 0。某个题型一题都没有时,0 会被算进总平均,把分数拉下去。
先跑 selftest,用能手算的例子卡死:["x","a","b"] 对 gold {"a"},R@1=0、R@2=1、MRR=0.5、nDCG@3=1/log2(3)=0.6309。这一步五分钟,省掉一整天怀疑数据。
三、一条命令跑完 114 题
run_eval.py 的骨架就三段:读题、逐题取 ranked、按题型分组打分:
qs = load_set(a.set)
idx = Index(load_fields(FIELDS))
fn = getattr(idx, a.retriever)
rows, skipped = [], []
for q in qs:
if q["gold_pid"] not in idx.fields or (not a.all and not q["locatable"]):
skipped.append(q["qid"])
continue
ranked = fn(q["query"])[:200]
rows.append({"qid": q["qid"], "type": q["type"], "ranked": ranked,
"gold": [q["gold_pid"]],
"hit_at_k": recall_at_k(ranked, [q["gold_pid"]], K)})
print("== %s %d 题(不可定位 %d,已跳过) 用时 %.1fs"
% (NAMES[a.retriever], len(rows), len(skipped), time.time() - t0))
print("总计 " + fmt(macro(rows, K)))
print("-- 分题型 " + "-" * 46)
by = collections.defaultdict(list)
for r in rows:
by[r["type"]].append(r)
for t, rs in sorted(by.items(), key=lambda x: -len(x[1])):
d = macro(rs, K)
print("%-18s %s" % (t, fmt(d)))
真实输出(247 页语料):
== unicode61 114 题(不可定位 7,已跳过) 用时 0.1s
总计 n=114 R@1 0.6930 R@5 0.7895 R@10 0.8070 MRR 0.7349 nDCG@10 0.7493
-- 分题型 ----------------------------------------------
flat n= 41 R@1 0.8537 R@5 0.9512 R@10 0.9756 MRR 0.8901 nDCG@10 0.9096
struct_col_lookup n= 37 R@1 0.8378 R@5 0.9459 R@10 0.9730 MRR 0.8765 nDCG@10 0.8983
struct_cell n= 20 R@1 0.0000 R@5 0.0000 R@10 0.0000 MRR 0.0177 nDCG@10 0.0000
struct_header n= 13 R@1 0.8462 R@5 1.0000 R@10 1.0000 MRR 0.9231 nDCG@10 0.9432
struct_colmax n= 2 R@1 1.0000 R@5 1.0000 R@10 1.0000 MRR 1.0000 nDCG@10 1.0000
struct_rowmax n= 1 R@1 0.0000 R@5 1.0000 R@10 1.0000 MRR 0.5000 nDCG@10 0.6309
== RRF K=1 114 题(不可定位 7,已跳过) 用时 0.4s
总计 n=114 R@1 0.7018 R@5 0.7895 R@10 0.8246 MRR 0.7439 nDCG@10 0.7607
-- 分题型 ----------------------------------------------
flat n= 41 R@1 0.8537 R@5 0.9268 R@10 0.9756 MRR 0.8876 nDCG@10 0.9075
struct_col_lookup n= 37 R@1 0.8378 R@5 0.9189 R@10 0.9730 MRR 0.8748 nDCG@10 0.8969
struct_cell n= 20 R@1 0.0000 R@5 0.1000 R@10 0.1000 MRR 0.0524 nDCG@10 0.0531
struct_header n= 13 R@1 0.8462 R@5 1.0000 R@10 1.0000 MRR 0.9231 nDCG@10 0.9432
struct_colmax n= 2 R@1 1.0000 R@5 1.0000 R@10 1.0000 MRR 1.0000 nDCG@10 1.0000
struct_rowmax n= 1 R@1 1.0000 R@5 1.0000 R@10 1.0000 MRR 1.0000 nDCG@10 1.0000
uni 0.1 秒,RRF 0.4 秒,多出来的是第二路检索加权排序。检索指标不花 token,所以我把它和模型调用分开跑。
四、报表摊开,四件事
1. 坐标题在检索层就是 0,调参救不回来
struct_cell 20 题,unicode61 的 R@1 / R@5 / R@10 全是 0;换 RRF 也只捞回 2 题,R@10 0.10。这跟第九篇“R@10 涨的那两道全是坐标题、是运气”对上了。所以第十篇让坐标题走结构通道、按下标直取,不是拍脑袋。
2. 总分不变,题型内部一涨一跌
unicode61 换 RRF,总分 R@5 都是 0.7895,一位没动。拆开看有意思:
| 题型 | n | unicode61 R@5 | RRF K=1 R@5 | ΔR@5 |
|---|---|---|---|---|
| flat | 41 | 0.9512(39/41) | 0.9268(38/41) | -2.4 |
| struct_col_lookup | 37 | 0.9459(35/37) | 0.9189(34/37) | -2.7 |
| struct_header | 13 | 1.0000(13/13) | 1.0000(13/13) | 0 |
| struct_cell | 20 | 0.0000(0/20) | 0.1000(2/20) | +10.0 |
| struct_colmax | 2 | 1.0000(2/2) | 1.0000(2/2) | 0 |
| struct_rowmax | 1 | 1.0000(1/1) | 1.0000(1/1) | 0 |
| 合计 | 114 | 0.7895(90/114) | 0.7895(90/114) | 0 |
只看总分,我会得出“RRF 对 R@5 没影响”的结论;实际是它拿两题换了另外两题。下游只吃 top-5 的话,这个交换要不要认,得我自己判断,不能让总分替我判断。
3. 小样本的 1.0 不是满分
struct_colmax 只有 2 题、struct_rowmax 1 题,全中就是 1.0,看着比 flat 还漂亮。所以报表强制带 n,n 小于 10 的行只当趋势看。
4. nDCG 和 Recall 会分叉
R@1 从 0.6930 抬到 0.7018 的同时,nDCG@10 从 0.7493 抬到 0.7607。只有 Recall 只能说“多召回了”;加上 nDCG 才能说“排得更靠前”。这两件事对要不要加 rerank 的意义完全不同。
五、我踩过的分母坑
同一份预测,分母算 121 和 114:
| 指标 | 121 题(含不可定位) | 114 题(只算可定位) |
|---|---|---|
| R@1 | 0.6694 | 0.6930 |
| R@5 | 0.7769 | 0.7895 |
| R@10 | 0.7934 | 0.8070 |
| MRR | 0.7124 | 0.7349 |
R@10 差 1.4 个百分点,R@1 差 2.4 个点,全来自那 7 道本来就没有正确答案的题。第八篇起我一直用 114,所以脚本默认剔除,并在输出第一行打印“不可定位 7,已跳过”。报表不写分母,等于没写。
另外两条也是真踩出来的:
- 预测和索引必须同版本。fields.json 一换(字段改了、jieba 版本变了),旧预测拿去对新索引打分,数字会差得莫名其妙。我第一次对不上就是拿旧分词配了新索引,排查半天以为 RRF 写错了。
- JSON 产物要连 argv 一起存。retriever、k、权重、语料文件全写进去。不然两周后只剩一堆数字,没人知道是哪个配置跑出来的。
六、这版还没做的
- 答案层还是人工加规则:exact / contains 两档,
1.00和1要归一再判。第十篇那个 85.1% 就是这么判的,没上 LLM-as-judge。 - 没有置信区间。114 题差 2 个点,差不多就是两道题的事,换个题源就没了。所以这篇里“高 2 个点”我都只当方向看。
- 没有延迟和成本维度。这正好是下一篇要补的。
总结
一句话:评测不是算个总分,是把分母、题型和版本钉死,让“改好了”这三个字有据可查。
- 指标先写 selftest,能手算的例子过一遍,再拿去跑数据。
- 报表必须带 n、必须分题型;总分只用来对外汇报。
- 分母口径写进输出第一行,别让它躲在代码里。
下期预告
这个系列的最后一篇:成本与部署。把本地零 token 的部分(路由、坐标取值、FTS 检索)和走 API 的部分(截图转写、答案生成)分开算账,再把前面四篇跑成一个能演示的原型:一台 4 核机器、一条 docker compose、一张按 1000 次请求折算的账单。
参考文献
- 上篇(三)clarify 变选择题、ask() 收口:https://www.fedte.cc/p/853.html
- (二)结构通道模块化与废表过滤:https://www.fedte.cc/p/852.html
- (一)项目骨架与最小闭环:https://www.fedte.cc/p/851.html
- 第八篇 jieba 预分词与单字过滤(R@10 0.8246 的出处):https://www.fedte.cc/p/847.html
- 第九篇 RRF 融合(R@1 0.7018 的出处):https://www.fedte.cc/p/848.html
- 第十篇 121 题端到端 85.1%、坐标题 100%:https://www.fedte.cc/p/849.html
- SQLite FTS5 官方文档(bm25()、列权重、MATCH):https://www.sqlite.org/fts5.html
- Cormack, Clarke, Buettcher. Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods, SIGIR 2009:https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf
- Järvelin, Kekäläinen. Cumulated gain-based evaluation of IR techniques. ACM TOIS 20(4), 2002(nDCG 原始论文):https://doi.org/10.1145/582415.582418
- trec_eval(官方检索评测工具,Recall / MRR / nDCG 的标准实现,可拿来对拍我的结果):https://github.com/usnistgov/trec_eval
- Python argparse 官方文档:https://docs.python.org/3/library/argparse.html
转载请注明:Falost的小窝 » 多模态 RAG 最小可用原型(四):把评测收成一条命令——Recall@k / MRR / nDCG@k 手写实现,121 题按题型出报表