🚀 Polish - SPLADE
这是论文 From distillation to hard negative sampling: Making sparse neural ir models more effective 中描述的 SPLADE++ (EnsembleDistil) 模型的波兰语版本。稀疏词汇与扩展(SPLADE)是一系列采用Transformer语言模型的现代基于词项的检索方法。在这种方法中,掩码语言建模(MLM)头经过优化,以生成适用于文本检索的词汇大小的权重向量。SPLADE是一种高效的稀疏检索排序算法,其结果优于BM25等经典方法,与高质量的密集编码器相当。
该模型是在MS MARCO数据集的波兰语翻译版本上,从 polish - distilroberta 检查点进行微调得到的。我们使用了官方 SPLADE 仓库 中的默认训练超参数。
🚀 快速开始
模型简介
这是一个适用于波兰语的SPLADE++ (EnsembleDistil) 模型,基于Transformer架构,在文本检索任务中表现出色。它是在波兰语版的MS MARCO数据集上微调得到的,使用了默认的训练超参数。
依赖说明
使用该模型仅需要Huggingface Transformers库,无需其他额外依赖。
✨ 主要特性
- 语言适配:专门为波兰语设计,基于波兰语版的MS MARCO数据集进行微调。
- 高效检索:作为一种稀疏检索排序算法,效果优于经典的BM25算法,与高质量的密集编码器相当。
- 灵活使用:既可以独立使用,也可以集成到大型数据集的索引和搜索任务中。
📦 安装指南
该模型的使用依赖于Huggingface Transformers库,可使用以下命令进行安装:
pip install transformers
💻 使用示例
基础用法
以下是一个使用SPLADE的示例,仅依赖Huggingface Transformers库:
import torch, math
import numpy as np
from transformers import AutoTokenizer, AutoModelForMaskedLM
model_name = "sdadas/polish-splade"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForMaskedLM.from_pretrained(model_name)
vocab = {v: k for k, v in tokenizer.get_vocab().items()}
def encode_splade(text: str):
input = tokenizer([text], padding="longest", truncation=True, return_tensors="pt", max_length=512)
output = model(**input)
logits, attention_mask = output["logits"].detach(), input["attention_mask"].detach()
attention_mask = attention_mask.unsqueeze(-1)
vector = torch.max(torch.log(torch.add(torch.relu(logits), 1)) * attention_mask, dim=1)
vector = vector[0].detach().squeeze()
idx = np.nonzero(vector.cpu().numpy())[0]
vector = vector[idx]
return {vocab[k]: float(v) for k, v in zip(list(idx), list(vector))}
def cos_sim(vec1, vec2):
intersection = set(vec1.keys()) & set(vec2.keys())
numerator = sum([vec1[x] * vec2[x] for x in intersection])
sum1 = sum([vec1[x] ** 2 for x in list(vec1.keys())])
sum2 = sum([vec2[x] ** 2 for x in list(vec2.keys())])
denominator = math.sqrt(sum1) * math.sqrt(sum2)
return (numerator / denominator) if denominator else 0.0
question = encode_splade("Jak dożyć 100 lat?")
answer = encode_splade("Trzeba zdrowo się odżywiać i uprawiać sport.")
print(cos_sim(question, answer))
高级用法
使用 PIRB 库的示例:
from search import SpladeEncoder
from sentence_transformers.util import cos_sim
config = {"name": "sdadas/polish-splade", "fp16": True}
encoder = SpladeEncoder(config, True)
results = encoder.encode_batch(["Jak dożyć 100 lat?", "Trzeba zdrowo się odżywiać i uprawiać sport."])
print(cos_sim(results[0], results[1]))
大型数据集处理
使用SPLADE对大型数据集进行索引和搜索是一项更复杂的任务,需要与基于词项的索引(如Lucene)集成。为此,你可以使用官方 SPLADE实现 或我们 PIRB库 中的该模型重新实现。
📚 详细文档
模型信息
引用说明
如果你使用了该模型,请引用以下论文:
@article{dadas2024pirb,
title={{PIRB}: A Comprehensive Benchmark of Polish Dense and Hybrid Text Retrieval Methods},
author={Sławomir Dadas and Michał Perełkiewicz and Rafał Poświata},
year={2024},
eprint={2402.13350},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
📄 许可证
本项目采用Apache 2.0许可证。