stsb xlm r multilingual

sentence-transformers

Introduction

The sentence-transformers/stsb-xlm-r-multilingual model is designed to map sentences and paragraphs into a 768-dimensional dense vector space, useful for clustering and semantic search tasks. It is part of the Sentence Transformers library, which enhances sentence similarity tasks.

Architecture

The model architecture consists of:

  • A Transformer model: XLMRobertaModel with a sequence length of up to 128 tokens, maintaining the case of the input text.
  • A Pooling layer that performs mean pooling over token embeddings to generate fixed-size sentence embeddings.

Training

This model was trained using a multilingual dataset, following the Sentence-BERT framework. It uses a Siamese BERT-network approach to derive sentence embeddings. Further information can be found in the related publication: Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks.

Guide: Running Locally

To run the model locally, follow these steps:

  1. Install Dependencies:

    pip install -U sentence-transformers
    
  2. Usage with Sentence Transformers:

    from sentence_transformers import SentenceTransformer
    
    sentences = ["This is an example sentence", "Each sentence is converted"]
    model = SentenceTransformer('sentence-transformers/stsb-xlm-r-multilingual')
    embeddings = model.encode(sentences)
    print(embeddings)
    
  3. Alternative Usage with Hugging Face Transformers:

    from transformers import AutoTokenizer, AutoModel
    import torch
    
    def mean_pooling(model_output, attention_mask):
        token_embeddings = model_output[0]
        input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
        return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
    
    sentences = ['This is an example sentence', 'Each sentence is converted']
    tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/stsb-xlm-r-multilingual')
    model = AutoModel.from_pretrained('sentence-transformers/stsb-xlm-r-multilingual')
    encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
    
    with torch.no_grad():
        model_output = model(**encoded_input)
    
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
    print("Sentence embeddings:")
    print(sentence_embeddings)
    
  4. Cloud GPUs Suggestion: For enhanced performance, especially with large datasets, consider using cloud-based GPUs such as those offered by AWS, GCP, or Azure.

License

This model is licensed under the Apache 2.0 License.

More Related APIs in Sentence Similarity