Deep Tech Point
first stop in your tech adventure

Exploring Sentence Embeddings with Sentence Transformers

May 2, 2024 | AI

In this article we will learn what are sentence embeddings and transformers and how they typically work. We will also take a look at the practically applications of sentence embeddings in theory. However, we will roll our sleeves and take get our hands dirty with applying SentenceTransformer(“all-MiniLM-L6-v2”) model, which works on tasks like semantic similarity comparison, clustering, and classification. In this exercise, which is in-depth described in Google Colab we delve into the functionalities of the sentence-transformers Python library and explore how it can be utilized to generate and compare sentence embeddings.

What are sentence transformers and how they typically work?

Sentence transformers are a type of model specifically designed to generate high-quality sentence embeddings. These embeddings capture the semantic meaning of a sentence in a dense vector space, allowing for various natural language processing (NLP) tasks such as text classification, semantic similarity, clustering, and information retrieval.

Here’s how they typically work:

1. Pretrained Models

Sentence transformers are often based on pre-trained transformer architectures like BERT, RoBERTa, or DistilBERT. These models are trained on large corpora of text data using unsupervised or semi-supervised learning approaches to learn contextual representations of words and sentences.

2. Fine-tuning for Sentence Embeddings

The pretrained transformer models are fine-tuned on specific tasks related to generating sentence embeddings. This fine-tuning process involves training the model on a labeled dataset with tasks such as semantic textual similarity (STS), natural language inference (NLI), or paraphrase identification. During fine-tuning, the model learns to encode sentences into fixed-length vectors while preserving their semantic meaning.

3. Sentence Embedding Extraction

Once the model is trained, it can be used to encode input sentences into embeddings. These embeddings are typically fixed-length vectors where each dimension captures some aspect of the sentence’s meaning. The embeddings are generated in such a way that semantically similar sentences are represented by vectors that are close together in the embedding space.

Applications of sentence embeddings

Sentence embeddings produced by these models can be used in various downstream NLP tasks. They enable a wide range of applications that require understanding and processing of natural language text at the sentence level, for example:

Sentence transformers exercise with all-MiniLM-L6-v2 model

Sentence embeddings, which transform textual data into numerical representations, have become a cornerstone in natural language processing (NLP). They facilitate various tasks like semantic similarity comparison, clustering, and classification. In this exercise, which is in-depth described in Google Colab we delve into the functionalities of the sentence-transformers Python library and explore how it can be utilized to generate and compare sentence embeddings.

Installation

To begin, we need to install the sentence-transformers package. Utilizing pip, the Python package manager, we execute the following command:

!pip install sentence-transformers

This command installs the necessary dependencies, enabling us to leverage the capabilities of sentence-transformers within our Python environment.

Setting Logging Verbosity

Logging verbosity configuration is crucial, especially in production environments, to manage the volume of log messages efficiently. Within the Transformers library, the logging module allows us to control the verbosity level. By invoking

logging.set_verbosity_error()

, we configure the logging behavior to display only critical error messages, disregarding less significant log messages.

Generating Sentence Embeddings

The SentenceTransformer class from the sentence_transformers module empowers us to transform sentences into fixed-dimensional vectors, often termed as sentence embeddings.

from sentence_transformers import SentenceTransformer

By initializing an instance of this class with a pre-trained model identifier, such as “all-MiniLM-L6-v2”, we equip ourselves with a powerful tool capable of encoding sentences into dense vector representations.

model = SentenceTransformer("all-MiniLM-L6-v2")

This code snippet defines a Python list variable named sentences1, which contains three string elements. Each string element represents a sentence or a textual passage:

sentences1 = ['Pyton is under my bed',
              'A man is sitting on a porch',
              'The movies are so boring']

The encode() method processes each sentence in sentences1 through the pre-trained transformer model and generates a numerical representation (embedding) for each sentence. The resulting embeddings are stored in the variable embeddings1. Each row of embeddings1 corresponds to the embedding of a sentence in sentences1. These embeddings can be used for various downstream NLP tasks, such as semantic similarity comparison, clustering, or classification:

embeddings1 = model.encode(sentences1, convert_to_tensor=True)

The embeddings stored in embeddings1 represent the numerical representations of each sentence in sentences1 obtained after processing them through the pre-trained SentenceTransformer model. Each row in embeddings1 corresponds to the embedding of a respective sentence from sentences1:

embeddings1

Basically the same applies as for the sentences1 and embeddings1:

sentences2 = ['The snake is under my bed',
              'A woman is sitting on a porch',
              'The new movie made me sleepy']
embeddings2 = model.encode(sentences2,
                           convert_to_tensor=True)
embeddings2

By importing the util module, you gain access to the functions and classes defined within it, allowing you to use them in your Python code. These utilities might include functions for processing text data, manipulating embeddings, performing evaluation tasks, or other common NLP-related operations.

For example, you might find functions in util for calculating similarity scores between embeddings, loading pre-trained models, tokenizing text, or converting between different data formats.

from sentence_transformers import util

Computing Similarity Scores

Cosine similarity serves as a fundamental metric to quantify the likeness between sentence embeddings. Leveraging the cos_sim function from the util module within sentence_transformers, we calculate the cosine similarity scores between pairs of embeddings. These scores range from -1 to 1, where higher values (closer to 1) denote increased similarity.

Application Example

To illustrate the practical usage of sentence embeddings and cosine similarity scores, let’s consider an example. We have two sets of sentences: sentences1 and sentences2. By encoding these sentences using the initialized SentenceTransformer model, we obtain their respective embeddings. Subsequently, we compute cosine similarity scores between pairs of embeddings, representing the similarity between corresponding sentences.

Conclusion

This article has provided an in-depth exploration of sentence embeddings and transformers, shedding light on their fundamental workings and practical applications in the field of natural language processing (NLP). By understanding how sentence transformers operate, particularly through the lens of the all-MiniLM-L6-v2 model, you have gained valuable insights into the process of generating and comparing sentence embeddings for tasks such as semantic similarity comparison, clustering, and classification.

Through a step-by-step exercise detailed in Google Colab, you have been equipped with the necessary knowledge and tools to implement sentence transformers in your own projects. From installation to generating embeddings and computing similarity scores, this exercise has demonstrated the tangible utility of the sentence-transformers Python library in real-world scenarios.

As the demand for NLP solutions continues to grow across various industries, the significance of sentence embeddings and transformers cannot be overstated. These technologies enable researchers and practitioners to unlock new possibilities in understanding, processing, and extracting insights from natural language text. With the foundation laid out in this article, you are now well-prepared to embark on your own NLP endeavors, leveraging the power of sentence transformers to tackle complex linguistic challenges and drive innovation in the field.