🚀 LEALLA-small
LEALLA是一组轻量级、语言无关的句子嵌入模型,支持109种语言。它从LaBSE模型蒸馏而来,可用于获取多语言句子嵌入和双语语料检索。
🚀 快速开始
LEALLA是从LaBSE蒸馏得到的轻量级语言无关句子嵌入模型集合,支持109种语言。该模型在获取多语言句子嵌入和双语语料检索方面表现出色。
此模型从TF Hub的v1模型迁移而来,两个版本模型生成的嵌入是等价的。不过,对于某些语言(如日语),在比较嵌入和相似度时,LEALLA模型似乎需要更高的容差。
✨ 主要特性
- 支持109种语言,包括但不限于英语、意大利语、日语等。
- 轻量级设计,在多语言句子嵌入和双语语料检索方面表现出色。
- 从LaBSE模型蒸馏而来,保证了一定的性能。
📦 安装指南
文档未提及具体安装步骤,可参考相关依赖库的安装方式,如torch
和transformers
。
💻 使用示例
基础用法
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
计算句子相似度
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.",
}
📄 许可证
本项目采用Apache-2.0许可证。
信息表格
属性 |
详情 |
模型类型 |
轻量级语言无关句子嵌入模型 |
训练数据 |
CommonCrawl、Wikipedia |