Understanding Sentence Transformers: A Step-by-Step Guide

Nov 24, 2022 | Educational

In today’s AI-driven world, the ability to measure the similarity between sentences is vital for numerous applications, including clustering, semantic search, and more. This blog will walk you through using a **sentence-transformers** model that converts sentences into a 768-dimensional dense vector space. Buckle up, as we simplify this powerful tool!

1. What Are Sentence Transformers?

Sentence transformers are models specifically designed to generate embeddings for sentences and paragraphs. The key insight is that similar sentences will result in similar vector representations, making it easier to quantify semantic similarity.

2. Getting Started with the Model

To harness the power of sentence-transformers, first, you need to arrange your environment with the necessary libraries. Follow these steps:

Installing the Library

Start by installing the **sentence-transformers** library. Open your terminal and run:

pip install -U sentence-transformers

Using the Model

The practical usage of the model becomes straightforward once the library is installed. Here’s an example:

from sentence_transformers import SentenceTransformer

sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('MODEL_NAME')
embeddings = model.encode(sentences)
print(embeddings)

In this code, we define a list of sentences, use a pre-trained model to convert these sentences into vector embeddings, and finally print the output.

3. Using Hugging Face Transformers

If you want to work without the **sentence-transformers** library, you can leverage the Hugging Face library like this:

from transformers import AutoTokenizer, AutoModel
import torch

# Mean Pooling - Take attention mask into account for correct averaging
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 we want sentence embeddings for
sentences = ["This is an example sentence", "Each sentence is converted"]

# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('MODEL_NAME')
model = AutoModel.from_pretrained('MODEL_NAME')

# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')

# Compute token embeddings
with torch.no_grad():
    model_output = model(**encoded_input)

# Perform pooling (mean pooling)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)

This code snippet showcases how to tokenize sentences, compute their embeddings, and conduct mean pooling for further analysis.

4. Evaluating Your Model

For an automated evaluation of your model’s performance, you can refer to the **Sentence Embeddings Benchmark** at seb.sbert.net.

5. Model Training Details

The **sentence-transformers** model undergoes rigorous training using various parameters. Key points include:

  • Data Loader: A list of 960 samples for efficient data handling.
  • Batch Size: Configured to 4 for optimal performance.
  • Loss Function: Utilizes CosineSimilarityLoss for accurate error calculations.
  • Optimizer: Employs AdamW for effective gradient descent.

6. Troubleshooting Common Issues

If you encounter any issues while setting up or using the model, here are some troubleshooting ideas:

  • Ensure the **sentence-transformers** library is correctly installed.
  • Check for any typos in your code (especially in sentence definitions or model names).
  • Verify the versions of your libraries to ensure compatibility.
  • Look for error messages in the terminal and search for solutions online.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox