So in this lecture, we are going to look at how to do named entity recognition, also known as NPR
in TensorFlow.
So what is NPR?
Well, basically the idea is you have a document and you want to identify all the people, places and
companies in that document.
This data might be used as a pre processing step in a larger piece of code.
So, for example, if I see Apple, I would mark that as a company.
If I see Steve Jobs, I would mark that as a person.
And if I see CA., I would mark that as a place.
In any case, suppose you have some use for it.
How does this actually work?
Well, it turns out that an air is essentially exactly the same as parts of speech tagging, which you
already know how to do.
As you recall, that was a many to many task where we assign each word to a tag and we do the same thing
with NPR.
So every place has the tag look, every person has the tag per and every company has the tag org.
Now, one challenging aspect of NPR is that some entities span multiple tokens.
So, for example, consider Steve Jobs.
This has two tokens, but both of them are part of a name.
If you consider jobs by itself, that would be a regular word, meaning occupations.
Another challenging aspect of any R is that this data is highly imbalanced.
You can see that most words just get the tag.
Oh, which means that it's not a place person or company.
Consider this sentence from our dataset.
Sheep have been long known to contract scrapie a brain wasting disease similar to BSE, which is believed
to have been transferred to cattle through feed containing animal waste.
This is a very long sentence and it has no named entities.
All the labels are just oh.
Let's briefly discuss the format of any AirTags, since you're probably wondering what all these A's
and B's signify.
This is called IOB format since obviously we use the letters I, O and B in our tags.
The idea is that each entity potentially appears as a chunk, a chunk as a sequence of multiple tokens.
An example of that is Steve Jobs.
In this case, we would use Beeper to represent the fact that Steve is a person tag and it's the beginning
of a chunk for jobs.
We would use IPR to represent the fact that jobs is also a person tag and it's inside a trunk.
So B stands for beginning, AI stands for inside and O stands for outside, meaning outside of any chunk.
Luckily, these details don't really concern us, since ultimately our data set looks exactly like post
tagging from the previous lecture.
We have a sequence of words and each word is mapped to attack.
Now, it turns out that the code for this is so simple that we are not even going to bother stepping
through it like we normally would.
I hope you remember my famous rule that makes this possible, and that rule is all data is the same.
Thanks to this rule, no code needs to be written.
We simply change the data set and the previous code still works.
So at the top here you can see that I've imported pickle and I've downloaded the train set and test
set, which are pickle files.
This is because the data format from the original data was a bit different than what we needed.
So I did some processing for the course and uploaded the results to my website.
This dataset is called Cornwall two thousand three, which is probably the most popular NPR dataset
that exists.
In any case, let's scroll down to the results.
As you can see, we get a very high accuracy, and that one is also high but a bit lower as expected,
especially for the test set.
This may be due to a large class imbalance where the model performs poorly on some under-represented
class.
I'll let you investigate that yourself.
Importantly, we must again compare it to our baseline, which is to simply memorize the tags from the
train sets.
As you can see, we get worse performance using the baseline and the test F1 is now even worse.
Therefore, we can conclude that our model was successful in making use of context to predict named
entities.