💡 Learn from AI

Introduction to Embeddings in Large Language Models

GloVe

GloVe

GloVe (Global Vectors for Word Representation) is another popular embedding method that is widely used in natural language processing. It was introduced by researchers at Stanford University in 2014.

How GloVe Works

GloVe is similar to Word2Vec in that it uses co-occurrence matrices to learn word embeddings. However, it differs from Word2Vec in how it constructs these matrices. Instead of using a sliding window over a sentence to count the co-occurrences of words, GloVe constructs a matrix that contains the global co-occurrence statistics for all words in a corpus. This matrix is then factorized using a technique similar to singular value decomposition. The resulting word embeddings capture not only the local context of words but also their global context across the entire corpus.

Advantages of GloVe

One of the advantages of GloVe over Word2Vec is that it is less computationally intensive. This is because the co-occurrence matrix has much fewer non-zero elements than the matrix used by Word2Vec. Additionally, GloVe has been shown to outperform Word2Vec on some tasks, such as word analogy tasks.

Example of Using GloVe

Here is an example of how GloVe embeddings can be used:

import numpy as np
import pandas as pd
import os
import urllib.request
import zipfile

# Download and unzip the GloVe embeddings
url = 'http://nlp.stanford.edu/data/glove.6B.zip'
urllib.request.urlretrieve(url, 'glove.6B.zip')
with zipfile.ZipFile('glove.6B.zip', 'r') as zip_ref:
    zip_ref.extractall('glove.6B')
    
# Load the embeddings into a dictionary
embeddings = {}
with open('glove.6B/glove.6B.50d.txt', 'r', encoding='utf-8') as f:
    for line in f:
        values = line.split()
        word = values[0]
        vector = np.asarray(values[1:], dtype='float32')
        embeddings[word] = vector
        
# Use the embeddings to find the most similar words to a given word
def most_similar(word):
    distances = {}
    for w in embeddings:
        if w != word:
            distances[w] = np.dot(embeddings[word], embeddings[w]) / (np.linalg.norm(embeddings[word]) * np.linalg.norm(embeddings[w]))
    return sorted(distances.items(), key=lambda x: x[1], reverse=True)[:10]

most_similar('cat')
Take quiz (4 questions)

Previous unit

Word2Vec

Next unit

BERT

All courses were automatically generated using OpenAI's GPT-3. Your feedback helps us improve as we cannot manually review every course. Thank you!