模型概述
模型特點
模型能力
使用案例
🚀 biencoder-camembert-base-mmarcoFR
這是一個用於法語的密集單向量雙編碼器模型,可用於語義搜索。該模型將查詢和段落映射到768維的密集向量,通過餘弦相似度計算相關性。
🚀 快速開始
本模型可以使用 Sentence-Transformers、FlagEmbedding 或 Huggingface Transformers 進行調用,以下是使用示例。
💻 使用示例
基礎用法
使用 Sentence-Transformers
首先安裝 庫:pip install -U sentence-transformers
。然後,你可以這樣使用該模型:
from sentence_transformers import SentenceTransformer
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
model = SentenceTransformer('antoinelouis/biencoder-camembert-base-mmarcoFR')
q_embeddings = model.encode(queries, normalize_embeddings=True)
p_embeddings = model.encode(passages, normalize_embeddings=True)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
使用 FlagEmbedding
首先安裝 庫:pip install -U FlagEmbedding
。然後,你可以這樣使用該模型:
from FlagEmbedding import FlagModel
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
model = FlagModel('antoinelouis/biencoder-camembert-base-mmarcoFR')
q_embeddings = model.encode(queries, normalize_embeddings=True)
p_embeddings = model.encode(passages, normalize_embeddings=True)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
使用 Transformers
首先安裝 庫:pip install -U transformers
。然後,你可以這樣使用該模型:
from transformers import AutoTokenizer, AutoModel
from torch.nn.functional import normalize
def mean_pooling(model_output, attention_mask):
""" Perform mean pooling on-top of the contextualized word embeddings, while ignoring mask tokens in the mean computation."""
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
model = AutoModel.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
q_input = tokenizer(queries, padding=True, truncation=True, return_tensors='pt')
p_input = tokenizer(passages, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
q_output = model(**encoded_queries)
p_output = model(**encoded_passages)
q_embeddings = mean_pooling(q_output, q_input['attention_mask'])
q_embedddings = normalize(q_embeddings, p=2, dim=1)
p_embeddings = mean_pooling(p_output, p_input['attention_mask'])
p_embedddings = normalize(p_embeddings, p=2, dim=1)
similarity = q_embeddings @ p_embeddings.T
print(similarity)
📚 詳細文檔
評估
該模型在 mMARCO-fr 的較小開發集上進行評估,該數據集包含 6980 個查詢和 880 萬個候選段落的語料庫。我們報告了平均倒數排名(MRR)、歸一化折損累計增益(NDCG)、平均精度均值(MAP)以及不同截斷點的召回率(R@k)。要查看它與其他法語神經檢索器的比較情況,請查看 DécouvrIR 排行榜。
訓練
數據
我們使用 mMARCO 數據集中的法語訓練樣本,這是 MS MARCO 的多語言機器翻譯版本,包含 880 萬個段落和 53.9 萬個訓練查詢。我們沒有使用官方數據集提供的 BM25 負樣本,而是使用 msmarco-hard-negatives 蒸餾數據集,從 12 個不同的密集檢索器中挖掘出更難的負樣本。
實現
該模型從 camembert-base 檢查點初始化,並通過交叉熵損失(如 DPR 中所述)進行優化,溫度為 0.05。使用 AdamW 優化器在一個 32GB NVIDIA V100 GPU 上進行 20 個 epoch(即 65700 步)的微調,批量大小為 152,峰值學習率為 2e-5,在前 500 步進行熱身並採用線性調度。我們將問題和段落的最大序列長度都設置為 128 個標記。使用餘弦相似度來計算相關性得分。
引用
@online{louis2024decouvrir,
author = 'Antoine Louis',
title = 'DécouvrIR: A Benchmark for Evaluating the Robustness of Information Retrieval Models in French',
publisher = 'Hugging Face',
month = 'mar',
year = '2024',
url = 'https://huggingface.co/spaces/antoinelouis/decouvrir',
}
📄 許可證
本項目採用 MIT 許可證。
📦 模型信息
屬性 | 詳情 |
---|---|
模型類型 | 密集單向量雙編碼器模型 |
訓練數據 | mMARCO 數據集中的法語訓練樣本 |
評估指標 | 平均倒數排名(MRR)、歸一化折損累計增益(NDCG)、平均精度均值(MAP)、不同截斷點的召回率(R@k) |
基礎模型 | camembert-base |
庫名稱 | sentence-transformers |
任務類型 | 句子相似度、段落檢索 |
數據集 | unicamp-dl/mmarco |
評估數據集 | mMARCO-fr |
評估結果 | Recall@500: 89.1;Recall@100: 77.8;Recall@10: 51.5;MAP@10: 27.9;nDCG@10: 33.7;MRR@10: 28.5 |







