Hello guys.
So in this video we are going to basically see the word two vec practical implementation I really want
to show you some Google pre-trained models and basically give you an idea like how word two vec creates
a vector, right?
So for this tutorial I'm going to use a library which is called as Gensim.
So let's go ahead and let's install this particular library.
So you just need to write pip install Gensim.
So here you can see that requirement is already satisfied.
And uh, what I am actually going to do is that I'm going to import this import Gensim.
And from Gensim dot model I'm going to import word two vec and keyed vectors okay.
And I'll talk about this.
Why this two libraries are specifically required.
Now one very important thing for this as I said that to show you the practical implementation of word
two vec here, I'm going to take a Google pre-trained model.
In the upcoming video I will try to show you a different model which can be trained from scratch.
But here I'm going to show you a Google Word two vec pre-trained model and what this particular model
is all about.
So I'm basically taking a word two vec Google news 300 okay.
And this is basically a pre-trained vector trained on Google News data set about 100 billion words.
The model contains 300 dimensional vectors for 3 million words and phrases.
Write.
These phrases were obtained using a sample data driven approach in distributed representation of this
and all.
All the research paper and everything is basically given over here.
Okay, so this same model we are going to use and we are going to see that how it can easily create
a vectors whenever we give any kind of words.
So in Gensim you know you have something called as API, right?
So there is a library called as API.
So you just need to write import Gensim dot downloader as API.
And you just need to write API dot load and basically the model name.
Right.
So the model name is nothing but word two vec Google news.
Uh dash 300 okay.
Now once you do this, all you have to do is that when you provide any word inside this w v variable,
which is nothing, but this is an instance of this specific model.
It will try to give you the vector.
I'm not going to execute this line of code because I've already done it, because the model size is
1662.8 MB, right?
So it will probably take some time for you all to download.
So I have already downloaded this so that I can record the video directly.
You can just go ahead and download it.
Now let me go and see that how the King vector look like.
Okay.
So this is basically the king uh, vectors, how the word is basically converted into vectors and the
dimensions that you'll be seeing.
Right.
How many vectors are there.
As said from here.
Right.
We have 300 dimensions.
So this is what we are getting.
This entire vectors you'll be able to see will be having 300 dimensions.
So if I probably use this vector underscore king dot shape.
Right.
So if I execute this here you will be able to see 300 dimensions.
Right.
So with respect to this you use any kind of word you will be able to get some kind of vectors.
Now let me give you some of the example okay.
Suppose I make over here.
And uh for this particular vector what all things I use, I use this w v variable which is a word two
vec object.
Right.
All I have to do, I have to give this and give any word of your choice.
Let's say I want to give cricket.
Right?
So if I give probably cricket, you'll be able to see that the vector is automatically generated.
So here is again the vector which you are basically getting with respect to this particular shape.
That is 300 dimension.
Now this w v this w v variable right.
Which is the word vector.
It also has some of the functions you can actually use something called as most underscore similar.
Now let's say that I have given this most underscore similar.
I'm saying that from uh, if I'm giving this cricket word, right, which is the most similar word present
in this entire corpus.
Right.
And I'm actually going to use the same word, two vec object, and I'm going to find out all the words.
So if I execute this here you'll be able to see the most similar with respect to cricket.
You know, it will first of all try to convert into a vector.
And then it will probably check whether that vector is being able to see any similar kind of words or
not.
So these are all the words that is similar to cricket in that specific corpus.
So you have cricketing.
It is having like 0.83 similarity point 83% similarity.
Then cricket is 81% similarity.
Then you have test cricket.
Here you can see 80% similarity 20 cricket right?
You have 80% similarity.
Then you have cricket, then you have cricketer.
All these things are, you know, they are showing some kind of similarities with respect to this Google
News feed right now.
Similarly, if I really want to find out which is the most similar for word with respect to happy,
you'll also be able to see I'll be having words like glad, pleased, ecstatic, overjoyed, thrilled,
satisfied, proud, delighted, and understand that these all are similar words when I compare to happy,
right?
So it is also being able to say that, okay, these are the similar words when compared to the happy
word.
And they are also being able to show with respect to how much distance.
And I've already shown you how this distance is basically calculated.
Right.
So we have a concept of cosine similarity and all.
Similarly I can also provide two two words.
And I can basically say that okay how much this two word are basically similar.
So if I probably execute this here you'll be able to see w v dot similarity I'm getting.
Hockey and sports are somewhere under 53% similar right now.
This is very interesting okay I'm going to take the vector of King.
And I'm going to subtract with the vector of man.
And I'm going to add the vector of omen.
Now let's see what will be the kind of output vectors that I'll be getting.
Okay.
So once I do this in short what I'm doing I'm subtracting king minus man plus woman.
Obviously the answer should be queen, but I really want to prove you through the vectors itself.
Whether we are able to get the queen or not.
So here I'm going to execute this.
I'll got I got my vector.
So this is my entire vector.
And again this vector is of 300 dimensions okay.
So this is my entire vector.
Now what I'm going to do I'm going to use w v dot most underscore similar.
And I'm just going to pass this entire vector over here.
So once I execute this here you'll be able to see that Sea King is the first.
Obviously, uh, King should be the most similar vector.
But here I'm getting queen monarch.
Princess.
Crown prince.
Prince Prince.
Sorry.
King.
Sultan Queen resort.
But here you can see that the most similar word after king is queen.
So the kind of vector we are getting after doing all this particular subtraction matches matches more
towards the vector with respect to queen.
Right?
And this is what we are able to get with the help of word two vec.
Try to use this guys.
This is an amazing model.
Altogether you'll be able to see uh, that you can actually use this model itself.
You know you can actually use word two vec, Google News 300, which will be able to solve many problem
of yours.
And this is just a brief idea about a pre-trained model.
You know how a word two vec pre-trained model looks like.
You can also take your own text and train it from scratch.
But again, there is a different process for all together.
And this entire thing I've executed in Google Colab because the model size is quite huge.
Okay.
Uh, so in the upcoming videos, what I'm actually going to do, I'm also going to show you what two
vec technique by using Gensim where you can train the model from scratch.
Okay.
So yes, uh, this was it for, for this particular video.
I will see you all in the next video.
Thank you.