重排序器
重排序器模型通常是具有 1 个输出类别的 CrossEncoder
模型,即给定一个文本对(查询,答案),模型会输出一个分数。这个分数,可以是一个合理范围在 -10.0 到 10.0 之间的浮点数,或者是一个限定在 0 到 1 之间的分数,表示答案在多大程度上能够回答该查询。
许多重排序器模型都是在 MS MARCO 上训练的
但最有可能的是,当您在自己的数据集上训练时,会获得最佳结果。因此,本页包含了一些您可以根据自己的数据调整的示例训练脚本。
-
此示例在从 GooAQ 数据集中使用高效的
SentenceTransformers
挖掘出的带标签的文本对数据上使用BinaryCrossEntropyLoss
。该模型在 MS MARCO、NFCorpus、NQ 的子集上通过
CrossEncoderNanoBEIREvaluator
进行评估。此外,它还在 GooAQ 开发集上,通过对高效SentenceTransformers
的前 100 个结果进行重排序来评估性能增益。 -
此示例在从 GooAQ 数据集加载的正例对数据上使用
CachedMultipleNegativesRankingLoss
。该模型在 MS MARCO、NFCorpus、NQ 的子集上通过
CrossEncoderNanoBEIREvaluator
进行评估。 -
此示例在从 GooAQ 数据集中使用高效的
SentenceTransformers
挖掘出的带标签的列表数据上使用LambdaLoss
。该模型在 MS MARCO、NFCorpus、NQ 的子集上通过
CrossEncoderNanoBEIREvaluator
进行评估。此外,它还在 GooAQ 开发集上,通过对高效SentenceTransformers
的前 100 个结果进行重排序来评估性能增益。 -
此示例使用的训练脚本与
training_gooaq_bce.py
几乎完全相同,只是在较小的 NQ (自然问题) 数据集上进行。
BinaryCrossEntropyLoss
BinaryCrossEntropyLoss
是一个非常强大而又简单的损失函数。给定文本对(例如,(查询,答案)对),此损失函数使用 CrossEncoder
模型来计算预测分数。它将这些分数与黄金(或白银,即通过某个模型确定的)标签进行比较,模型表现越好,计算出的损失就越低。
CachedMultipleNegativesRankingLoss
CachedMultipleNegativesRankingLoss
(又名带 GradCache 的 InfoNCE)比常见的 BinaryCrossEntropyLoss
更复杂。它接受正例对(即(查询,答案)对)或三元组(即(查询,正确答案,错误答案)三元组),然后通过从批次中的其他问题中提取答案,为每个查询随机找到 num_negatives
个额外的错误答案。这通常被称为“批内负例”。
然后,损失函数将计算所有(查询,答案)对的分数,包括刚刚选择的错误答案对。接着,损失函数将使用交叉熵损失来确保(查询,正确答案)的分数高于所有(随机选择的)错误答案的(查询,错误答案)的分数。
CachedMultipleNegativesRankingLoss
使用一种名为 GradCache 的方法,允许在小批量中计算分数而不会过度增加内存使用。推荐使用此损失函数,而不是“标准”的 MultipleNegativesRankingLoss
(又名 InfoNCE)损失,后者没有这种巧妙的小批量处理支持,因此需要大量内存。
对于此损失函数,有必要对 activation_fn
和 scale
进行实验。使用 scale=10.0
的 torch.nn.Sigmoid
效果不错,使用 scale=1.0
的 torch.nn.Identity`
也可以,而 mGTE 论文的作者建议使用 scale=10.0
的 torch.nn.Tanh
。
推理
tomaarsen/reranker-ModernBERT-base-gooaq-bce 模型是使用第一个脚本训练的。如果您想在自己训练模型之前试用一下,请随时使用此脚本。
from sentence_transformers import CrossEncoder
# Download from the 🤗 Hub
model = CrossEncoder("tomaarsen/reranker-ModernBERT-base-gooaq-bce")
# Get scores for pairs of texts
pairs = [
["how to obtain a teacher's certificate in texas?", 'Some aspiring educators may be confused about the difference between teaching certification and teaching certificates. Teacher certification is another term for the licensure required to teach in public schools, while a teaching certificate is awarded upon completion of an academic program.'],
["how to obtain a teacher's certificate in texas?", '["Step 1: Obtain a Bachelor\'s Degree. One of the most important Texas teacher qualifications is a bachelor\'s degree. ... ", \'Step 2: Complete an Educator Preparation Program (EPP) ... \', \'Step 3: Pass Texas Teacher Certification Exams. ... \', \'Step 4: Complete a Final Application and Background Check.\']'],
["how to obtain a teacher's certificate in texas?", "Washington Teachers Licensing Application Process Official transcripts showing proof of bachelor's degree. Proof of teacher program completion at an approved teacher preparation school. Passing scores on the required examinations. Completed application for teacher certification in Washington."],
["how to obtain a teacher's certificate in texas?", 'Teacher education programs may take 4 years to complete after which certification plans are prepared for a three year period. During this plan period, the teacher must obtain a Standard Certification within 1-2 years. Learn how to get certified to teach in Texas.'],
["how to obtain a teacher's certificate in texas?", 'In Texas, the minimum age to work is 14. Unlike some states, Texas does not require juvenile workers to obtain a child employment certificate or an age certificate to work. A prospective employer that wants one can request a certificate of age for any minors it employs, obtainable from the Texas Workforce Commission.'],
]
scores = model.predict(pairs)
print(scores)
# [0.00121048 0.97105724 0.00536712 0.8632406 0.00168043]
# Or rank different texts based on similarity to a single text
ranks = model.rank(
"how to obtain a teacher's certificate in texas?",
[
"[\"Step 1: Obtain a Bachelor's Degree. One of the most important Texas teacher qualifications is a bachelor's degree. ... \", 'Step 2: Complete an Educator Preparation Program (EPP) ... ', 'Step 3: Pass Texas Teacher Certification Exams. ... ', 'Step 4: Complete a Final Application and Background Check.']",
"Teacher education programs may take 4 years to complete after which certification plans are prepared for a three year period. During this plan period, the teacher must obtain a Standard Certification within 1-2 years. Learn how to get certified to teach in Texas.",
"Washington Teachers Licensing Application Process Official transcripts showing proof of bachelor's degree. Proof of teacher program completion at an approved teacher preparation school. Passing scores on the required examinations. Completed application for teacher certification in Washington.",
"Some aspiring educators may be confused about the difference between teaching certification and teaching certificates. Teacher certification is another term for the licensure required to teach in public schools, while a teaching certificate is awarded upon completion of an academic program.",
"In Texas, the minimum age to work is 14. Unlike some states, Texas does not require juvenile workers to obtain a child employment certificate or an age certificate to work. A prospective employer that wants one can request a certificate of age for any minors it employs, obtainable from the Texas Workforce Commission.",
],
)
print(ranks)
# [
# {'corpus_id': 0, 'score': 0.97105724},
# {'corpus_id': 1, 'score': 0.8632406},
# {'corpus_id': 2, 'score': 0.0053671156},
# {'corpus_id': 4, 'score': 0.0016804343},
# {'corpus_id': 3, 'score': 0.0012104829},
# ]