LASER (Language-Agnostic SEntence Representations)
repository·main·Indexed 25 days ago
https://github.com/facebookresearch/laserA toolkit for calculating multilingual sentence embeddings used for cross-lingual document classification, bitext filtering, mining, and similarity search. It includes the laser_encoders package (v0.0.2), LaserEncoderPipeline for simple embedding generation, and support for LASER2 and LASER3 encoders. The library provides utilities for reconstructing sentences from NLLB and WMT22 African bitext metadata, a Tatoeba multilingual test set for evaluation, and a Docker image with a Flask REST API for vectorization.
What's inside LASER
- LASER is a library designed to calculate and utilize multilingual sentence embeddings. It enables the generation of sentence-level representations that are language-agnostic, facilitating tasks like cross-lingual transfer.
Specify languages during Docker build
mainYou can pre-download encoders and tokenizers for specific languages using the
langsbuild argument. This argument accepts a space-separated list of language codes (e.g.,eng_Latn,fra_Latn).docker build --build-arg langs="eng_Latn fra_Latn" -t laser -f docker/Dockerfile .Run the XNLI cross-lingual natural language inference application
mainTo demonstrate the use of multilingual sentence embeddings for cross-lingual Natural Language Inference (NLI), you can run the provided XNLI application. This process trains an NLI classifier on the English MultiNLI corpus (optimizing meta-parameters on the English XNLI development corpus) and then applies it to the test sets of 14 transfer languages.
Running the script performs the following tasks:
- Installs the XNLI and MultiNLI corpora.
- Calculates the multilingual sentence embeddings.
- Trains the classifier.
- Displays the results.
bash ./xnli.shDownload LASER encoders for WMT '22
mainTo download encoders for all 24 supported languages for the WMT '22 shared task, run the
download_models.shscript located in the current directory. The models will be stored in$LASER/models/wmt22.Language Model Formats:
- Most focus languages use the
laser3-xxxformat. - Afrikaans (
afr), English (eng), and French (fra) are supported by thelaser2model.
Supported Languages:
amh,fuv,hau,ibo,kam,kin,lin,lug,luo,nso,nya,orm,sna,som,ssw,swh,tsn,tso,umb,wol,xho,yor, andzul.bash ./download_models.sh- Most focus languages use the
Run the xSIM sample script
mainTo quickly download a sample dataset (flores200), a sample encoder (laser2), and calculate the sentence embeddings and the xSIM error rate for a set of languages, run the provided evaluation script via bash.
bash ./eval.shRun the multilingual similarity search demonstration
mainTo demonstrate LASER's application to multilingual similarity search, you can run a provided script that downloads the
newstest2012(WMT 2012) dataset, calculates sentence embeddings for an N-way parallel corpus, and computes the similarity search error rate for each language pair.Note: The parallel corpus used for this task must not contain duplicate sentences, as the error rate calculation relies on matching sentence indices between the source and target languages in the joint embedding space.
bash ./wmt.shExtract sentences from paragraphs in bitext data
mainTo extract sentences from paragraphs within this dataset, use the sentence splitter utility provided in the LASER repository. The utility can be found in theutilsdirectory of the main repository.Run the LASER clustering tutorial in Google Colab
mainYou can explore generating multilingual embeddings and performing clustering on the MASSIVE dataset using the provided Google Colab notebook. This tutorial demonstrates how LASER encodes sentences from multiple languages into a shared embedding space for cross-lingual clustering.
https://colab.research.google.com/github/Paulooh007/LASER/blob/laser-clustering/tasks/clustering/LaserClusteringExample.ipynbExtract parallel texts from WikiMatrix TSV files
mainUse the
extract.pytool to process WikiMatrix TSV files and extract parallel texts based on a margin score threshold.Usage
Run the script with the following arguments:
--tsv: Path to the.tsv.gzfile.--bitext: Path to the output.txtbitext file.--src-lang: ISO code for the source language.--trg-lang: ISO code for the target language.--threshold: The margin score threshold.
Threshold Optimization
- Higher threshold: Increases the likelihood that sentences are mutual translations, but results in less data.
- Recommended value: A threshold of
1.04is a good starting point for most language pairs.
Note: The data is distributed under the Creative Commons Attribution-ShareAlike license.
python3 extract.py \ --tsv WikiMatrix.en-fr.tsv.gz \ --bitext WikiMatrix.en-fr.txt \ --src-lang en --trg-lang fr \ --threshold 1.04Set up cross-lingual document classification with MLDoc
mainTo use LASER for cross-lingual document classification using the MLDoc corpus, follow these steps:
- Download the MLDoc corpus: Obtain the corpus from the MLDoc repository.
- Install the corpus: Place the downloaded files in a directory named
MLDoc. - Run the training and embedding pipeline: Execute the provided shell script to calculate multilingual sentence embeddings for all languages and train the classifier.
The system allows training a document classifier on one language (e.g., English) and applying it to other languages (e.g., German, Spanish, French, Italian, Japanese, Russian, and Chinese) without requiring language-specific resources for the target languages.
bash ./mldoc.shRun the LASER Docker image
mainTo run the standard LASER container, use the following command:
docker run -it laserInstall the `laser_encoders` package
mainYou can install the core sentence embedding package using pip:
pip install laser_encoders