Ldir Qwen2 Reranker 1.5B
基于Qwen2-1.5B的下游任务模型,专注于重排序任务,在中文医疗问答和通用文本重排序任务中表现优异。
下载量 51
发布时间 : 8/13/2024
模型简介
该模型是基于Qwen2-1.5B开发的重排序模型,主要用于提升检索系统的相关性排序效果,特别优化了中文医疗问答场景下的性能。
模型特点
中文医疗问答优化
在CMedQA医疗问答数据集上表现出色,MAP指标达到86.5以上
多任务适配
支持多种重排序任务,包括通用文本和医疗领域
高效推理
支持FP16加速和多GPU并行计算
模型能力
文本相关性重排序
医疗问答优化
跨语言重排序
使用案例
信息检索
医疗问答系统
提升医疗问答系统中答案的排序质量
在CMedQAv1数据集上MRR达到88.91
搜索引擎优化
改进搜索引擎结果的相关性排序
在MMarco数据集上MAP达到39.35
🚀 LdIR-Qwen2-reranker-1.5B
本模型是基于Qwen/Qwen2 - 1.5B的下游任务模型。我们借鉴了FlagEmbedding reranker的工作,并使用Qwen2 - 1.5B作为预训练模型进行实现。
🚀 快速开始
依赖安装
transformers==4.41.2
flash-attn==2.5.7
代码使用
from typing import cast, List, Union, Tuple, Dict, Optional
import numpy as np
import torch
from tqdm import tqdm
import transformers
from transformers import AutoTokenizer, PreTrainedModel, PreTrainedTokenizer, DataCollatorWithPadding
from transformers.models.qwen2 import Qwen2Config, Qwen2ForSequenceClassification
from transformers.trainer_pt_utils import LabelSmoother
IGNORE_TOKEN_ID = LabelSmoother.ignore_index
def preprocess(
sources,
tokenizer: transformers.PreTrainedTokenizer,
max_len: int = 1024,
) -> Dict:
# Apply prompt templates
input_ids, attention_masks = [], []
for i, source in enumerate(sources):
messages = [
{"role": "user",
"content": "\n\n".join(source)}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text])
input_id = model_inputs['input_ids'][0]
attention_mask = model_inputs['attention_mask'][0]
if len(input_id) > max_len:
## last five tokens: <|im_end|>(151645), \n(198), <|im_start|>(151644), assistant(77091), \n(198)
diff = len(input_id) - max_len
input_id = input_id[:-5-diff] + input_id[-5:]
attention_mask = attention_mask[:-5-diff] + attention_mask[-5:]
assert len(input_id) == max_len
input_ids.append(input_id)
attention_masks.append(attention_mask)
return dict(
input_ids=input_ids,
attention_mask=attention_masks
)
class FlagRerankerCustom:
def __init__(
self,
model: PreTrainedModel,
tokenizer: PreTrainedTokenizer,
use_fp16: bool = False
) -> None:
self.tokenizer = tokenizer
self.model = model
self.data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
if torch.cuda.is_available():
self.device = torch.device('cuda')
elif torch.backends.mps.is_available():
self.device = torch.device('mps')
else:
self.device = torch.device('cpu')
use_fp16 = False
if use_fp16:
self.model.half()
self.model = self.model.to(self.device)
self.model.eval()
self.num_gpus = torch.cuda.device_count()
if self.num_gpus > 1:
print(f"----------using {self.num_gpus}*GPUs----------")
self.model = torch.nn.DataParallel(self.model)
@torch.no_grad()
def compute_score(self, sentence_pairs: Union[List[Tuple[str, str]], Tuple[str, str]], batch_size: int = 64,
max_length: int = 1024) -> List[float]:
if self.num_gpus > 0:
batch_size = batch_size * self.num_gpus
assert isinstance(sentence_pairs, list)
if isinstance(sentence_pairs[0], str):
sentence_pairs = [sentence_pairs]
all_scores = []
for start_index in tqdm(range(0, len(sentence_pairs), batch_size), desc="Compute Scores",
disable=True):
sentences_batch = sentence_pairs[start_index:start_index + batch_size]
inputs = preprocess(sources=sentences_batch, tokenizer=self.tokenizer, max_len=max_length)
inputs = [dict(zip(inputs, t)) for t in zip(*inputs.values())]
inputs = self.data_collator(inputs).to(self.device)
scores = self.model(**inputs, return_dict=True).logits
scores = scores.squeeze()
all_scores.extend(scores.detach().to(torch.float).cpu().numpy().tolist())
if len(all_scores) == 1:
return all_scores[0]
return all_scores
tokenizer = transformers.AutoTokenizer.from_pretrained(
"neofung/LdIR-Qwen2-reranker-1.5B",
padding_side="right",
)
config = Qwen2Config.from_pretrained(
"neofung/LdIR-Qwen2-reranker-1.5B",
trust_remote_code=True,
bf16=True,
)
model = Qwen2ForSequenceClassification.from_pretrained(
"neofung/LdIR-Qwen2-reranker-1.5B",
config = config,
trust_remote_code = True,
)
model = FlagRerankerCustom(model=model, tokenizer=tokenizer, use_fp16=False)
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
model.compute_score(pairs)
# [-2.655318021774292, 11.7670316696167]
在C - MTEB上的评估
from C_MTEB.tasks import *
from mteb import MTEB
save_name = "LdIR-Qwen2-reranker-1.5B"
evaluation = MTEB(
task_types=["Reranking"], task_langs=['zh', 'zh2en', 'en2zh']
)
evaluation.run(model, output_folder=f"reranker_results/{save_name}")
📊 评估结果
任务类型 | 数据集 | 评估指标 | 数值 |
---|---|---|---|
重排序 | MTEB CMedQAv1 | MAP | 86.50438688414654 |
重排序 | MTEB CMedQAv1 | MRR | 88.91170634920635 |
重排序 | MTEB CMedQAv2 | MAP | 87.10592353383732 |
重排序 | MTEB CMedQAv2 | MRR | 89.10178571428571 |
重排序 | MTEB MMarcoReranking | MAP | 39.354813242907133 |
重排序 | MTEB MMarcoReranking | MRR | 39.075793650793655 |
重排序 | MTEB T2Reranking | MAP | 68.83696915006163 |
重排序 | MTEB T2Reranking | MRR | 79.77644651857584 |
📄 许可证
本项目采用Apache - 2.0许可证。
Jina Embeddings V3
Jina Embeddings V3 是一个多语言句子嵌入模型,支持超过100种语言,专注于句子相似度和特征提取任务。
文本嵌入
Transformers 支持多种语言

J
jinaai
3.7M
911
Ms Marco MiniLM L6 V2
Apache-2.0
基于MS Marco段落排序任务训练的交叉编码器模型,用于信息检索中的查询-段落相关性评分
文本嵌入 英语
M
cross-encoder
2.5M
86
Opensearch Neural Sparse Encoding Doc V2 Distill
Apache-2.0
基于蒸馏技术的稀疏检索模型,专为OpenSearch优化,支持免推理文档编码,在搜索相关性和效率上优于V1版本
文本嵌入
Transformers 英语

O
opensearch-project
1.8M
7
Sapbert From PubMedBERT Fulltext
Apache-2.0
基于PubMedBERT的生物医学实体表征模型,通过自对齐预训练优化语义关系捕捉
文本嵌入 英语
S
cambridgeltl
1.7M
49
Gte Large
MIT
GTE-Large 是一个强大的句子转换器模型,专注于句子相似度和文本嵌入任务,在多个基准测试中表现出色。
文本嵌入 英语
G
thenlper
1.5M
278
Gte Base En V1.5
Apache-2.0
GTE-base-en-v1.5 是一个英文句子转换器模型,专注于句子相似度任务,在多个文本嵌入基准测试中表现优异。
文本嵌入
Transformers 支持多种语言

G
Alibaba-NLP
1.5M
63
Gte Multilingual Base
Apache-2.0
GTE Multilingual Base 是一个多语言的句子嵌入模型,支持超过50种语言,适用于句子相似度计算等任务。
文本嵌入
Transformers 支持多种语言

G
Alibaba-NLP
1.2M
246
Polybert
polyBERT是一个化学语言模型,旨在实现完全由机器驱动的超快聚合物信息学。它将PSMILES字符串映射为600维密集指纹,以数值形式表示聚合物化学结构。
文本嵌入
Transformers

P
kuelumbus
1.0M
5
Bert Base Turkish Cased Mean Nli Stsb Tr
Apache-2.0
基于土耳其语BERT的句子嵌入模型,专为语义相似度任务优化
文本嵌入
Transformers 其他

B
emrecan
1.0M
40
GIST Small Embedding V0
MIT
基于BAAI/bge-small-en-v1.5模型微调的文本嵌入模型,通过MEDI数据集与MTEB分类任务数据集训练,优化了检索任务的查询编码能力。
文本嵌入
Safetensors 英语
G
avsolatorio
945.68k
29
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98
智启未来,您的人工智能解决方案智库
简体中文