英文文本向量表示模型MiniLM-IBKD-STS
文本表示是自然语言处理(NLP)领域的核心问题, 其在很多NLP、信息检索的下游任务中发挥着非常重要的作用。近几年, 随着深度学习的发展,尤其是预训练语言模型的出现极大的推动了文本表示技术的效果, 基于预训练语言模型的文本表示模型在学术研究数据、工业实际应用中都明显优于传统的基于统计模型或者浅层神经网络的文本表示模型。这里, 我们主要关注基于预训练语言模型的文本表示。
文本表示示例, 输入一个句子, 输入一个固定维度的连续向量:
- 输入: how long it take to get a master's degree
- 输出: [0.27162,-0.66159,0.33031,0.24121,0.46122,…]
文本的向量表示通常可以用于文本聚类、文本相似度计算、文本向量召回等下游任务中。
模型介绍
本模型为将MiniLM模型利用IBKD方法蒸馏得到的文本表示模型,其教师模型为SimCSE-SUP-Roberta-large。适用于STS(Semantic Textual Similarity)任务。具体训练细节请参考论文。
使用方式和范围
使用方式:
- 直接推理, 对给定文本计算其对应的文本向量表示,向量维度384
使用范围:
- 本模型可以使用在通用领域的文本向量表示及其下游应用场景, 主要应用场景为文本相似度计算。
如何使用
在ModelScope框架上,提供输入文本(默认最长文本长度为128),即可以通过简单的Pipeline调用来使用coROM文本向量表示模型。ModelScope封装了统一的接口对外提供单句向量表示、双句文本相似度、多候选相似度计算功能
代码示例
from modelscope.models import Model
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
model_id = "damo/nlp_minilm_ibkd_sentence-embedding_english-sts"
pipeline_se = pipeline(Tasks.sentence_embedding,
model=model_id)
# 当输入包含“soure_sentence”与“sentences_to_compare”时,会输出source_sentence中首个句子与sentences_to_compare中每个句子的向量表示,以及source_sentence中首个句子与sentences_to_compare中每个句子的相似度。
from modelscope.models import Model
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
model_id = "damo/nlp_minilm_ibkd_sentence-embedding_english-sts"
pipeline_se = pipeline(Tasks.sentence_embedding,
model=model_id)
inputs = {
"source_sentence": ["how long it take to get a master degree"],
"sentences_to_compare": [
"On average, students take about 18 to 24 months to complete a master degree.",
"On the other hand, some students prefer to go at a slower pace and choose to take",
"several years to complete their studies.",
"It can take anywhere from two semesters"
]
}
result = pipeline_se(input=inputs)
print (result)
默认向量维度384, scores中的score计算两个向量之间的内积距离得到
模型局限性以及可能的偏差
本模型基于英文领域通用数据集训练,在垂类领域英文文本上的文本效果会有降低,请用户自行评测后决定如何使用
训练示例代码
# 需在GPU环境运行
# 加载数据集过程可能由于网络原因失败,请尝试重新运行代码
from modelscope.metainfo import Trainers
from modelscope.msdatasets import MsDataset
from modelscope.trainers import build_trainer
import tempfile
import os
tmp_dir = tempfile.TemporaryDirectory().name
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
# load dataset
ds = MsDataset.load('msmarco-passage-ranking', 'zyznull')
train_ds = ds['train'].to_hf_dataset()
dev_ds = ds['dev'].to_hf_dataset()
model_id = 'damo/nlp_minilm_ibkd_sentence-embedding_english-sts'
def cfg_modify_fn(cfg):
cfg.task = 'sentence-embedding'
cfg['preprocessor'] = {'type': 'sentence-embedding','max_length': 256}
cfg['dataset'] = {
'train': {
'type': 'bert',
'query_sequence': 'query',
'pos_sequence': 'positive_passages',
'neg_sequence': 'negative_passages',
'text_fileds': ['text'],
'qid_field': 'query_id'
},
'val': {
'type': 'bert',
'query_sequence': 'query',
'pos_sequence': 'positive_passages',
'neg_sequence': 'negative_passages',
'text_fileds': ['text'],
'qid_field': 'query_id'
},
}
cfg['train']['neg_samples'] = 4
cfg['evaluation']['dataloader']['batch_size_per_gpu'] = 30
cfg.train.max_epochs = 1
cfg.train.train_batch_size = 4
return cfg
kwargs = dict(
model=model_id,
train_dataset=train_ds,
work_dir=tmp_dir,
eval_dataset=dev_ds,
cfg_modify_fn=cfg_modify_fn)
trainer = build_trainer(name=Trainers.nlp_sentence_embedding_trainer, default_args=kwargs)
trainer.train()
模型效果评估
STS任务评估结果如下:
Model | STS12 | STS13 | STS14 | STS15 | STS16 | STS-B | SICK-R | Avg | Params | Dimension |
---|---|---|---|---|---|---|---|---|---|---|
SimCSE-RoBERTa-base | 76.53 | 85.21 | 80.95 | 86.03 | 82.57 | 85.83 | 80.50 | 82.52 | 110M | 768 |
SimCSE-RoBERTalarge | 77.46 | 87.27 | 82.36 | 86.66 | 83.93 | 86.70 | 81.95 | 83.76 | 330M | 1024 |
SimCSE-MiniLM | 70.34 | 78.59 | 75.08 | 81.10 | 77.74 | 79.39 | 77.85 | 77.16 | 23M | 384 |
MiniLM-MSE | 73.75 | 81.42 | 77.72 | 83.58 | 78.99 | 81.19 | 78.48 | 79.30 | 23M | 384 |
MiniLM-HPD | 76.03 | 84.71 | 80.45 | 85.53 | 82.07 | 85.33 | 80.01 | 82.05 | 23M | 128 |
MiniLM-CRD | 74.79 | 84.19 | 78.98 | 84.70 | 80.65 | 82.71 | 79.91 | 81.30 | 23M | 384 |
MiniLM-IBKD | 76.77 | 86.13 | 81.03 | 85.66 | 82.81 | 86.14 | 81.25 | 82.69 | 23M | 384 |
引用
@misc{zhang2023text,
title={Text Representation Distillation via Information Bottleneck Principle},
author={Yanzhao Zhang and Dingkun Long and Zehan Li and Pengjun Xie},
year={2023},
eprint={2311.05472},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
评论