| Map > Data Science > Predicting the Future > Modeling > Classification > Large Language Model > Embeddings | ||||||||||||
Embeddings: How LLMs Represent Meaning |
||||||||||||
| An embedding converts an object (text, image, sound) into a vector of numbers that represents its meaning or characteristics. For example: | ||||||||||||
|
||||||||||||
| These numbers are not manually assigned. A neural network learns them from large amounts of data. | ||||||||||||
|
The important idea is: Similar meanings → vectors that are close together |
||||||||||||
| Why do we need embeddings? | ||||||||||||
| Consider this search: "automobile" | ||||||||||||
|
"I purchased a car." |
||||||||||||
| A traditional keyword search may only find: | ||||||||||||
|
"I bought a new automobile." |
||||||||||||
| because it contains the word automobile. However, an embedding-based search can recognize that: | ||||||||||||
|
automobile ≈ car ≈ vehicle |
||||||||||||
| and therefore return all three. This is called semantic search. | ||||||||||||
| Embeddings and distance | ||||||||||||
| Once text has been converted to vectors, we can calculate how similar two vectors are. A common measure is cosine similarity. For two vectors: | ||||||||||||
|
A = [1, 2, 3] |
||||||||||||
| cosine similarity is: | ||||||||||||
|
||||||||||||
| The result is between approximately: | ||||||||||||
|
-1 → completely opposite |
||||||||||||
| For text embeddings, we generally want semantically similar sentences to have a high cosine similarity. | ||||||||||||
| Python example � no neural network yet | ||||||||||||
| For our first model, we'll use a very simple word tokenizer. | ||||||||||||
| Python
import numpy as np |
||||||||||||
| Now define cosine similarity: | ||||||||||||
|
Python def cosine_similarity(a, b): |
||||||||||||
| Compare dog and puppy:: | ||||||||||||
| Python
print(cosine_similarity(texts["dog"], texts["puppy"])) print( cosine_similarity(texts["dog"], texts["car"] ) ) |
||||||||||||
| You should get something conceptually like: | ||||||||||||
|
dog vs puppy →
cosine=0.99 → high similarity |
||||||||||||
| This demonstrates the mathematical principle of embeddings. | ||||||||||||
| Real embeddings with Python | ||||||||||||
| Now let's use an actual embedding model. A very convenient library is Sentence Transformers. | ||||||||||||
|
pip install sentence-transformers |
||||||||||||
| Then: | ||||||||||||
|
Python from sentence_transformers
import SentenceTransformer |
||||||||||||
| You will get approximately: | ||||||||||||
|
(5, 384) |
||||||||||||
| Each sentence has been converted into a 384-dimensional vector. | ||||||||||||
| Calculate similarity | ||||||||||||
| Now we use cosine to calculate similarity. | ||||||||||||
|
Python from sklearn.metrics.pairwise import cosine_similarity |
||||||||||||
| You'll get a 5 � 5 matrix: | ||||||||||||
|
[[1.0000001 0.59701616 0.54455847 0.27561283 0.06691159] |
||||||||||||
| The model learns these vectors during training. | ||||||||||||
| Embedding search | ||||||||||||
| This becomes particularly powerful when we have thousands or millions of record. | ||||||||||||
| Python
documents = [ |
||||||||||||
| Our user searches: | ||||||||||||
|
"How should I take care of my puppy?" |
||||||||||||
| We create an embedding for the query: | ||||||||||||
| Python
query = "How should I take care of my puppy?" |
||||||||||||
| Then calculate similarity: | ||||||||||||
| Python
document_embeddings = model.encode(documents) |
||||||||||||
| Sort them:. | ||||||||||||
|
Python results = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True) |
||||||||||||
| You might get: | ||||||||||||
|
0.719 Puppies require special care. |
||||||||||||
| Notice something important. The query doesn't necessarily contain the exact words from the document.. | ||||||||||||
|
Query: |
||||||||||||
| Yet the embedding model recognizes their semantic relationship. | ||||||||||||
| Embedding vs. LLM | ||||||||||||
| An important distinction: | ||||||||||||
|
||||||||||||
| So, embedding primarily represents meaning but LLM generates language. | ||||||||||||
| Embedding can represent different types of data, including text, images, audio, video, and speech, by converting them into numerical vectors that capture their meaningful features. | ||||||||||||
| This is why embeddings are so powerful for AI systems. | ||||||||||||
| This is a small neural network that learns embeddings from text, so you can see exactly what is happening rather than using a pre-trained embedding library. The example uses a simple word-context prediction task, which is conceptually related to Word2Vec. | ||||||||||||