Your Guide to Using Sentence Transformers

Mar 29, 2024 | Educational

Sentence transformers are powerful tools designed to convert sentences and paragraphs into dense vector representations. These models allow you to tackle a range of tasks including clustering and semantic search. However, as with any tool, understanding how to effectively use them is key to unlocking their potential. Let’s dive in!

Understanding Sentence Transformers

This particular model, identified as bert-large-nli-stsb-mean-tokens, maps sentences to a 1024-dimensional vector space. Imagine trying to find a needle in a haystack; the better your needle (or sentence) is represented in a vector space, the easier it is to find it amidst many others. However, a note of caution: this model is deprecated and will produce low-quality embeddings. Therefore, it’s recommended to explore alternative models available on SBERT.net – Pretrained Models.

Getting Started

To begin using sentence transformers, you need to install the sentence-transformers library. Here’s how:

pip install -U sentence-transformers

Once you have the library set up, you can utilize the model in your Python code. Below are examples for both direct usage and a more indirect approach using Hugging Face Transformers.

Usage Example with Sentence-Transformers

Here’s how you can use the sentence-transformer model directly:

from sentence_transformers import SentenceTransformer

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

Using Hugging Face Transformers

If you prefer not to use sentence-transformers, here’s the alternative approach using Hugging Face:

from transformers import AutoTokenizer, AutoModel
import torch

# Define your 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)

# Define your sentences
sentences = ["This is an example sentence.", "Each sentence is converted."]
# Load the model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/bert-large-nli-stsb-mean-tokens')
model = AutoModel.from_pretrained('sentence-transformers/bert-large-nli-stsb-mean-tokens')

# Tokenize the 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)

In this code, the model acts like a chef who prepares a dish. The ingredients (tokenized sentences) undergo a cooking process (the transformer model) and are finally plated (mean-pooled) for presentation (the sentence embeddings). Just like with cooking, it’s essential to follow the recipe correctly for the best results!

Troubleshooting

If you encounter issues while using these models, consider the following tips:

  • Make sure you have installed the latest version of the libraries.
  • Verify that your Python environment is correctly set up and has access to all required libraries.
  • If embeddings are not as expected, try using recommended models as this model is deprecated.
  • For best practices, ensure that the input sentences are well-formed and consistent.

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