fbpx
Learn to build large language model applications: vector databases, langchain, fine tuning and prompt engineering. Learn more

Demystifying embeddings 101 – The foundation of large language models

Ruhma Khawaja author
Ruhma Khawaja

August 16

Embeddings are a key building block of large language models. For the unversed, large language models (LLMs) are composed of several key building blocks that enable them to efficiently process and understand natural language data.

A large language model (LLM) is a type of artificial intelligence model that is trained on a massive dataset of text. This dataset can be anything from books and articles to websites and social media posts. The LLM learns the statistical relationships between words, phrases, and sentences in the dataset, which allows it to generate text that is similar to the text it was trained on.

How is a large language model built?

LLMs are typically built using a transformer architecture. Transformers are a type of neural network that are well-suited for natural language processing tasks. They are able to learn long-range dependencies between words, which is essential for understanding the nuances of human language.

 

Learn to build custom large language model applications today!                                                 

 

LLMs are so large that they cannot be run on a single computer. They are typically trained on clusters of computers or even on cloud computing platforms. The training process can take weeks or even months, depending on the size of the dataset and the complexity of the model.

Key building blocks of large language model

Foundation of LLM
Foundation of LLM

1. Embeddings

Embeddings are continuous vector representations of words or tokens that capture their semantic meanings in a high-dimensional space. They allow the model to convert discrete tokens into a format that can be processed by the neural network. LLMs learn embeddings during training to capture relationships between words, like synonyms or analogies.

2. Tokenization

Tokenization is the process of converting a sequence of text into individual words, subwords, or tokens that the model can understand. LLMs use subword algorithms like BPE or wordpiece to split text into smaller units that capture common and uncommon words. This approach helps to limit the model’s vocabulary size while maintaining its ability to represent any text sequence.

3. Attention

Attention mechanisms in LLMs, particularly the self-attention mechanism used in transformers, allow the model to weigh the importance of different words or phrases. By assigning different weights to the tokens in the input sequence, the model can focus on the most relevant information while ignoring less important details. This ability to selectively focus on specific parts of the input is crucial for capturing long-range dependencies and understanding the nuances of natural language.

 

 

4. Pre-training

Pre-training is the process of training an LLM on a large dataset, usually unsupervised or self-supervised, before fine-tuning it for a specific task. During pretraining, the model learns general language patterns, relationships between words, and other foundational knowledge.

The process creates a pretrained model that can be fine-tuned using a smaller dataset for specific tasks. This reduces the need for labeled data and training time while achieving good results in natural language processing tasks (NLP).

 

5. Transfer learning

Transfer learning is the technique of leveraging the knowledge gained during pretraining and applying it to a new, related task. In the context of LLMs, transfer learning involves fine-tuning a pretrained model on a smaller, task-specific dataset to achieve high performance on that task. The benefit of transfer learning is that it allows the model to benefit from the vast amount of general language knowledge learned during pretraining, reducing the need for large labeled datasets and extensive training for each new task.

Understanding embeddings

Embeddings are used to represent words as vectors of numbers, which can then be used by machine learning models to understand the meaning of text. Embeddings have evolved over time from the simplest one-hot encoding approach to more recent semantic embedding approaches.

Embeddings
Embeddings – By Data Science Dojo

Types of embeddings

 

Type of embedding

 

 

Description

 

Use-cases

Word embeddings Represent individual words as vectors of numbers. Text classification, text summarization, question answering, machine translation
Sentence embeddings Represent entire sentences as vectors of numbers. Text classification, text summarization, question answering, machine translation
Bag-of-words (BoW) embeddings Represent text as a bag of words, where each word is assigned a unique ID. Text classification, text summarization
TF-IDF embeddings Represent text as a bag of words, where each word is assigned a weight based on its frequency and inverse document frequency. Text classification, text summarization
GloVe embeddings Learn word embeddings from a corpus of text by using global co-occurrence statistics. Text classification, text summarization, question answering, machine translation
Word2Vec embeddings Learn word embeddings from a corpus of text by predicting the surrounding words in a sentence. Text classification, text summarization, question answering, machine translation

Classic approaches to embeddings

In the early days of natural language processing (NLP), embeddings were simply one-hot encoded. Zero vector represents each word with a single one at the index that matches its position in the vocabulary.

1. One-hot encoding

One-hot encoding is the simplest approach to embedding words. It represents each word as a vector of zeros, with a single one at the index corresponding to the word’s position in the vocabulary. For example, if we have a vocabulary of 10,000 words, then the word “cat” would be represented as a vector of 10,000 zeros, with a single one at index 0.

One-hot encoding is a simple and efficient way to represent words as vectors of numbers. However, it does not take into account the context in which words are used. This can be a limitation for tasks such as text classification and sentiment analysis, where the context of a word can be important for determining its meaning.

For example, the word “cat” can have multiple meanings, such as “a small furry mammal” or “to hit someone with a closed fist.” In one-hot encoding, these two meanings would be represented by the same vector. This can make it difficult for machine learning models to learn the correct meaning of words.

2. TF-IDF

TF-IDF (term frequency-inverse document frequency) is a statistical measure that is used to quantify the importanceThe process creates a pretrained model that can be fine-tuned using a smaller dataset for specific tasks. This reduces the need for labeled data and training time while achieving good results in natural language processing tasks (NLP). of a word in a document. It is a widely used technique in natural language processing (NLP) for tasks such as text classification, information retrieval, and machine translation.

TF-IDF is calculated by multiplying the term frequency (TF) of a word in a document by its inverse document frequency (IDF). TF measures the number of times a word appears in a document, while IDF measures how rare a word is in a corpus of documents.

The TF-IDF score for a word is high when the word appears frequently in a document and when the word is rare in the corpus. This means that TF-IDF scores can be used to identify words that are important in a document, even if they do not appear very often.

 

Large language model bootcamp

Understanding TF-IDF with example

Here is an example of how TF-IDF can be used to create word embeddings. Let’s say we have a corpus of documents about cats. We can calculate the TF-IDF scores for all of the words in the corpus. The words with the highest TF-IDF scores will be the words that are most important in the corpus, such as “cat,” “dog,” “fur,” and “meow.”

We can then create a vector for each word, where each element of the vector represents the TF-IDF score for that word. The TF-IDF vector for the word “cat” would be high, while the TF-IDF vector for the word “dog” would also be high, but not as high as the TF-IDF vector for the word “cat.”

The TF-IDF word embeddings can then be used by a machine-learning model to classify documents about cats. The model would first create a vector representation of a new document. Then, it would compare the vector representation of the new document to the TF-IDF word embeddings. The document would be classified as a “cat” document if its vector representation is most similar to the TF-IDF word embeddings for “cat.”

Count-based and TF-IDF 

To address the limitations of one-hot encoding, count-based and TF-IDF techniques were developed. These techniques take into account the frequency of words in a document or corpus.

Count-based techniques simply count the number of times each word appears in a document. TF-IDF techniques take into account both the frequency of a word and its inverse document frequency.

Count-based and TF-IDF techniques are more effective than one-hot encoding at capturing the context in which words are used. However, they still do not capture the semantic meaning of words.

Capturing local context with N-grams

To capture the semantic meaning of words, n-grams can be used. N-grams are sequences of n-words. For example, a 2-gram is a sequence of two words.

N-grams can be used to create a vector representation of a word. The vector representation is based on the frequencies of the n-grams that contain the word.

N-grams are a more effective way to capture the semantic meaning of words than count-based or TF-IDF techniques. However, they still have some limitations. For example, they are not able to capture long-distance dependencies between words.

Semantic encoding techniques

Semantic encoding techniques are the most recent approach to embedding words. These techniques use neural networks to learn vector representations of words that capture their semantic meaning.

One of the most popular semantic encoding techniques is Word2Vec. Word2Vec uses a neural network to predict the surrounding words in a sentence. The network learns to associate words that are semantically similar with similar vector representations.

Semantic encoding techniques are the most effective way to capture the semantic meaning of words. They are able to capture long-distance dependencies between words and they are able to learn the meaning of words even if they have never been seen before. Here are some other semantic encoding techniques:

1. ELMo: Embeddings from language models

ELMo is a type of word embedding that incorporates both word-level characteristics and contextual semantics. It is created by taking the outputs of all layers of a deep bidirectional language model (bi-LSTM) and combining them in a weighted fashion. This allows ELMo to capture the meaning of a word in its context, as well as its own inherent properties.

The intuition behind ELMo is that the higher layers of the bi-LSTM capture context, while the lower layers capture syntax. This is supported by empirical results, which show that ELMo outperforms other word embeddings on tasks such as POS tagging and word sense disambiguation.

ELMo is trained to predict the next word in a sequence of words, a task called language modeling. This means that it has a good understanding of the relationships between words. When assigning an embedding to a word, ELMo takes into account the words that surround it in the sentence. This allows it to generate different embeddings for the same word depending on its context.

Understanding ELMo with example

For example, the word “play” can have multiple meanings, such as “to perform” or “a game.” In standard word embeddings, each instance of the word “play” would have the same representation. However, ELMo can distinguish between these different meanings by taking into account the context in which the word appears. In the sentence “The Broadway play premiered yesterday,” for example, ELMo would assign the word “play” an embedding that reflects its meaning as a theater production.

ELMo has been shown to be effective for a variety of natural language processing tasks, including sentiment analysis, question answering, and machine translation. It is a powerful tool that can be used to improve the performance of NLP models.

2. GloVe

GloVe is a statistical method for learning word embeddings from a corpus of text. GloVe is similar to Word2Vec, but it uses a different approach to learning the vector representations of words.

How GloVe works

GloVe works by creating a co-occurrence matrix. The co-occurrence matrix is a table that shows how often two words appear together in a corpus of text. For example, the co-occurrence matrix for the words “cat” and “dog” would show how often the words “cat” and “dog” appear together in a corpus of text.

GloVe then uses a machine learning algorithm to learn the vector representations of words from the co-occurrence matrix. The machine learning algorithm learns to associate words that appear together frequently with similar vector representations.

3. Word2Vec

Word2Vec is a semantic encoding technique that is used to learn vector representations of words. Word vectors represent word meaning and can enhance machine learning models for tasks like text classification, sentiment analysis, and machine translation.

Word2Vec works by training a neural network on a corpus of text. The neural network is trained to predict the surrounding words in a sentence. The network learns to associate words that are semantically similar with similar vector representations.

There are two main variants of Word2Vec:

  • Continuous Bag-of-Words (CBOW): The CBOW model predicts the surrounding words in a sentence based on the current word. For example, the model might be trained to predict the words “the” and “dog” given the word “cat”.
  • Skip-gram: The skip-gram model predicts the current word based on the surrounding words in a sentence. For example, the model might be trained to predict the word “cat” given the words “the” and “dog”.

Word2Vec has been shown to be effective for a variety of tasks, including:

  • Text classification: Word2Vec can be used to train a classifier to classify text into different categories, such as news articles, product reviews, and social media posts.
  • Sentiment analysis: Word2Vec can be used to train a classifier to determine the sentiment of text, such as whether it is positive, negative, or neutral.
  • Machine translation: Word2Vec can be used to train a machine translation model to translate text from one language to another.

 

 

 

 

GloVe Word2Vec ELMo
Accuracy More accurate Less accurate More accurate
Training time Faster to train Slower to train Slower to train
Scalability More scalable Less scalable Less scalable
Ability to capture long-distance dependencies Not as good at capturing long-distance dependencies Better at capturing long-distance dependencies Best at capturing long-distance dependencies

 

Word2Vec vs Dense word embeddings

Word2Vec is a neural network model that learns to represent words as vectors of numbers. Word2Vec is trained on a large corpus of text, and it learns to predict the surrounding words in a sentence.

Word2Vec can be used to create dense word embeddings. Dense word embeddings are vectors that have a fixed size, regardless of the size of the vocabulary. This makes them easy to use with machine learning models.

Dense word embeddings have been shown to be effective in a variety of NLP tasks, such as text classification, sentiment analysis, and machine translation.

Read more –> Top vector databases in the market – Guide to embeddings and VC pipeline

Conclusion

Semantic encoding techniques are the most recent approach to embedding words and are the most effective way to capture their semantic meaning. They are able to capture long-distance dependencies between words and they are able to learn the meaning of words even if they have never been seen before.

Safe to say, embeddings are a powerful tool that can be used to improve the performance of machine learning models for a variety of tasks, such as text classification, sentiment analysis, and machine translation. As research in NLP continues to evolve, we can expect to see even more sophisticated embeddings that can capture even more of the nuances of human language.

Register today

Ruhma Khawaja author
Written by Ruhma Khawaja
Interested in writing for us? Apply here: Submit your guest post with us
Newsletters | Data Science Dojo
Up for a Weekly Dose of Data Science?

Subscribe to our weekly newsletter & stay up-to-date with current data science news, blogs, and resources.

Data Science Dojo | data science for everyone

Discover more from Data Science Dojo

Subscribe to get the latest updates on AI, Data Science, LLMs, and Machine Learning.