这是一个 CoSENT(余弦句子)模型:shibing624/text2vec-base-chinese。
它能将句子映射到 768 维的稠密向量空间,可用于句子嵌入、文本匹配或语义搜索等任务。
关于该模型的自动化评估,请参见 评估基准:text2vec
| 架构 | 基础模型 | 模型 | ATEC | BQ | LCQMC | PAWSX | STS-B | SOHU-dd | SOHU-dc | 平均值 | QPS |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Word2Vec | word2vec | w2v-light-tencent-chinese | 20.00 | 31.49 | 59.46 | 2.57 | 55.78 | 55.04 | 20.70 | 35.03 | 23769 |
| SBERT | xlm-roberta-base | sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 | 18.42 | 38.52 | 63.96 | 10.14 | 78.90 | 63.01 | 52.28 | 46.46 | 3138 |
| Instructor | hfl/chinese-roberta-wwm-ext | moka-ai/m3e-base | 41.27 | 63.81 | 74.87 | 12.20 | 76.96 | 75.83 | 60.55 | 57.93 | 2980 |
| CoSENT | hfl/chinese-macbert-base | shibing624/text2vec-base-chinese | 31.93 | 42.67 | 70.16 | 17.21 | 79.30 | 70.27 | 50.42 | 51.61 | 3008 |
| CoSENT | hfl/chinese-lert-large | GanymedeNil/text2vec-large-chinese | 32.61 | 44.59 | 69.30 | 14.51 | 79.44 | 73.01 | 59.04 | 53.12 | 2092 |
| CoSENT | nghuyong/ernie-3.0-base-zh | shibing624/text2vec-base-chinese-sentence | 43.37 | 61.43 | 73.48 | 38.90 | 78.25 | 70.60 | 53.08 | 59.87 | 3089 |
| CoSENT | nghuyong/ernie-3.0-base-zh | shibing624/text2vec-base-chinese-paraphrase | 44.89 | 63.58 | 74.24 | 40.90 | 78.93 | 76.70 | 63.30 | 63.08 | 3066 |
| CoSENT | sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 | shibing624/text2vec-base-multilingual | 32.39 | 50.33 | 65.64 | 32.56 | 74.45 | 68.88 | 51.17 | 53.67 | 4004 |
说明:
shibing624/text2vec-base-chinese模型,是使用 CoSENT 方法训练,基于hfl/chinese-macbert-base在中文 STS-B 数据上训练得到,并在中文 STS-B 测试集上评估取得了较好效果。运行examples/training_sup_text_matching_model.py代码可训练模型,模型文件已上传至 HF model hub,推荐用于中文通用语义匹配任务。shibing624/text2vec-base-chinese-sentence模型,是使用 CoSENT 方法训练,基于nghuyong/ernie-3.0-base-zh,采用人工挑选后的中文 STS 数据集shibing624/nli-zh-all/text2vec-base-chinese-sentence-dataset训练得到,并在中文各 NLI 测试集上评估取得了较好效果。运行examples/training_sup_text_matching_model_jsonl_data.py代码可训练模型,模型文件已上传至 HF model hub,推荐用于中文 s2s(句子 vs 句子)语义匹配任务。shibing624/text2vec-base-chinese-paraphrase模型,是使用 CoSENT 方法训练,基于nghuyong/ernie-3.0-base-zh,采用人工挑选后的中文 STS 数据集shibing624/nli-zh-all/text2vec-base-chinese-paraphrase-dataset。该数据集在shibing624/nli-zh-all/text2vec-base-chinese-sentence-dataset的基础上加入了 s2p(句子到 paraphrase)数据,增强了其对长文本的表征能力,并在中文各 NLI 测试集上评估达到了 SOTA 水平。运行examples/training_sup_text_matching_model_jsonl_data.py代码可训练模型,模型文件已上传至 HF model hub,推荐用于中文 s2p(句子 vs 段落)语义匹配任务。sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2模型是使用 SBERT 训练的,是paraphrase-MiniLM-L12-v2模型的多语言版本,支持中文、英文等多种语言。w2v-light-tencent-chinese是腾讯词向量的 Word2Vec 模型,可在 CPU 上加载使用,适用于中文字面匹配任务以及数据缺失的冷启动场景。当您安装了text2vec后,使用此模型会变得非常简单:
pip install -U text2vec然后你可以像这样使用该模型:
from text2vec import SentenceModel
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
model = SentenceModel('shibing624/text2vec-base-chinese')
embeddings = model.encode(sentences)
print(embeddings)如果不使用 text2vec,您可以这样使用该模型:
首先,将输入传递给 transformer 模型,然后必须在上下文化的词嵌入之上应用正确的池化操作。
安装 transformers:
pip install transformers然后加载模型并预测:
from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
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)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="jeffding/text2vec-base-chinese-openmind",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
model = model_path
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModel.from_pretrained(model_path)
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling. In this case, mean pooling.
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
if __name__ == "__main__":
main()sentence-transformers 是一个用于计算句子密集向量表示的热门库。
安装 sentence-transformers:
pip install -U sentence-transformers然后加载模型并进行预测:
from sentence_transformers import SentenceTransformer
m = SentenceTransformer("shibing624/text2vec-base-chinese")
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
sentence_embeddings = m.encode(sentences)
print("Sentence embeddings:")
print(sentence_embeddings)CoSENT(
(0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_mean_tokens': True})
)我们的模型旨在用作句子和短段落编码器。给定输入文本,它会输出一个捕捉语义信息的向量。该句子向量可用于信息检索、聚类或句子相似度任务。
默认情况下,超过256个词片的输入文本会被截断。
我们使用预训练的hfl/chinese-macbert-base模型。有关预训练过程的更多详细信息,请参考该模型卡片。
我们使用对比目标对模型进行微调。具体来说,我们计算批次中每个可能句子对的余弦相似度。 然后,通过与真实对和虚假对进行比较来应用排序损失。
此模型由text2vec训练。
如果您觉得此模型有帮助,欢迎引用:
@software{text2vec,
author = {Xu Ming},
title = {text2vec: A Tool for Text to Vector},
year = {2022},
url = {https://github.com/shibing624/text2vec},
}