Views
No views yet
1import torch
2from transformers import BertModel, BertTokenizerFast
3
4
5tokenizer = BertTokenizerFast.from_pretrained("setu4993/LEALLA-large")
6model = BertModel.from_pretrained("setu4993/LEALLA-large")
7model = model.eval()
8
9english_sentences = [
10 "dog",
11 "Puppies are nice.",
12 "I enjoy taking long walks along the beach with my dog.",
13]
14english_inputs = tokenizer(english_sentences, return_tensors="pt", padding=True)
15
16with torch.no_grad():
17 english_outputs = model(**english_inputs)english_embeddings = english_outputs.pooler_output1italian_sentences = [
2 "cane",
3 "I cuccioli sono carini.",
4 "Mi piace fare lunghe passeggiate lungo la spiaggia con il mio cane.",
5]
6japanese_sentences = ["犬", "子犬はいいです", "私は犬と一緒にビーチを散歩するのが好きです"]
7italian_inputs = tokenizer(italian_sentences, return_tensors="pt", padding=True)
8japanese_inputs = tokenizer(japanese_sentences, return_tensors="pt", padding=True)
9
10with torch.no_grad():
11 italian_outputs = model(**italian_inputs)
12 japanese_outputs = model(**japanese_inputs)
13
14italian_embeddings = italian_outputs.pooler_output
15japanese_embeddings = japanese_outputs.pooler_output1import torch.nn.functional as F
2
3
4def similarity(embeddings_1, embeddings_2):
5 normalized_embeddings_1 = F.normalize(embeddings_1, p=2)
6 normalized_embeddings_2 = F.normalize(embeddings_2, p=2)
7 return torch.matmul(
8 normalized_embeddings_1, normalized_embeddings_2.transpose(0, 1)
9 )
10
11
12print(similarity(english_embeddings, italian_embeddings))
13print(similarity(english_embeddings, japanese_embeddings))
14print(similarity(italian_embeddings, japanese_embeddings))1@inproceedings{mao-nakagawa-2023-lealla,
2 title = "{LEALLA}: Learning Lightweight Language-agnostic Sentence Embeddings with Knowledge Distillation",
3 author = "Mao, Zhuoyuan and
4 Nakagawa, Tetsuji",
5 booktitle = "Proceedings of the 17th Conference of the European Chapter of the Association for Computational Linguistics",
6 month = may,
7 year = "2023",
8 address = "Dubrovnik, Croatia",
9 publisher = "Association for Computational Linguistics",
10 url = "https://aclanthology.org/2023.eacl-main.138",
11 doi = "10.18653/v1/2023.eacl-main.138",
12 pages = "1886--1894",
13 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.",
14}