Sentence Similarity With Sentence-Transformers in Python
Key Takeaways
This video demonstrates how to use the sentence-transformers library in Python to compare similarity between different sentences, utilizing the BERT model for creating sentence embeddings and the sklearn library for calculating cosine similarity.
Full Transcript
and welcome to this video on using the sentence transformers library to compare similarity between different sentences so this generally pretty short video i'm not going to go really into depth i'm just going to show you how to actually use the library now if you do want to go into a little more depth i have another video that i'll be releasing just before this one and that will go into what is actually happening here how we are calculating similarity or or pulling the how the model that we'll be using is actually creating those embeddings and then how we're actually calculating the similarity there so if you're interested in that go check it out otherwise if you just want to get a quick similarity score between two sentences this is probably the way to go so we have these six sentences up here and this one three years later the coffin was still full of jello and this one the person box was packed with jelly many dozens of months later they're saying the same thing but the second one is saying in a way that most of us wouldn't normally say it instead of saying coughing we're saying person box instead of jello we're saying jelly i think that's kind of normal actually and instead of years we're saying dozens of months so it's not really sharing the same words but we're going to see that we can actually find that these two sentences are the most similar out of all of these so we're taking those and we're going to be importing the sentence transformers library and we want to import the sentence transformer and then from that we want to initialize a sentence transformer model so we write sentence transformer and then in here we're going to be using this model that i've already defined a model name for which is the bert base mli mean tokens model so initialize that i need to rerun that so we have our model and i'll just show you really quickly this model is coming from the hugging face transformers library behind sentence transformers so this is the actual model we are using now first thing we do here is create our sentence vectors or sentence embeddings so we'll call a sentence vex equals model and code and all we need to do here is pass our sentences so we can pass a single sentence or a list of sentences it's completely fine and then let's just have a quick look at what we have here so you see that we have this big array and if we look at the shape we see that we have a 6 by 768 array so the six refers to our six sentences here and the 768 refers to the hidden state size within the bert model that we're using so each one of these sentences is now being represented by a dense vector containing 768 values and that means that we already take those and compare similarity between them so to do that we're going to be using the sklearn implementation of cosine similarity which we can import like this so sklearn pairwise or metrics pairwise and we import cosine similarity and to calculate our cosine similarity all we do is take that function and inside here we pass our first sentence so this three years later the coffin is still full of jello i want to pass that sentence vector which is just in index zero of our sentence vector array and because we are extracting that single array value so if we just have a look at this you see that we have a almost like a list of lists here if we just extract this we only get a list so what we want to do is actually keep that inside a list otherwise we'll get dimension error and then we do sentence vex one onwards so this will be the remaining sentences okay so let's take these or let's just bring them down here calculate this and we can see that our highest similarity by quite a bit is just 0.72 now that means that between this sentence and this sentence we have a similarity score of 0.72 so clearly it's working it's scoring the highest similarity and you can play around this and and test multiple different words and sentences and just see how it works but that's the easy way putting all this together so i think it's really cool that we can do that so easily but i don't think there's really anything else to say about it so thank you for watching and i'll see you in the next one
Original Description
🎁 Free NLP for Semantic Search Course:
https://www.pinecone.io/learn/nlp
Hard mode: https://youtu.be/jVPd7lEvjtg
All we ever seem to talk about nowadays are BERT this, BERT that. I want to talk about something else, but BERT is just too good - so this video will be about BERT for sentence similarity.
A big part of NLP relies on similarity in highly-dimensional spaces. Typically an NLP solution will take some text, process it to create a big vector/array representing said text - then perform several transformations.
It's highly-dimensional magic.
Sentence similarity is one of the clearest examples of how powerful highly-dimensional magic can be.
The logic is this:
- Take a sentence, convert it into a vector.
- Take many other sentences, and convert them into vectors.
- Find sentences that have the smallest distance (Euclidean) or smallest angle (cosine similarity) between them - more on that here.
- We now have a measure of semantic similarity between sentences - easy!
At a high level, there's not much else to it. But of course, we want to understand what is happening in a little more detail and implement this in Python too.
🤖 70% Discount on the NLP With Transformers in Python course:
https://bit.ly/3DFvvY5
Medium article:
https://towardsdatascience.com/bert-for-measuring-text-similarity-eec91c6bf9e1
🎉 Sign-up For New Articles Every Week on Medium!
https://medium.com/@jamescalam/membership
📖 If membership is too expensive - here's a free link:
https://towardsdatascience.com/bert-for-measuring-text-similarity-eec91c6bf9e1?sk=c0f2990b4660210b447e52d55bd0f4e5
👾 Discord
https://discord.gg/c5QtDB9RAP
🕹️ Free AI-Powered Code Refactoring with Sourcery:
https://sourcery.ai/?utm_source=YouTub&utm_campaign=JBriggs&utm_medium=aff
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from James Briggs · James Briggs · 32 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
▶
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Stoic Philosophy Text Generation with TensorFlow
James Briggs
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
Every New Feature in Python 3.10.0a2
James Briggs
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
How-to use the Kaggle API in Python
James Briggs
Language Generation with OpenAI's GPT-2 in Python
James Briggs
Text Summarization with Google AI's T5 in Python
James Briggs
How-to do Sentiment Analysis with Flair in Python
James Briggs
Python Environment Setup for Machine Learning
James Briggs
Sequential Model - TensorFlow Essentials #1
James Briggs
Functional API - TensorFlow Essentials #2
James Briggs
Training Parameters - TensorFlow Essentials #3
James Briggs
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
Building a PlotLy $GME Chart in Python
James Briggs
How-to Use The Reddit API in Python
James Briggs
How to Build Custom Q&A Transformer Models in Python
James Briggs
How to Build Q&A Models in Python (Transformers)
James Briggs
How-to Decode Outputs From NLP Models (Python)
James Briggs
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
Unicode Normalization for NLP in Python
James Briggs
The NEW Match-Case Statement in Python 3.10
James Briggs
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
How to Build Python Packages for Pip
James Briggs
How-to Structure a Q&A ML App
James Briggs
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
Q&A Document Retrieval With DPR
James Briggs
How to Use Type Annotations in Python
James Briggs
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
Sentence Similarity With Sentence-Transformers in Python
James Briggs
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
NER With Transformers and spaCy (Python)
James Briggs
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
New Features in Python 3.10
James Briggs
Training BERT #5 - Training With BertForPretraining
James Briggs
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
Faiss - Introduction to Similarity Search
James Briggs
Angular App Setup With Material - Stoic Q&A #5
James Briggs
Why are there so many Tokenization methods in HF Transformers?
James Briggs
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
How LSH Random Projection works in search (+Python)
James Briggs
IndexLSH for Fast Similarity Search in Faiss
James Briggs
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
Build NLP Pipelines with HuggingFace Datasets
James Briggs
Composite Indexes and the Faiss Index Factory
James Briggs
More on: RAG Basics
View skill →Related Reads
📰
📰
📰
📰
RAG Is Not a Feature. It's a System, and These Are the Parts Nobody Demos.
Dev.to · Marketing wizr
What Is RAG? The AI Technology That Makes ChatGPT Smarter Without Retraining
Medium · RAG
Understanding the Limits of Linear RAG — and Why Agentic Workflows Are Catching On
Medium · AI
Understanding the Limits of Linear RAG — and Why Agentic Workflows Are Catching On
Medium · Machine Learning
🎓
Tutor Explanation
DeepCamp AI