How to Utilize Sentence Transformers for Sentence Similarity Tasks

Mar 31, 2024 | Educational

Welcome to your go-to guide on effectively using the sentence-transformers, specifically the roberta-base-nli-stsb-mean-tokens model for sentence embedding. While this model has been deprecated due to its low-quality embeddings, it is still valuable to understand how it was utilized and the alternatives available.

Understanding Sentence Transformers

A sentence transformer can be likened to a highly skilled translation artist who takes sentences or paragraphs and converts them into a universal language: a dense vector space. This vector space is 768 dimensions wide, allowing for nuanced representations of the underlying meanings of the sentences. Such embeddings can be utilized in various tasks including clustering, semantic search, and more.

Installation of Sentence Transformers

To get started, you’ll need to have the sentence-transformers library installed. Here’s how you can do that:

pip install -U sentence-transformers

Using the Model

Once you have the library installed, you can start using the model. Below is a simple code snippet to illustrate this process:

python
from sentence_transformers import SentenceTransformer

sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformersroberta-base-nli-stsb-mean-tokens')
embeddings = model.encode(sentences)
print(embeddings)

This is akin to making a smoothie: you toss in the ingredients (your sentences), and the blender (model) processes them into a smooth mix (embeddings) ready for use.

Using HuggingFace Transformers

If you prefer not to use sentence-transformers, here’s an alternative approach using HuggingFace directly:

python
from transformers import AutoTokenizer, AutoModel
import torch

# 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 for embeddings
sentences = ["This is an example sentence", "Each sentence is converted"]

# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('sentence-transformersroberta-base-nli-stsb-mean-tokens')
model = AutoModel.from_pretrained('sentence-transformersroberta-base-nli-stsb-mean-tokens')

# 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
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)

Think of this as a team effort. You have different squad members, where some (tokenizers) prepare the inputs, others (models) provide the core processing, and finally, the pooling method combines everyone’s contributions into meaningful embeddings.

Evaluation of the Model

If you’re curious about how this model performed in automated evaluations, you can find more information in the Sentence Embeddings Benchmark.

Troubleshooting Common Issues

  • Low Quality Sentence Embeddings: If you encounter low-quality embeddings, it’s important to note that this specific model is deprecated. Explore the updated models mentioned on SBERT.net – Pretrained Models.
  • Installation Problems: Ensure that you are using an updated version of Python and that the sentence-transformers library is installed correctly. You may need to run pip install --upgrade pip.
  • Pooling Errors: Make sure that you utilize the correct input dimensions and mask to avoid shape misalignment errors during pooling.

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

Conclusion

In this blog, we have discussed how to utilize sentence transformers, particularly focusing on the roberta-base-nli-stsb-mean-tokens model. We have provided both the usage of this model and an alternative method using the HuggingFace Transformers library. Remember to explore recommended models for production-level tasks and continue updating your techniques accordingly!

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