NER With Transformers and spaCy (Python)

James Briggs · Beginner ·🧠 Large Language Models ·5y ago

Key Takeaways

This video teaches named entity recognition (NER) using Transformers and spaCy in Python

Full Transcript

okay in this video we're going to have a look at using named entity recognition through the spacey library which i've already done a video on before but this time rather than using the traditional spacing models we're going to use a transformer model now the cell for this is is pretty similar to normal spacey so the very first thing we want to do is obviously install spacey transformers so to do that all we need to write is pip install spacey and then in square brackets transformers now alongside this we can also install what we need to run it with cuda so if you have cuda you can check your version number like this okay and so for me i can see that i have cuda 11.1 and so what i would do rather than just writing pip install spacey transformers i would write this transformers and then after this i include my cuda version so i write cuda and i'm on 11.1 so i put 1 1 1. and then i'd enter this and install spacey but i already have it installed so i'm not going to do that again now once we have installed spacey transformers we also need to download the transformer model from spacey and to do that we would write this so we write python and we specify the spacing module and then we write download and the transformer model is very similar to the other spacing models if you've used them before we have to do this for every space model not just the transformer model um so it's english for depending on which model you want i'm going to be using the english one that all our other models as well for other languages we're using the english core web and i think there's only a web transfer model but i'm not 100 sure on that so don't take my word for that and then the transformer part of this is trf at the end so usually the the models we typically use are sm for small and lg for large this time we're using the transformer version of that model again i already have it installed so i'm not going to run it again and now to start using this we do what we would normally do with spaces so all we actually need to do is import spacey and i'm also going to import displaces so that i can visualize the name entity recognition so from spacey import displacy and then what we do is this is almost exactly the same as what we always do with spacey there's really no difference here so we do nlp spacey dot load and then here we include our model name so usually what we do is write encore web sm or lg this time we're just going to write trf for transformer run that and once we've loaded that we can perform named entity recognition as we normally would so what we do is doc nlp and then in here we pass our text and then we want to visualize that and we can do that in jupiter just like this so render and then we pass in the document or the dock that we just created and we want the style to be entities because we're doing any r here okay so there we have our trf model now i'm going to swap these around because i want to compare this to what we would normally use which is the let's use the large model because it's probably the closest match to the transform model and i'll just call this lg and modify this to be lg as well and what we'll do is just do the same thing again here and here and rerun that and you can see that in this case there's actually no difference now i think this is to be expected because the traditional spacey model especially the large model is still really good in terms of performance so in most cases or at least a fair few cases we shouldn't really see that much difference because this is still a pretty solid model and we can you can see this as well here so this is a longer text i got this from the investing subreddit and let's do the same thing again so we're going to take the transformer model also the traditional model will convert both of those to dots so let's do it like this let's try and keep it reasonably tidy okay and then we just want this and this again okay now again with this one we don't see any difference because like i said this traditional spacey model is still pretty good but what we can do is start adding more complex or or at least longer pieces of text and and then let's compare how they perform because in this case the transform model will outperform the traditional model okay so this text is quite a bit longer and now let me let me copy these two bring them down here and let's do that again i'm not sure why sorry i'm loading the reloading the model that i didn't realize okay and here and here now let's compare these two so the transformer model is correctly identifying fastly as an organization the large model is identified fastly as a person so you see a little bit of difference there um then up here we have this quarter 121 identified as a date by a transform model not at all with the large traditional model and go a little bit further so this one i'm not really sure if this is a good thing or bad thing so the transformer model is identifying a whopping 27 as the percent which i don't i don't think that's a i don't think that's really a good thing but it depends on what you're doing maybe maybe you kind of want that exaggeration of the percentage in there i but i don't think so so i suppose that depends but i would say probably transform model is actually performing worse with that single that single use that single case the rest of these the money centers they're all matching between the two models here the large model is pulling out this one cardinal i'm not sure we really want that one in there to be honest and then if we continue the transform model is getting the q1 date and the fastly organization down here usual model missing q1 we do have facets person and then down here the large model is seeing cfo as an organization which is obviously not the case it's actually uh it's a position obviously so yeah i mean they're not far off of each other for sure but i think in this case the transform model is definitely outperforming at least by a little bit and i think the more complex the language gets the better the transform model can form so i mean that's it for this video i just wanted to give you a quick demonstration of spacey and transformers which i think is really cool it can actually use both of those packages together so i think thank you very much for watching and i'll see you again in the next one

Original Description

Named entity recognition (NER) consists of extracting 'entities' from text - what we mean by that is given the sentence: "Apple reached an all-time high stock price of 143 dollars this January." We might want to extract the key pieces of information - or 'entities' - and categorize each of those entities. Like so: - Apple  : Organization - 143 dollars :  Monetary Value - this January :  Date For us humans, this is easy. But how can we teach a machine to distinguish between a granny smith apple and the Apple we trade on NASDAQ? (No, we can't rely on the 'A' being capitalized…) This is where NER comes in - using NER, we can extract keywords like apple and identify that it is, in fact, an organization - not a fruit. The go-to library for NER is spaCy, which is incredible. But what if we added transformers to spaCy? Even better - we'll cover exactly that in this video. 🤖 70% Discount on the NLP With Transformers in Python course: https://bit.ly/3DFvvY5
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from James Briggs · James Briggs · 34 of 60

1 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

Related Reads

📰
I Added 200+ Languages to a Translator… Then Realized Language Wasn't the Hardest Part
Building a translator with 200+ languages reveals that language itself isn't the hardest part, prompting a reevaluation of the challenges in translation technology
Dev.to · Bhavin Sheth
📰
OpenAI Unveils GPT-Live: Voice AI That Feels Truly Conversational
Learn about GPT-Live, a voice AI that enables truly conversational interactions, and its potential applications
Medium · AI
📰
OpenAI Unveils GPT-Live: Voice AI That Feels Truly Conversational
Learn about GPT-Live, a voice AI that enables truly conversational interactions, and its potential applications
Medium · Machine Learning
📰
The Majority Machine
Learn how AI can lead us to mistake consensus for truth and why this matters for critical thinking
Medium · LLM
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →