Unlocking the Power of Sentence Transformers

Mar 31, 2024 | Educational

If you’re diving into the world of semantic search or text clustering, you’ve likely heard of the sentence-transformers library. This powerful tool maps sentences and paragraphs to a 768-dimensional dense vector space, enabling enhanced sentence similarity assessments. In this blog post, we’ll guide you step by step on how to use the sentence-transformers library, troubleshoot common issues, and understand the underlying concepts like a pro!

Getting Started with Sentence Transformers

Before you can start extracting features from your sentences, you need to install the sentence-transformers library. You can easily do this using pip:

pip install -U sentence-transformers

Usage: Sentence-Transformers Model

Once you’ve installed the library, using it is straightforward. Let’s walk through an example:

from sentence_transformers import SentenceTransformer

sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/msmarco-roberta-base-v3')
embeddings = model.encode(sentences)
print(embeddings)

In this example, think of the model as a magician who transforms ordinary sentences into invisible threads of meaning. Each thread represents a unique understanding of the original sentence in a dense vector space.

Usage: HuggingFace Transformers

If you want to use the model without the sentence-transformers library, you can do it using HuggingFace Transformers. Here’s how:

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]  # First element of model_output contains all token embeddings
    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('sentence-transformers/msmarco-roberta-base-v3')
model = AutoModel.from_pretrained('sentence-transformers/msmarco-roberta-base-v3')

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

Here, we’re introducing an additional layer of intricacy with the HuggingFace approach. Imagine this as assembling a complex puzzle: each token embedding is a piece that, when combined using mean pooling, reveals the bigger picture of the sentence’s meaning.

Evaluation Results

For those interested in automated evaluation of this model, you can check the Sentence Embeddings Benchmark.

Full Model Architecture

The architecture can be visualized as follows:

SentenceTransformer(
  (0): Transformer(max_seq_length: 510, do_lower_case: False) with Transformer model: RobertaModel
  (1): Pooling(word_embedding_dimension: 768, pooling_mode_cls_token: False, pooling_mode_mean_tokens: True, pooling_mode_max_tokens: False, pooling_mode_mean_sqrt_len_tokens: False)
)

Troubleshooting Common Issues

When working with such models, issues may arise. Here are some tips:

  • Model Not Found: Double-check the model name and ensure you’ve installed the library.
  • CUDA Errors: If you’re using GPU acceleration, ensure your CUDA toolkit matches your PyTorch version.
  • Out of Memory: If you’re handling large datasets, consider using batch processing or reducing your sequence length.

If you encounter any persistent issues, don’t hesitate to reach out. 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