Gated Recurrent Unit (GRU)
Have you ever wondered how RNNs remember important information while generating a sentence word by word?
For example in the sentence:
“Dogs love to play with different things, including balls, sticks and even other animals before they go to sleep.”
How does the RNN model remember that the subject, “Dogs” is in plural tense and so it correctly uses the plural words: “things”, “animals” and “they”?
That is possible through Gated Recurrent Unit (GRU)
RNN Unit
Below is a basic structure of an RNN:

For each timestep, t:
This runs into the problem of vanishing gradients.
Vanishing gradients
When training an RNN, we use a method called backpropagation to update the weights. In the formula above, the following are the weights:
We look at the prediction of each timestep (
Remember that in an RNN, the parameters are constant across timesteps. That is if you have a parameter:
Example:
Let’s say the actual sentence is:
"I am craving pizza and soda."
But the model predicts:
"I am singing opera and dancing."
Clearly, something went wrong in the prediction. So what do we do? We look at the mistake made at the last word ("dancing" instead of "soda") and use that error to go backward through the sentence to adjust the model's weights. This process is called backpropagation through time.
To adjust the weights, we compute what's called the gradient, which tells the model how much to change its weights to improve the prediction. But here's the problem:
As we move backwards in time (from the end of the sentence to the beginning), the gradient is multiplied at each step. And most of the numbers involved in these multiplications are less than 1 (like 0.5, 0.2, or even smaller).
Here’s a simple example:
If you multiply 0.5 by itself just twice, you get:
0.5 × 0.5=0.25
Do it again:
0.25×0.5=0.125
The more times you multiply, the smaller it gets and fast.
Now imagine you have a long sentence with 10 or 50 timesteps, the gradient will be multiplied by that small number 10 or 50 times, exponentially reducing the value. The gradient becomes so tiny that it barely makes any difference when updating the weights. This means the model learns very slowly, or not at all, especially from the earlier parts of the sentence.
This issue is called the vanishing gradient problem, and it’s one of the biggest challenges when training regular RNNs on long sequences.
GRU (Simplified)
Gated recurrent units are a type of RNN that solve the problem of vanishing gradients and also capture long-term dependencies.
They possess the following terms:
c → memory cell
It provides a bit of memory to store information such as the tense (present/past), number(singular/plural), context, etc. which will later be used to select appropriate words.
It is a candidate for replacing
It decides whether or not we update the memory cell. It's value is between 0 and 1 due to the sigmoid function, as seen in the formula below.
Equations
Long-term Dependencies
RNN Sampling Example
“The dog I saw was beautiful.”
Timestep 2
Starting from the left,
- Sample Input: "The"
- Given,
= 0 - GRU computes:
= 0, - this equation:
if= 0, then = 0 - RNN predicts a probability distribution of words from which a word will be sampled in the next timestep
Timestep 3
- Sample Input: "dog"
- GRU computes:
= 1, (the model identifies “dog” as a singular noun and considers this important for subject-verb agreement) - this equation:
if= 1, then = 1 (memory stores the information: "singular noun") - RNN predicts a probability distribution of words from which a word will be sampled in the next timestep
Timestep 4
- Sample Input: "I"
- GRU computes:
= 0, - this equation:
if= 0, then = 1 (memory still holds on to the info) - RNN predicts a probability distribution of words from which a word will be sampled in the next timestep
Timestep 5
- Sample Input: "saw"
- GRU computes:
= 0, - this equation:
if= 0, then = 1 (memory still holds on to the info) - RNN takes in
and uses it predict a probability distribution of singular words from which a word will be sampled in the next timestep. Within that probability distribution, "was" is present.
This continues until there is some new information the model thinks it needs to remember.
GRUs solving Vanishing Gradients
This equation:
- When we do backpropagation, we need to compute how much the current output depends on the previous outputs.
- In this formula, the new state
is a weighted sum of the previous state and the new candidate state . - The weight
controls how much of the old state is kept.
{If you don't get this after the first read, re-read it until you do.}
Because of this structure:
- The update equation is linear and additive, not deeply nested with nonlinearities like in standard RNNs. This makes it easier for gradients to flow backward through time without vanishing, because:
- There's a direct path via
- The derivative of a linear function remains stable, it's not squashed like with tanh or sigmoid.
- There's a direct path via
- If
is small, more of the past state is preserved, which means less change to the gradient. This helps it avoid shrinking too fast. - The additive nature of the update (instead of full multiplication) helps prevent the gradient from becoming too small (or "vanishing").
References
https://www.geeksforgeeks.org/vanishing-and-exploding-gradients-problems-in-deep-learning/
https://www.superdatascience.com/blogs/recurrent-neural-networks-rnn-the-vanishing-gradient-problem/