# Bidirectional RNN - Time Step Outputs

**URL:** <https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763>\
**Category:** Sequence Models\
**Tags:** week-module-1, coursera-platform\
**Created:** [June 9, 2024, 1:46am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763 "2024-06-09T01:46:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungng777](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hungng777](https://community.deeplearning.ai/u/hungng777)\
**Post date:** [June 9, 2024, 1:46am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/1 "2024-06-09T01:46:03Z")

</div>

Assume the input sentence is “I love mango juice” for the bidirectional RNN. At time step 1, the RNN in the forward direction receives “I” as the input while the RNN in the backward direction receives “juice” as the input. Is the output of time step 1 the combined output (e.g. concatenated output) of the forward and backward RNNs?

At time step 2, the RNN in the forward direction receives “Iove” as the input while the RNN in the backward direction receives “mango” as the input. Is the output of time step 2 also the combined output (e.g. concatenated output) of the forward and backward RNNs?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [June 9, 2024, 4:06am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/2 "2024-06-09T04:06:47Z")

</div>

Output at timestep 1 is the concat of “I” from both the forward and backward passes. See [this link](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional#args_1) to learn about the `merge_mode` parameter.

Hope this clears any doubts on why the entire sequence should be known for this bidirectional layer to be effective.

---

<div class="post-metadata">

**Author:** ![hungng777](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hungng777](https://community.deeplearning.ai/u/hungng777)\
**Post date:** [June 9, 2024, 4:40am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/3 "2024-06-09T04:40:41Z")

</div>

Why output at timestep 1 is the concat of “I” from both the forward and backward passes? I thought the backward RNN at time step 1 received “juice” as the input.

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [June 9, 2024, 6:32am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/4 "2024-06-09T06:32:08Z")

</div>

Bidirectional RNN aims to learn about a token from both directions:

1. Forward direction: starting from start of sentence and ending at current token
2. Backward direction: starting from the end of sentence ending at current token.

---

<div class="post-metadata">

**Author:** ![hungng777](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hungng777](https://community.deeplearning.ai/u/hungng777)\
**Post date:** [June 9, 2024, 6:38pm UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/5 "2024-06-09T18:38:59Z")

</div>

I don’t quite get your explanation. Could you explain in terms of what happen at time step 1, time step 2 and etc.?

Thanks.

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [June 10, 2024, 3:47am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/6 "2024-06-10T03:47:45Z")

</div>

Why don’t you go through these links and answer your question?

1. Read [Bi-Directional Recurrent Neural Network](https://medium.com/analytics-vidhya/bi-directional-rnn-basics-of-lstm-and-gru-e114aa4779bb) section of the medium article.
2. Keras [source code](https://github.com/keras-team/keras/blob/v3.3.3/keras/src/layers/rnn/bidirectional.py#L10)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [June 10, 2024, 10:50pm UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/8 "2024-06-10T22:50:49Z")

</div>

Other mentors have been following this conversation. I don’t personally know the answer right at the moment. The reason is that we never actually build a bidirectional network by hand, in the way of the “Step by Step” exercise in Week 1. When we get around to using them, we just use the TF/Keras Bidirectional module, which is a “black box” and our choices are to read either the documentation that Balaji already gave us or (gulp) the source code he also linked for us.

But my next step is to watch the lecture that Prof Ng gives specifically on this topic. That will be our best hope of getting to a better level of understanding. I watched it back in 2019 when I first took this course, but the memory is not current any more. In my written notes, it just shows the diagram, but doesn’t have enough detail. I’m assuming you’ve watched the lecture already, but it might be worth watching again. My personal life is quite busy in the next 6 to 8 hours and then it’ll be bedtime where I am. So it’s not likely I will have time to watch the lecture in the next 18 hours or so.

But there may well be other folks who see this and have more information. Maybe we get lucky and one or more of them will chime in before I get time to watch the lecture again.

---

<div class="post-metadata">

**Author:** ![Nevermnd](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nevermnd/32/126021_2.png) [@Nevermnd](https://community.deeplearning.ai/u/Nevermnd)\
**Post date:** [June 11, 2024, 2:55am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/9 "2024-06-11T02:55:05Z")

</div>

@hungng777 Ha. I don’t want to be too simple here and am maybe not the smartest one here to help.

But to go alll the way back to your original question the answer is simply: ‘Yes’.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/a/a/aa4eae19e3b97532446d589f8e28b61b67204cd6.jpeg)

Where I think you might be getting (understandably) confused:

Your data point (X) order _never_ changes, nor do your time steps (T)-- I.e. They are always sequential (X^{\<1\>}, X^{\<2\>}, etc and your timesteps T^{\<1\>}, T^{\<2\>}, etc)-- Yet in your reverse transversal, it is only _your network_ that runs in reverse order; I.e. You feed it X^{\<4\>}, X^{\<3\>}, etc.

Like in “I love mango juice”, it gets too confusing to think of ‘juice’ as now X^{\<1\>}. Don’t do that. It is still X^{\<4\>}, you’ve only changed your data order.

Hope that helps.

\*Also, _obviously_, you have to complete the entire back and forth path before you can concatenate.

---

<div class="post-metadata">

**Author:** ![hungng777](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hungng777](https://community.deeplearning.ai/u/hungng777)\
**Post date:** [June 11, 2024, 6:23am UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/10 "2024-06-11T06:23:18Z")

</div>

Thank you for the explanation. I just want to know more about bidirectional RNN and its variants (e.g. bidirectional LSTM) since they are the basic building blocks of more advanced constructs (e.g. attention model).

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [June 14, 2024, 9:50pm UTC](https://community.deeplearning.ai/t/bidirectional-rnn-time-step-outputs/643763/11 "2024-06-14T21:50:39Z")

</div>

I just watched this lecture again. Sorry it took me a few days. I think Anthony’s answer covers the fundamental question, but if you use this screen shot slightly later in the video:

 ![Screenshot 2024-06-14 at 2.40.55 PM](https://global.discourse-cdn.com/dlai/original/3X/5/e/5e46ee0ea5584e86a25c82615b5703ed43a101c5.jpeg)  
It gives a more complete picture of how the \hat{y}^{\<n\>} values are computed. You can see that the W\_y weight matrices are used to process both the forward and backward states. At each timestep, you have two separate “cell state” values: one for the forward direction and one for the reverse direction. These are separate from each other. What will be contained in those states is determined by whether you select a plain RNN architecture, a GRU or LSTM architecture. But with whichever architecture you have chosen, you do the “forward prop” in both time directions. Then you’d do back prop to learn the weight values based on the comparison of the \hat{y}^{\<n\>} values and the y^{\<n\>} at all the timesteps. Of course that will also drive updates to the other weight and bias values that are used to compute the updated cell states in both directions. Those weights will be separate between the directions, because the states are distinct.
