Hello guys.
So we are going to continue our discussion with respect to our end to end deep learning project using
simple RNN.
So already in my previous video, I have already shown you how to actually use embedding layer.
Now let us go ahead step by step and see like what kind of deep learning project we are implementing
with the help of simple RNN.
And as I have already told you, I'm going to use the IMDb data set and we are just going to do the
text classification using simple RNN.
Um, so let's go ahead.
And first of all, I will quickly use the same environment.
You will be able to see that over here.
I have my simple RNN folder with an end classification.
What requirement dot txt was there?
What virtual environment was there?
We are going to use that same thing.
Okay, so here is my end classification folder.
Here is my simple RNN.
And similarly I will keep on creating all the other projects as we go ahead like LSTM, RNN or uh,
GRU or any other further, uh, variants of RNN.
We are going to see everything will be creating folder wise.
Okay.
So first of all I will go ahead and select my kernel okay.
Here I'm going to import numpy as NP.
Okay.
Uh NP let's execute this and see it's working fine.
Then along with this we are also going to import TensorFlow as TF here.
Also we are going to use uh TensorFlow and Keras.
Then uh, as you all know this IMDb data set is available inside the TensorFlow.
Uh, for that we can actually load that particular data set directly from there.
So let's go ahead and import that also.
So if I go ahead and right from TensorFlow dot Keras dot data sets I'm going to import IMDb okay.
Then I'm going to basically write from TensorFlow dot Keras dot pre-processing as I said that uh I can
also use this pre-processing okay.
Pre-processing.
Import sequence okay.
So I'm going to import the sequence I'll talk about it.
Why we will be using the sequence and all.
But you can just imagine that this is a kind of a pre-processing technique that we really need to use.
Then from Keras sorry from TensorFlow dot Keras dot models I'm going to specifically import sequential.
Sequential is must for any type of neural network that we are going to use.
Then from TensorFlow dot Keras dot layers I'm going to import embedding.
Along with the embedding I'm going to use simple RNN.
Okay.
Uh and then we'll also use another layer which is called as dense.
Now, usually with the help of an, we usually use this dense layer right where we create the hidden
layers, where we create the hidden nodes with the help of simple RNN will be creating the, uh, RNN
nodes.
Right.
And this embedding is basically for your embedding layer.
So let's go ahead and import this.
So this has got successfully imported.
Uh now I'm just going to go ahead and import my IMDb, which I've actually done it over here.
Now let's load the IMDb data set.
So here you'll be able to see I will be loading the IMDb data set.
Um, so first of all, I will just go ahead and use my max features.
This Max feature is just like your vocabulary size, right.
So here I'll go ahead.
And first of all write my vocabulary.
So I'm basically initializing my vocabulary size.
Perfect.
Then, uh, I will just go ahead and create my x underscore train comma y underscore train values okay.
I will talk about this particular data set in depth okay.
What kind of data set is given.
Everything I'll be talking about then along with this I will go ahead and write x underscore text comma
y underscore test.
And if we go ahead and write this I am db dot load data okay.
And I'll say uh based on how many number of words what is my vocabulary size.
Right.
So that is nothing.
My max features.
So once I give this I will be able to print the shape of my training and test data.
Okay.
So here I will just go ahead and execute it.
So I'll say, hey, uh, go ahead and print the extreme X test shape just to make you understand how
many records we are actually playing up with, right?
So here you'll be able to see that, uh, it will be downloading this entire data set.
And the training data shape is 25,000 records.
Okay.
So this will down get downloaded in your local and it will be staying in their, uh, cache memory itself.
Then here you have your testing data, which is again 25,000 records.
Okay.
Similarly for this particular sentence you have this labels, right.
It can be positive or negative.
Now let's do one thing.
Let's go ahead and explore this kind of sample reviews.
What is there inside it and what kind of labels is specifically present okay.
So we will just go ahead and inspect our sample review and its label.
Okay.
So here I'm going to write sample review is equal to x x underscore train of zero okay.
So if I'm taking x underscore train of zero let's see first of all what is there in my X train and what
is there in my X test.
Okay.
So in X train you'll be able to see x train of zero.
In my first sentence I have this kind of words.
And you remember this kind of words.
When do we get it?
I have already shown you with respect to the embedding layer, right?
So if you go ahead and open this here also, you could see in every sentence I was getting this kind
of words right.
So in short this is nothing.
But it is a one hot representation of every word.
Right?
When I say one hot representation here you are able to see the index, right.
Uh, so this is one sentence, okay.
And as you know, my vocabulary size is 10,000.
So based on that you will be seeing different different index.
And each index represents one word okay.
So this is what you can actually get it okay.
So I'm going to take this as a sample review okay.
And let's see y of train of zero y underscore train of zero.
This will basically be 0 or 1 okay.
So here you can see it is one okay.
It can be zero or it can be one.
One basically means it says that it is a positive sentiment.
Zero basically means it says a negative sentiment.
Okay.
So I will also go ahead and take my sample underscore label okay.
And then I will go ahead and write y underscore train of zero.
Now you may be thinking Krish how did we get this direct vectors right.
How do we how do we get this indexes directly.
Uh this data set is put up in that way, but I will what I will do, I will decode this and I'll try
to show you what exactly is the real sentence also.
Okay.
Now let's go ahead and print this.
So I will go ahead and print.
Okay.
I'll say hey, uh, this is my sample review, uh, as integers.
Okay.
And here I'm going to specifically use this as my sample review.
Okay.
I'm just printing it.
Similarly, I will go ahead and print my sample label okay.
So once I print both of them you can see this is my sample review as integers.
This is my entire sentence.
And it is like very very big right.
So that many number of uh sentence length is quite big I guess.
Okay.
In this and this sample label is one.
Now let's do one thing okay.
Let's map mapping of word index back to words.
We need to do this okay.
We will try to convert this just for our understanding okay.
How do I do that.
Okay that we will try to see.
So here I'm going to basically use word underscore index is equal to I'll say hey go and use this IMDb
and I'll say, hey, get all the word index.
How many word index are there?
You just get it.
Okay.
So once I execute this I will be able to see my word index.
So this will basically be my word index for this particular word you'll be seeing.
This is the index for this word.
It is this index for nunnery.
This is this index on Giovani.
All these words I'm actually seeing the indexes okay.
Now what I will do now as you know this word index is an, uh, dictionary, right?
It is a it is obviously a dictionary.
What I will do, I will try to reverse.
See, I can also reverse this word index.
Okay.
I can basically write first of all index information.
And then I can go ahead and write this particular value okay.
So for this I will just go ahead and write this code.
Please focus on this.
And this is where dictionary comprehension will also come into picture.
So here what I'm saying for key comma value in word underscore index dot items okay.
And then I'm saying hey write like this value colon key for key value colon key right.
So value this is basically just getting reversed okay.
So now in order to see the reverse word index I can also go ahead and print it if you want.
So I will go ahead and print it.
And this also will be able to see it.
See uh let's see Where did it go?
Okay, uh, I will just try to print in this way.
Just a second.
Instead of using print, I will just use this.
Let me comment this out.
So this is how it is reversed okay.
Now what we are going to do is that we are going to probably from this particular, uh, keys, you
know, or from this particular keys.
I'm trying to get all my reviews.
Like, let's say I'll consider this particular sample review.
I will get all my words.
So one is over here.
So for one, what will be the word we'll try to see then I have 14 over here.
Then for 14 what is the word I'll try to see.
Okay.
So uh, when I check the documentation with respect to the TensorFlow this was the code that is used.
Okay.
So here I'm saying reverse underscore word underscore.
Uh get index okay.
Over here.
Uh it is saying dot get I minus three comma question mark for I in sample review.
See what it is doing.
First of all I'm going through each and every words in that sample review.
Then it is taking this particular I right.
So here you can see for I in sample underscore review.
So first index I got whatever index.
Then over here I'm writing I minus three okay I minus three.
We are trying to get that specific index value.
If we are not getting that index value, it will be just replaced by question mark.
Okay.
Now if I go ahead and see this decoded review you'll be able to see this.
See initially I got question mark.
It was one one minus three is minus two.
So when we are trying to search for minus two index I'm not getting any value.
So it is replaced by question mark.
Then it shows this frame was just brilliant.
So and so so all the information is over here.
This frame was just brilliant.
Casting, locations, scenery, story direction.
Everyone really suited that part they played and you could just imagine being the robot and all this
information is specifically there.
So this is nothing, but this is my decoded review.
Right.
So initially this was my entire sample review and we had completely decoded it over here.
And this is just one sentence along with the text.
Okay.
So this just to give you an idea from where this exactly sentence is coming, but for our sake, uh,
this step is already done.
We are able to get this particular one hot representation index, so we'll directly go ahead and use
it.
Just for your understanding, I've actually given over here.
Okay.
Perfect.
Uh, so this was one of the basic information.
What we did is that we loaded the data, we did all the things and all.
Okay, now let me just go ahead and use this from text.
Uh, from TensorFlow.
If you remember, we had also imported something called a sequence, right in pre-processing.
Now why this sequence will be very much necessary because here we are going to do the padding sequence.
Padding sequence has been done by in embedding.
Also I showed you right how to do the padding sequence.
We can use this pad underscore sequences right.
We can use pre and post.
I will also show you one example with respect to the sequences.
Let's say the maximum length of my sentence.
I'm just going to take it as 500 okay.
That can be the maximum text probably that I can have in my sentences.
Now let's go ahead and write X underscore train is equal to sequence dot pad underscore sequence.
And here I'm going to basically write x underscore train comma max length is nothing but it is equal
to this max length.
So that's it's just like we are trying to apply pre padding right by using this pad sequence.
Similarly for x test I can go ahead and write something like this okay sequence dot this.
This will basically be my x text okay.
And let's say this is my X test okay.
And here also I can go ahead and write my max underscore length.
Okay.
Let's go ahead and display my X train now.
So x train uh max underscore length.
Uh max okay here my feature is max underscore length.
Okay.
Uh, again I'm getting an error.
Uh max underscore length.
Okay.
This should be okay.
I made one mistake.
This parameter is max length.
Okay, so once it has got executed here, you can see by default it is done that pre padding okay.
Pre padding before zeros are basically coming up.
So if I go ahead and see x train of zero x underscore train of zero, here is what I'm actually able
to get.
See initially all I'm getting are zeros.
And then I am specifically getting all these values at the end.
So pre padding technique is basically applied.
So this step is also done okay.
Now in the next step we will go ahead and design our simple RNN.
We will train our simple RNN.
And this is what we are basically going to see in the next video.
Till here we have kept all the transformation technique that is required, and we are ready to probably
use this entire inputs for training our simple RNN.
So yes, I will see you all in the next video.
Thank you.