🚀 LEALLA-large
LEALLA是一组轻量级的语言无关句子嵌入模型,支持109种语言。它从LaBSE中提炼而来,可用于获取多语言句子嵌入和双语文本检索。
🚀 快速开始
模型信息
此模型从TF Hub的v1模型迁移而来。该模型两个版本生成的嵌入是等效的。不过,对于某些语言(如日语),LEALLA模型在比较嵌入和相似度时似乎需要更高的容差。
模型使用
基础用法
import torch
from transformers import BertModel, BertTokenizerFast
tokenizer = BertTokenizerFast.from_pretrained("setu4993/LEALLA-large")
model = BertModel.from_pretrained("setu4993/LEALLA-large")
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))
📚 详细文档
关于数据、训练、评估和性能指标的详细信息,请参考原始论文。
📄 许可证
本项目采用Apache-2.0许可证。
📑 引用信息
@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.",
}
📋 模型信息表格
属性 |
详情 |
模型类型 |
轻量级语言无关句子嵌入模型,从LaBSE提炼而来 |
训练数据 |
CommonCrawl、Wikipedia |
支持语言 |
af、am、ar等共109种语言 |
标签 |
bert、sentence_embedding、multilingual、google、sentence-similarity、lealla、labse |