Now that you have some understanding of
word vectors or word embeddings from our
last video in this video we are going to
look at how you can get access of word
vectors in Spacy Library. We are not
going to do any text classification in
this particular video that is something
we'll do in the next video. To use word
embeddings in Spacy you need to load
the large or medium model. So large model
you can specify by LG, medium model you
can specify by
MD and the difference between these two
is this: see if you go to Spacy
English model page you will find that
for a small model there are no vectors.
See Vector says zero Keys zero unique
vectors, but the medium model has 514
keys
and these many unique vectors okay and
the each Vector has 300 Dimension. But
the large model has
more number of unique vectors instead of
20K you have
514k. Now if you are confused about
what word vectors is we have already
covered it in the last video so you
should have brief overview. In this video
we are just writing our code. To install
this large model you can run this
particular command on your command
prompt so something like this
and when you run it the model is big so
it's gonna take some time so please have
some patience. Now I'm assuming you have
downloaded the model
you can write your first statement here.
Let's say I want to compare word vectors
with different words such as dog, cat
banana
and some unknown garbage word right and
we can just say dog for example and
for token in dock
yeah
print token okay or token.text and I
want to print another thing called has
vector. So if you tab it
this will be a flag indicating if this
token has a vector or not it will also
print is oov OV is out of vocabulary
okay? So oov
and has vector
you notice that
the common words like dog, cat, banana has
vectors.
Whereas, some random word will not have
vector. For example
Dhaval P right,
because when species model was trained
it probably hasn't seen this particular
word and spaCy using
Globe embedding Global vectors and they
trained it on some popular English data
set right and some data set from
internet where it has captured general
English knowledge but it doesn't know
what the word the Dhaval P means or you
can give some regional word for example
Kem it's and it doesn't have any vector
see false and out of vocabulary is true.
So you can see that Spacy gives out of
the box excess of a vector and if you
want to print a particular vector
you can print it by saying this see doc,
zero so 0 will be dog one will be cat
2 will be banana okay? And if you do this
dot vector it will print that vector see
the size of this Vector is 300 Dimension.
So you can do
shape and you will find it's a 300
dimension vector. So spaCy see large model
comes with inbuilt vector so the moment
you create this document object you are
already getting this individual word
vectors okay?
Now let's try to compare these word
vectors and see what kind of similarity
we get between this word. So I will
create another
document called bread okay and I will
just call it a base token,
base token
and
base token.vector.shape
So even you you get by the way you you
might ask me okay why didn't you do this?
Well that and the sentence both will
give you the same embedding but but just
to be technically correct let me do this
okay so zeroth word is bread and the
vector that you are getting is this
particular vector. But since it has only
one word the sentence vector is same as
the
that individual word vector,
and the way spacy works is if you
give multiple word it will the sentence
vector will be the average of individual
word vectors okay?
And we will cover that in more detail
later. But now I want to compare this
word bread with different words such as
let's say sandwich, burger, cat, tiger
human and wheat. Now when I compare bread
with sandwich they are very similar
right? They are they appear in the same
context,
bread and burger are also kind of
matching, but bread and car are not
matching. So I'm expecting that the
similarity between these two words will
be higher. Whereas, similarly between bread
and car should be lower,
and you can check that by going over all
these tokens so I'm going token by token
and I'm saying that okay print.
So I will use Python's format string and
I will say okay my token.text
and I want to compare it with base token
.text so I'm comparing all these
words with bread okay, and I will put in
the similarity. How do you print the
similarity? So you can just do token.
similarity. So Spacy has this inbuilt
function
with base token
based open zero and base token both will
give you the same vector,
and you will notice that the similarity
between sandwich and bread is higher. So
one means they are exactly similar okay?
So let me do that so if I do bread here
see bread and bread is exactly similar,
one is actually similar zero is like not
similar. So as you get closer towards one
it means more similar.
Sandwich and bread is 0.6 burger and
bread point four. But look at car and
bread. See 0.06 they're not similar at
all so it's close to zero, tiger and
bread point zero tiger doesn't eat bread
right? Human eats bread so there is some
similarity and wheat and bread is more
similarity. I want to make sure you
understand that this is not
some magic bullet right, you don't you
don't give some random words and expect
that okay this will work perfectly. You
will see a lot of issues with this model
as well.
You have to understand how these models
are trained. Let's say you take all the
text from Google news and Wikipedia all
the text okay and you train this model
the way these models work is they will
look at the context of the words. So in
the language if human and bread is
appearing together, it tends to give it
more similarity. But car and bread if you
look at any Wikipedia article or Google
News anything any book, car and bread
will not appear in a similar context but
wheat and bread will appear in a similar
context okay? So if two words appear in a
similar context,
it will have higher similarity. Same
thing with like let's say profit and
loss, profit and loss will have more
similarity. Now someone may argue why
they are more similar they are exactly
opposite. You have to understand what do
you mean by similarity here.
This similarity means they are appearing
in the same context. So profit and loss
would appear in a similar context if you
are looking at companies financial
document or reading some financial
article. That's why they will have more
similarity but actually they are
antonyms. Okay, so you are not trying to
create a some English dictionary here so
folks please have that understanding
clear in your bread in your brain, I was
just going to say bread. All right! So now
I will
create a function for printing a
similarity. So I will use the similar
code okay same code and create this
function called print similarity okay,
and I will say base word and the words
to compare. This way I can give
random words and get the similarity. So
my base token would be what NLP base word
right
and then
my document would be NLP
words to compare.
So so the the reason I'm creating this
function is I want to give it this type
of value I want to compare iPhone with
random words okay?
So and then for token in doc
kind of similar thing okay? So I'll just
use copy paste just to save some time
and when you compare these you find that
apple and iPhone are 0.43 symbol, Samsung
and iPhone are 0.67, now again someone
will argue Apple is more similar to
iPhone than Samsung is similar to iPhone,
well not necessarily. See again this
model is trained on some Google news
articles let's say some Wikipedia
articles and in news people tend to
compare
Samsung and iPhone more,
versus apple and iPhone on whatever text
this model is trained it appears Samsung
and iPhone have more similarity.
iPhone to iPhone is obviously one, dog
and iPhone see dog and iPhone is 0.08
dogs don't use iPhone. So uh that's the
reason
kitten and iPhone maybe there are so
many pictures of kittens or people
making TikTok videos and you know
they might refer kitten and iPhone
together. It's kind of hard to explain
this similarity but you get a sense of
it if two words are appearing in the
same context, they will have more
similarity you need to just remember
that.
Now we already looked at out of
vocabulary here right, where if the word
doesn't appear in the Corpus that you
have trained on now, see this word cam is
from my local regional language it's an
Indian language called Gujarati on
Gujarati language, if I train a model
then it will be able to identify all
these Gujarati words you know
right? All these Gujarati words it will
be able to identify if the model is
trained on Gujarati language Corpus. But
this spacy's is default model is trained
on an English Corpus that's why it can
understand English.
Okay now
you can do something like this so I'm
just going to again copy paste, we already
looked at like King minus man plus woman
okay that kind of
that kind of equation and if you compare
result with the vector of queen and the
way you can compare this result using is
using cosine similarity
in one of the I think other videos on
YouTube. Let me show you I think we have
talked about cosine similarities so if
you don't know what cosine similarity
just code Basics cosine
similarity and
you can go through this video okay? It's
very important this concept will help
you in a in interview as well.
So the cosine similarity
the way you print that is by using this
function from sklearn and you can just
compare your result with the queen.
So there's a king if you take the
element of man from that king
what is left is the king has an
authority king is super rich king has
power but you take the element of man
and add the element of woman then woman
will be queen and queen also has
authority, power, money all of that so she
has all of that so you are just
replacing the gender and now you get
queen. So here you get 0.61 similarity
now people might think okay why is it
not close to one,
I have explained this before it's not
always perfect but more than 0.5 you get
it they are considered quite similar. All
right so that's all I had for a Spacy
word vectors. In the future videos we
will look at the word vectors in Gensim
as well as we'll see how you can do text
classification both using Spacy word
vectors and Gensim word vectors all
right that's all I had. In the next video
we will do fake versus real News
classification using these Spacy
vectors.
[Music]