Hello guys.
So we are going to continue the NLP with Deep Learning series.
Uh, already till now we have discussed about simple RNN right?
And we saw the practical implementation already.
Right.
And we saw complete little deployment.
So this entire implementation with deployment we have also seen inside the simple RNN, we also discussed
about something called as embedding layer which was specifically used to convert your text into vectors.
Right.
Then, uh, we went ahead and understood about LSTM, uh, GRU variants of RNN.
Right.
And along with that other variants also we saw and I have also completed the practical implementation
for this practical implementation For this right, we understood what were the problems in simple RNN.
And then with respect to that, we also understood about LSTM and GRU variants.
Now coming to the third one, which is uh, today what I'm actually going to discuss about is something
called as bidirectional RNN.
Okay.
It can be RNN.
It can be a LSTM RNN okay, I'll right RNN or LSTM RNN or gr rnn, but it will be bidirectional.
Okay.
Now first of all, I will just try to make you understand why bidirectional RNN or bidirectional LSTM
RNN.
Okay.
And then we will probably go ahead and discuss about the architecture.
And uh we will solve this okay.
Now first of all, uh, let's go ahead and talk about our simple RNN right in our simple RNN, what
we used to do like, uh, or simple RNN or LSTM, RNN, what we used to do.
So here was my input here.
I used to get my output and here was my hidden state.
And with respect to that, uh, if I unfold this with respect to time, right.
If I just unfold it so it would, it would look something like this.
Right.
right.
So this will be my with respect to t is equal to two and I have t is equal to three.
Then I have t is equal to four.
Like this I may have a lot of um you know, the same hidden neurons, hidden states.
Right.
So over here you'll be able to see I will pass at t is equal to one.
I may go ahead and pass X11.
Then I may pass X12.
Then similarly I may pass X13.
And finally I may pass S1N right.
This is with respect to the first sentence.
Right.
And you know that here also, uh, for every neuron right.
We will also be getting an output.
And finally whatever output we are specifically getting over here.
Uh, let's say if I just want to predict one kind of output, if I just take an example with respect
to sentiment analysis.
So this will be my final output Y hat.
And before I get the y hat here, I'm going to just apply a sigmoid activation function right here.
I may go ahead and apply my sigmoid activation function.
So let's go ahead and apply this right.
So this will basically be my sigmoid if it if I just have a binary classification I used to get y hat.
And then we used to basically go ahead and compute our loss.
And based on our optimization we used to reduce this loss okay.
And uh by default some initial weights.
Also we used to transfer and here with respect to the weights what all weights we used, one was our
input weight, one was our hidden weight.
And this is also my hidden weight.
This is also my hidden weight.
And here your you will also be able to see that I will get this as my output.
And I can just consider drawing it over here.
Right.
So let's say I hope everybody remembers this architecture right.
So I may take this as my w o output and then pass to my sigmoid and get my y hat.
Okay.
And then what we should do is to basically go ahead and compute our loss.
Okay.
Now, uh, in this scenario.
Right.
And when we just consider this is basically my hidden state.
So in the input also I will be giving some kind of weights.
And this will also be my hidden weights.
Right.
So three types of weights.
Uh we were specifically using input weights hidden weights and w o for output weights.
Right.
And with respect to this I had my output something like 010203 and oh four.
Right.
Whatever outputs we are basically creating.
Now let's take one specific example.
And before that, I just want to talk about various problem statements that you can solve with RNN.
Okay.
Like there is one kind of RNN which is called as one to many RNN.
Okay, one to many RNN.
Uh, I will just first of all categorize them.
Okay.
Then I have something called as many to one RNN.
Okay.
And the third one is like many to many RNN.
Okay.
Many to many RNN.
Okay.
And the fourth one is something like 1 to 1 RNN.
So these are the different types of RNN.
We specifically use okay one to many RNN, many to one RNN, many to many RNN and 1 to 1 RNN.
Now, in order to explain you some of the examples, I'll first of all go and show you these are the
variants or these are the types of RNN that I can say with respect to problem statement.
Okay.
And we will go ahead and discuss some of the types.
And then we'll go back again to our LSTM and we'll try to discuss sorry.
We'll go towards this bidirectional RNN and discuss more about it okay.
So first of all let me just quickly go ahead and give some of the examples of one to many and many to
one RNN and all with some real world examples itself.
So guys.
So here is one of the diagram that I am specifically using to explain you about 1 to 1, one to many,
many to one and many to many.
And there is also one more variant of many to many.
Okay.
So, uh, we will just go ahead and discuss about this, you know, and uh, let's take one example
for at a time, okay?
Now, with respect to this particular RNN, you should be able to see that there is one input and one
output, okay, one input and one output.
So we will discuss one example for this okay.
So here I'm giving one input.
And I am getting one output with respect to this particular RNN.
Okay.
Uh if I probably go ahead and see one to too many.
Okay.
One too many.
So here in this one, too many, you'll be able to see that I'm giving just one input.
And based on this one output, okay, it will basically give you several outputs okay.
So here your output are many okay.
Now in order to talk about an example over here okay.
One input and many outputs okay.
So that is the reason we say it as one to many.
Okay.
Let's say I give an example in Google like if you probably there is there is a concept of something
called as image captioning.
What does image captioning mean is that whenever I specifically give one image okay I when I specifically
give one image image captioning, when I give one image, the model should be able to tell or give a
text information about that particular image.
Let's say if I am giving a image which has a dog and a cat, right?
So here let's say there is 111 dog and there is one cat, right.
So this model should basically be say after giving this particular image, there is a dog and cat in
the image.
Right.
There is a dog and cat.
So this is basically called as image captioning.
In this we are just giving one input.
But we are getting many outputs in form of words.
Okay.
Now similarly if I go ahead and see many to one okay in many to one, you will be able to see that over
here I have multiple inputs and I have one output okay.
Now this also example if I probably go ahead and in the Google search Google image search I search for
a input.
Let's say a cat eating food a cat eating food.
So if you go ahead and search this in Google search image, you'll be able to get an images of dog eating
sorry of cat eating food right?
You'll be able to get this specific image.
Now in this case you are able to generate one output with respect to multiple inputs, right?
So you have your inputs are multiple, your inputs are many, and your output is one, right?
Similarly here you have many to many.
So this two examples that you can specifically consider is like your language translation.
So in language translation, if I want to convert one language to the other I may give one many inputs.
Right.
And similarly I will be able to get many outputs right based on the sentence that we are actually working
with.
Now these are some of the types of RNN that everybody should probably know about.
Okay.
And the reason why I'm talking about this particular RNN, let's say if I have a use case, which is
like many to many.
Now let's go back to this particular use case.
Okay.
I'm just going to probably take this entire diagram.
Okay.
I will copy this again.
Go back and explain it over here.
Now in this particular case, if I am considering all the outputs right.
If I'm considering all the outputs here, let's say this is my Y one.
This is my Y two.
This is my y three.
This is my y four.
With respect to all the inputs, if I'm considering all the outputs, if I require all the outputs,
then I will specifically use many to many.
And if I'm considering all the outputs here, I can basically go ahead and use sigmoid or softmax,
any types of activation function like how I have actually applied over here.
Okay.
So why I am teaching you this?
Let's take a specific example.
Okay.
So I will take a text example.
Now that is where your bidirectional LSTM, RNN or bidirectional RNN will come.
Okay so my name is Krish.
I'll say hey uh Krish okay I will, I'll just write a sentence.
Likes to eat one word.
dash in Bangalore.
So I'm giving this specific example.
Okay.
Krish likes to eat.
Okay.
One word.
I need to predict this word in Bangalore.
Okay.
So let's say at x t is equal to one I give this word at t is equal to two.
I give this word t is equal to three.
I give this word t is equal to four.
I give this word.
Okay.
Let's consider that I'm giving this okay.
So I'll just say instead of writing each two I will just go ahead and write eats okay.
Krish eats dash word okay.
So first word goes over here.
Second word goes over here.
And I need to basically do the prediction for the third word okay.
Prediction for the third word.
And that is what I really need to predict it over here.
Now in this scenario, please note okay, if I want to probably calculate this particular word, if
I use or if I need to predict this word if I use simple RNN or LSTM, RNN or GRU and write in all these
three cases, my this prediction will be dependent on this two words right on the previous context,
right?
But here you will be able to see this specific example.
If I really need to do the prediction for this particular word, I also need the context of the upcoming
words.
Because if I write Krish eats dash in Bangalore.
So in Bangalore what is famous?
I will eat that right?
Let's say there is also one more sentence.
I will go ahead and write over here.
Krish, eats something in Paris.
So if I take this word now, you know that if my first real training data says that I eat dosa.
Okay, if you don't know about dosa, I think you can go and search in the Google Krish eat dosa in
Bangalore.
Because dosa is very famous right now.
Obviously in my training data I will have dosa, right?
And over here you'll be seeing that in my training data.
I may have something like this.
I may want to eat pizza in Paris because pizza may be famous in Paris.
Right.
So here you will be able to see that this word, if we are trying to predict it, does not only depends
on the context of the previous word, but it also depends on the context of the forward word.
Because when the country changes or when the city changes, you will be able to see that this specific
word may also change.
So in this scenario, if I really want to predict a word in between here, I need to also have the context
of the upcoming word.
But in this scenario I don't have that if I use simple WRN.
WRN.
I am not able to do that.
I'm not able to get it.
So how do we fix this particular problem?
So fixing this specific problem is what bidirectional RNN is actually doing.
Now in the case of bidirectional RNN what will specifically happen.
We will discuss.
So let's go ahead and take this again okay.
I will just go ahead and rub this for now okay.
So here you will be able to see this.
I don't know what has happened, but let's go ahead and draw this again okay I will go ahead and draw
this.
So first of all I will go ahead and draw a simple RNN.
And then I'll try to convert that into a bidirectional RNN.
And how with the help of further context we will probably predict the words okay, so let's say this
is my LSTM or this is my RNN with respect to unfolding with respect to time.
Okay.
And here you have this.
Okay.
Now in this I pass my input with respect to this.
I get my this here is my input.
Here is my input.
And you know that since we are trying to solve a many to many problem, it can be many to one.
It can be any kind of problems Okay.
So here what we are basically going to do here is my output okay.
Now now you know that if I have a sentence like this right.
Krish eats dash in Bangalore.
Right.
I need to predict this particular word.
In this case I will give my X11X12X13.
Like this.
Up till the X1N, whichever is the last word right here.
You know that what we will be having here, we'll be having our input weights here.
Hidden weights here, input weights here.
Hidden weights here.
Input weights here.
Hidden weights okay.
Similarly here uh input weights.
And here finally I'll get my output weight okay.
Okay.
And with respect to this output weight, it will pass to the sigmoid activation function.
Whatever activation function.
Now in this case what we do we pass one by word 111 word at every time.
Step at t is equal to one, t is equal to two, t is equal to three, t is equal to four.
So this is my RNN a simple RNN right.
Or if I take a LSTM RNN this will probably be changing inside that.
But the flow will be almost like this, right?
Now what does bidirectional LSTM says?
Hey, if I want.
So let's say this is Y1Y2Y3Y4.
Okay.
It says that hey if y three wants the context of the upcoming words, it is better that you use one
or RNN and make sure that instead of passing the words from front, you pass all the words from back.
Okay, so what it is saying, hey, go ahead and try to create another RNN.
So here I will go ahead and create another RNN which will look something like this.
So let's say this is my RNN.
This is my RNN.
This is my another RNN.
This is my another RNA.
Okay, now I will consider all this RNN.
Then what I will do along with this inputs I will also pass this input here.
Okay.
And here I am going to get my output.
It will get combined to this particular output.
Along with this you will also be seeing that I will pass this information one by one.
I will pass this information.
See, this needs to be also getting connected, right.
Without without the other uh, other uh other RNN.
If it is not connected, then it will be of no use, right?
So what we will do, we will try to connect this RNN with this, this RNN with this.
Then we'll connect this RNN with this, like how it is connected over here with respect to the hidden
state okay.
And whatever RNN is basically coming out as an output.
This will get combined to this Y3 okay.
So this will get combined to y3.
Let me just complete this drawing.
Then you'll be able to understand more and why we are specifically doing this.
And you should know that all these words that I'm actually passing, right.
All these words, this should also get connected to this.
It should also get passed to this.
So here are all my words.
Right?
Right.
So this is the entire architecture of a bidirectional, uh, RNN, or it can be a LSTM RNN.
If I just change this hidden layer from RNN into LSTM, RNN becomes bidirectional LSTM rnn okay, now
see see focus on this entire architecture.
First of all, I will use this white colored RNN or LSTM RNN in the forward propagation by passing this
word.
So this will be my X11.
This will be my X12.
This will be my X13X14X15 okay so I will be passing every word over here one by one right in this direction.
In the other RNN I will be passing from backward direction every word.
So here you can see my last word at t is equal to one will pass get passed over here right from in the
backward direction.
Then my next word will get passed over here.
Right then my next word will get passed over here.
So with respect to timestamp in the reverse direction, if I go from this direction, this will be t
is equal to one.
This will be.
This will be t is equal to two.
This will be t is equal to three.
This will be t is equal to four.
And I am just reversing the sentence.
So first Bangalore will get passed over here.
Then in will get passed over here.
Then we will also go ahead and calculate the y hat.
Right.
And this will basically get concatenated.
This will get combined right.
So I hope you are able to understand this entire flow.
Now what is the use of this.
Let's say if I want to do the prediction for y three right.
Let's say I want to do this prediction of this blank crush eats dash okay.
Some blank statement.
So here you can see that.
Now to predict this I will be having the context of x one to I will be having the context of X11.
Along with this, you'll also be seeing that I will also be having the context of this particular words,
because my this RNN, when it is running from the reverse direction, that entire context of X14 and
X13 will be available.
And when it says okay, cricket and Bangalore is also coming from this particular direction with respect
to the vectors, it will definitely be able to understand that, okay, the food that is probably famous
in Bangalore that will only get predicted.
Okay, so this is the entire funda behind bidirectional RNN, right?
I can go ahead and create my hidden layers over here.
Right?
It is very simple.
What we are doing.
We are combining things.
We are just combining.
Whatever output we are basically getting.
If this is my output, I may apply a sigmoid activation function over here or this is my output.
I may apply sigmoid activation function, but here I am getting the context of the previous word.
And I'm also getting the context of the upcoming words.
And based on that, I am able to do the prediction.
So if I know Bangalore, if I know Krish, if I know Paris, if I know Krish, then this RNN will be
able to perform well when compared to the simple RNN and LSTM RNN.
And that is the biggest difference with respect to the simple RNN, LSTM, RNN.
Now one assignment that uh, I definitely want to give it to you.
Okay I know it is little bit difficult assignment I'll give you you need to come up.
Just keep on exploring.
See the research paper okay?
You need to come up with forward propagation.
Forward propagation equations.
Like how we wrote the equation for LSTM and simple RNN.
And similarly what will be the basic equations for this forward propagation I need to know this first
of all okay.
So here you will be able to see I am combining this with this.
Right.
So this will also be there will be some weights right which I'm actually passing.
This will be my X11 I'll be passing to this okay.
It will not be y of I over here w of I.
But this will get connected.
So here I am passing this input.
Also I'm passing this input also along with this this input I will be passing over here.
Right.
This is the same words input I'm actually giving.
That basically means that the same word which I'm passing over here, that word will also go over here.
Right.
So just see what will basically happen with respect to every output.
What will happen if I'm getting 01020304.
Because this is also getting combined to this.
Right?
So just come up with the forward propagation equation.
See research papers come with different different ways and try to do this okay.
This will basically be an assignment to understand.
But this is how your entire bidirectional RNN actually works.
Okay.
So I hope you're able to understand, uh, I tried to simplify this as much as I could, but again,
the main aim is over here that we need to provide the forward context to the output for doing the prediction.
It will actually help us to do the proper prediction.
So yes, this was it for my side.
I will see you all in the next video.
Have a great day.
Thank you.
Take care.