paraphrase Mini L M L6 v2

sentence-transformers

Introduction

The paraphrase-MiniLM-L6-v2 model is a sentence-transformers model that converts sentences and paragraphs into 384-dimensional dense vector spaces. It is suitable for tasks such as clustering and semantic search.

Architecture

The model architecture is a SentenceTransformer consisting of a Transformer layer and a Pooling layer. The Transformer is based on a BertModel with a maximum sequence length of 128 tokens. The pooling layer uses mean pooling to aggregate token embeddings into sentence embeddings.

Training

The model was trained by the sentence-transformers team, as outlined in the paper "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks" by Reimers and Gurevych.

Guide: Running Locally

To run this model locally, you can use either the sentence-transformers library or the Hugging Face Transformers library.

Using Sentence-Transformers

  1. Install the library:
    pip install -U sentence-transformers
    
  2. Use the model:
    from sentence_transformers import SentenceTransformer
    
    sentences = ["This is an example sentence", "Each sentence is converted"]
    model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')
    embeddings = model.encode(sentences)
    print(embeddings)
    

Using Hugging Face Transformers

  1. Install necessary packages:
    pip install transformers torch
    
  2. Use the model:
    from transformers import AutoTokenizer, AutoModel
    import torch
    
    # Define mean pooling function
    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/paraphrase-MiniLM-L6-v2')
    model = AutoModel.from_pretrained('sentence-transformers/paraphrase-MiniLM-L6-v2')
    
    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)
    

For better performance, consider using cloud GPUs such as AWS EC2, GCP, or Azure.

License

The paraphrase-MiniLM-L6-v2 model is licensed under the Apache 2.0 License.

More Related APIs in Sentence Similarity