NER With Transformers and spaCy (Python)
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
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
32
33
▶
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
Related Reads
📰
📰
📰
📰
I Added 200+ Languages to a Translator… Then Realized Language Wasn't the Hardest Part
Dev.to · Bhavin Sheth
OpenAI Unveils GPT-Live: Voice AI That Feels Truly Conversational
Medium · AI
OpenAI Unveils GPT-Live: Voice AI That Feels Truly Conversational
Medium · Machine Learning
The Majority Machine
Medium · LLM
🎓
Tutor Explanation
DeepCamp AI