重排器
重排器模型通常是具有 1 个输出类别的 CrossEncoder
模型,即给定一对文本(查询,答案),模型输出一个分数。这个分数,无论是一个合理范围在 -10.0 到 10.0 之间的浮点数,还是一个介于 0…1 之间的分数,都表示答案在多大程度上有助于回答查询。
许多重排器模型都在 MS MARCO 上训练
但最有可能的是,在您自己的数据集上训练时会获得最佳结果。因此,本页包含一些训练脚本示例,您可以根据自己的数据进行修改
-
此示例使用
BinaryCrossEntropyLoss
对从 GooAQ 数据集使用高效的SentenceTransformers
挖掘的带标签配对数据进行训练。该模型通过
CrossEncoderNanoBEIREvaluator
在 MS MARCO、NFCorpus、NQ 的子集上进行评估。此外,它还通过重排 GooAQ 开发集上高效SentenceTransformers
的前 100 个结果,来评估其性能增益。 -
此示例使用
CachedMultipleNegativesRankingLoss
对从 GooAQ 数据集加载的阳性配对数据进行训练。该模型通过
CrossEncoderNanoBEIREvaluator
在 MS MARCO、NFCorpus、NQ 的子集上进行评估。 -
此示例使用
LambdaLoss
对从 GooAQ 数据集使用高效的SentenceTransformers
挖掘的带标签列表数据进行训练。该模型通过
CrossEncoderNanoBEIREvaluator
在 MS MARCO、NFCorpus、NQ 的子集上进行评估。此外,它还通过重排 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
是有必要的。torch.nn.Sigmoid
结合 scale=10.0
效果不错,torch.nn.Identity`
结合 scale=1.0
也可以,而 mGTE 论文作者建议使用 torch.nn.Tanh
结合 scale=10.0
。
推理
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},
# ]