pipeline_tag: 句子相似度
language:
- 南非荷兰语
- 阿姆哈拉语
- 阿拉伯语
- 阿萨姆语
- 阿塞拜疆语
- 白俄罗斯语
- 保加利亚语
- 孟加拉语
- 藏语
- 波斯尼亚语
- 加泰罗尼亚语
- 宿务语
- 科西嘉语
- 捷克语
- 威尔士语
- 丹麦语
- 德语
- 希腊语
- 英语
- 世界语
- 西班牙语
- 爱沙尼亚语
- 巴斯克语
- 波斯语
- 芬兰语
- 法语
- 弗里斯兰语
- 爱尔兰语
- 苏格兰盖尔语
- 加利西亚语
- 古吉拉特语
- 豪萨语
- 夏威夷语
- 希伯来语
- 印地语
- 苗语
- 克罗地亚语
- 海地克里奥尔语
- 匈牙利语
- 亚美尼亚语
- 印尼语
- 伊博语
- 冰岛语
- 意大利语
- 日语
- 爪哇语
- 格鲁吉亚语
- 哈萨克语
- 高棉语
- 卡纳达语
- 韩语
- 库尔德语
- 吉尔吉斯语
- 拉丁语
- 卢森堡语
- 老挝语
- 立陶宛语
- 拉脱维亚语
- 马尔加什语
- 毛利语
- 马其顿语
- 马拉雅拉姆语
- 蒙古语
- 马拉地语
- 马来语
- 马耳他语
- 缅甸语
- 尼泊尔语
- 荷兰语
- 挪威语
- 齐切瓦语
- 奥里亚语
- 旁遮普语
- 波兰语
- 葡萄牙语
- 罗马尼亚语
- 俄语
- 卢旺达语
- 僧伽罗语
- 斯洛伐克语
- 斯洛文尼亚语
- 萨摩亚语
- 修纳语
- 索马里语
- 阿尔巴尼亚语
- 塞尔维亚语
- 塞索托语
- 巽他语
- 瑞典语
- 斯瓦希里语
- 泰米尔语
- 泰卢固语
- 塔吉克语
- 泰语
- 土库曼语
- 他加禄语
- 土耳其语
- 鞑靼语
- 维吾尔语
- 乌克兰语
- 乌尔都语
- 乌兹别克语
- 越南语
- 沃洛夫语
- 科萨语
- 意第绪语
- 约鲁巴语
- 中文
- 祖鲁语
tags:
- bert
- 句子嵌入
- 多语言
- 谷歌
- 句子相似度
- lealla
- labse
license: apache-2.0
datasets:
- CommonCrawl
- 维基百科
LEALLA-small
模型描述
LEALLA 是一组轻量级语言无关的句子嵌入模型集合,支持109种语言,由 LaBSE 蒸馏而来。该模型适用于获取多语言句子嵌入和双语文本检索。
此模型是从 TF Hub 上的 v1 版本迁移而来。两个版本模型生成的嵌入是 等效的。不过,对于某些语言(如日语),LEALLA 模型在比较嵌入和相似度时可能需要更高的容差。
使用方法
使用模型:
import torch
from transformers import BertModel, BertTokenizerFast
tokenizer = BertTokenizerFast.from_pretrained("setu4993/LEALLA-small")
model = BertModel.from_pretrained("setu4993/LEALLA-small")
model = model.eval()
english_sentences = [
"dog",
"Puppies are nice.",
"I enjoy taking long walks along the beach with my dog.",
]
english_inputs = tokenizer(english_sentences, return_tensors="pt", padding=True)
with torch.no_grad():
english_outputs = model(**english_inputs)
获取句子嵌入时,使用池化输出:
english_embeddings = english_outputs.pooler_output
其他语言的输出:
italian_sentences = [
"cane",
"I cuccioli sono carini.",
"Mi piace fare lunghe passeggiate lungo la spiaggia con il mio cane.",
]
japanese_sentences = ["犬", "子犬はいいです", "私は犬と一緒にビーチを散歩するのが好きです"]
italian_inputs = tokenizer(italian_sentences, return_tensors="pt", padding=True)
japanese_inputs = tokenizer(japanese_sentences, return_tensors="pt", padding=True)
with torch.no_grad():
italian_outputs = model(**italian_inputs)
japanese_outputs = model(**japanese_inputs)
italian_embeddings = italian_outputs.pooler_output
japanese_embeddings = japanese_outputs.pooler_output
对于句子间的相似度计算,建议在计算相似度之前先进行 L2 归一化:
import torch.nn.functional as F
def similarity(embeddings_1, embeddings_2):
normalized_embeddings_1 = F.normalize(embeddings_1, p=2)
normalized_embeddings_2 = F.normalize(embeddings_2, p=2)
return torch.matmul(
normalized_embeddings_1, normalized_embeddings_2.transpose(0, 1)
)
print(similarity(english_embeddings, italian_embeddings))
print(similarity(english_embeddings, japanese_embeddings))
print(similarity(italian_embeddings, japanese_embeddings))
详细信息
关于数据、训练、评估和性能指标的详细信息,请参阅 原始论文。
BibTeX 条目及引用信息
@inproceedings{mao-nakagawa-2023-lealla,
title = "{LEALLA}: Learning Lightweight Language-agnostic Sentence Embeddings with Knowledge Distillation",
author = "Mao, Zhuoyuan and
Nakagawa, Tetsuji",
booktitle = "Proceedings of the 17th Conference of the European Chapter of the Association for Computational Linguistics",
month = may,
year = "2023",
address = "Dubrovnik, Croatia",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2023.eacl-main.138",
doi = "10.18653/v1/2023.eacl-main.138",
pages = "1886--1894",
abstract = "Large-scale language-agnostic sentence embedding models such as LaBSE (Feng et al., 2022) obtain state-of-the-art performance for parallel sentence alignment. However, these large-scale models can suffer from inference speed and computation overhead. This study systematically explores learning language-agnostic sentence embeddings with lightweight models. We demonstrate that a thin-deep encoder can construct robust low-dimensional sentence embeddings for 109 languages. With our proposed distillation methods, we achieve further improvements by incorporating knowledge from a teacher model. Empirical results on Tatoeba, United Nations, and BUCC show the effectiveness of our lightweight models. We release our lightweight language-agnostic sentence embedding models LEALLA on TensorFlow Hub.",
}