Hello guys.
Now we are going to create our simple RNN neural network.
And then we will go ahead with training this entire training data.
And then we'll be seeing that how we can also apply early stopping in this.
And uh yes there will be lot many things that we are going to do right now.
So let's go ahead.
And now, first of all, in order to create our simple RNN, uh, we will go ahead and create model
dot uh model equals to sequential model.
So this is the first thing that I will be creating, as you know, that in my sequential model, the
first thing that I'm actually going to add is something called as embedding layer.
So in order to add I'll just go ahead and write model dot add.
And here I'm going to call my embedding layer.
Inside this embedding layer I have to give my vocabulary size which is initialized in this max underscore
features.
And you know that uh, I also have to use some feature dimension for my feature representation in the
embedding layer.
Let's consider that I'm going to use 128 features, uh, you know, for, for, for, uh, our dimension.
Right.
Like how in the embedding we used some dimension over here.
Right?
We, we represented ten, but since we have a very big data set, I will just go ahead and represent
it to 128.
You can probably put different numbers.
Also, it is up to you.
Okay then finally I will also go ahead and give my input underscore length okay.
And this will be nothing but my max length okay.
Max length which I have already created.
This is nothing but the length of the sentence.
So this is nothing.
But it is our embedding layer.
And this is responsible for taking all the words, converting that particular word into a vector of
this dimensions.
Okay, that is mentioned over here.
Then we will go ahead and add a simple RNN.
So for that I will just go ahead and use simple RNN.
This is also present in the layer section.
And then inside the simple RNN let's say I'm going to use 128 neurons.
So this is the number that I have come up with.
You can select any other numbers.
And I've also shown you an an how you can go ahead and find out the optimal numbers.
Right.
And here I'm going to use the activation function like ReLU okay.
Finally we'll go ahead and add a dense layer because I need to have only one output.
And my activation function will be over here as sigmoid.
Okay, so these are all the things that is available over here.
Uh, then uh, we will go ahead and execute it.
Once we execute it, you will be able to see my model summary, see if I have a multi-class classification
in the output layer, that many number of nodes, how many number of outputs you specifically have or
want to have, and the activation function can be changed to softmax.
That's it.
Only that is the change that you really need to do.
Now if I go ahead and see the model summary.
So this is how the model summary looks like.
And these are the total number of parameters.
Okay.
Uh now quickly we will go ahead and create an instance of early stop stopping callback.
Right.
So let's go ahead and do this.
So first of all I need to import from TensorFlow dot Keras dot callbacks import early stopping okay.
Then I'm going to initialize this early stopping and I'm going to monitor.
So I will just go ahead and use this early stopping.
I'm going to monitor my validation loss.
So I'll go ahead and write Val underscore loss.
And here I'm going to use my patients let's say the patients I'm going to use it for five epochs.
And I'm going to say hey whenever you find the best uh best, uh, validation loss or, uh, best accuracy.
One, you can restore the weights from there.
So for that, I'll just go ahead and restore best weights to true.
So this is done and this I will initialize it to my variable which is called as early stopping.
So let's go ahead and execute this I'll go ahead and write early stopping.
And this is my values.
Okay.
Now finally train the model with early stopping.
Okay.
So here I'm just going to go ahead and write model dot fit.
And inside this let's go ahead and write my x underscore train y underscore train.
And then I'm going to use an epochs of ten.
And finally you'll be able to see that my batch size, the batch size that I am actually going to use
over here is 32, let's say.
And finally I have my validation underscore split.
Let's say I'll say use 20% of that in the validation underscore split.
And finally I have my callbacks.
And this callbacks I can assign it to my early stopping okay.
So whichever we have initialized and all these things we will keep it in history, uh, the model history
itself.
And then we will go ahead and execute it.
So here you can see some error.
You must compile.
Okay I forgot to compile things over here.
The reason is very simple.
Uh, okay.
After this model dot summary, I will just go ahead and compile it.
The reason of compiling is that because I need to assign what what is the optimizer function that I
need to use.
So I will just go ahead and write.
My optimizer is equal to Adam okay.
And my loss since loss over here is the loss that I'm actually going to use is for a binary classification.
So I will just go ahead and write binary underscore cross entropy okay.
And you can go ahead and see the Keras APIs if fit for multi-class classification this will basically
become sparse categorical cross entropy and metrics.
I'm just going to use my accuracy.
Done.
Now we'll go ahead and use my early stopping.
And finally let's go ahead and train it.
Now this is obviously going to take some amount of time if you do not have a powerful machine.
So I would suggest please go ahead and do it in Google Colab.
I'm doing this in my local because I definitely have a very powerful workstation.
I have, uh, Nvidia RTX GPUs.
Uh, so that is the reason I will be able to train it with, uh, with a good speed, because I have
over here 25,000 records.
Right.
So we will wait, uh, and we will wait till all the epochs gets completed.
And then once this training is done, we will save this specific model and we'll see whether the accuracy
is increasing or not.
So here you can see loss is decreasing.
Uh, accuracy is also increasing.
So I think after we complete this we'll be able to get a good, uh, accuracy.
And after that, uh, we will go ahead and, uh, create, uh, Streamlit web app, and then we will
try to see that how, uh, the prediction is basically done, you know, in as an end to end project.
So right now epoch two is going on.
It will take some time.
So let's see.
Let's wait for some time till this happens.
Okay.
I'll see you in some time.