C.W.K.
Stream
Lesson 02 of 05 · published

Reciprocal Rank Fusion

~18 min · hybrid, fusion

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

score fusion 이 어려운 이유

다른 retriever 는 다른 scale 로 score. raw score 추가는 큰 numerical range 가진 ranker 가 우연히 이김. 프로덕션 시스템이 수렴하는 고침은 단순: score 아니라 rank 로 fuse.

Reciprocal Rank Fusion (RRF)

각 candidate 마다 RRF = sum(1 / (k + rank_i)) 를 모든 ranker 에 걸쳐 계산, 여기서 rank_i 는 i번째 ranker 의 rank (1 = best), k 는 smoothing 상수 (60 이 2009년 Cormack 논문의 canonical default).

결과: outlier 에 robust + retriever 별 파라미터 튜닝 0 + 임의 ranker 수에 작동 (3, 5, 10 retriever 다 같은 방식으로 결합) 되는 fusion score.

Code

10줄 RRF·python
def reciprocal_rank_fusion(rankings: list[list[str]], k: int = 60) -> list[tuple[str, float]]:
    scores: dict[str, float] = {}
    for ranking in rankings:                       # 각 ranking 은 best first id 리스트
        for rank, doc_id in enumerate(ranking):
            scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank + 1)
    return sorted(scores.items(), key=lambda kv: -kv[1])

vector_ids = [r[0] for r in vector_search(query, k=20)]
bm25_ids   = [r[0] for r in bm25_search(query,   k=20)]

for doc_id, score in reciprocal_rank_fusion([vector_ids, bm25_ids])[:10]:
    print(f'{score:.4f}  {doc_id}')

External links

Exercise

이전 exercise 의 vector + BM25 ranking. RRF 로 fuse. (a) vector only, (b) BM25 only, (c) 단순 union 대비 top-5 hit rate 비교. RRF 가 어디서 이기고 어디서 지는지 노트.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.