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:
Pasted image 20250513192949.png
For each timestep, t:

a<t>=g(Waaa<t−1>+WaxX<t>+ba)y<t>=g(Wyaa<t>+by)

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: Waa,Wax,Wya,ba,by

We look at the prediction of each timestep (y^<t>), compare it to the actual value (y<t>) and calculate the loss. Based on that loss, we go back in time to update the weights of all the parameters that were used to compute that output.

Remember that in an RNN, the parameters are constant across timesteps. That is if you have a parameter: Wa=5, this parameter is used in every single timestep. Timesteps do not have their own unique parameters, they all share the same 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.

C~<t> → candidate cell state
It is a candidate for replacing c<t>. That is, it is an option for replacing c<t>.

Γu → update gate
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
c<t>=a<t>
C~<t>=tanh(Wc[c<t−1>,xt]+bc)
Γu<t>=σ(Wu[c<t−1>,xt]+bu)
c<t>=Γu×C~<t>+(1−Γu)×c<t−1>

Long-term Dependencies

RNN Sampling Example
“The dog I saw was beautiful.”

Timestep 2
Starting from the left,

Timestep 3

Timestep 4

Timestep 5

This continues until there is some new information the model thinks it needs to remember.

GRUs solving Vanishing Gradients

This equation: c<t>=Γu×C~<t>+(1−Γu)×c<t−1> tells us how the GRU updates its hidden state (or "memory") over time.

{If you don't get this after the first read, re-read it until you do.}
Because of this structure:

  1. 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 c<t−1>
    • The derivative of a linear function remains stable, it's not squashed like with tanh or sigmoid.
x=2Linear Function: f(x)=xDerivation:ddx(x)=1No matter what the value of x is, the gradient is constantNonlinear(tanh) Function: f(x)=tanh(x)Derivation:ddx(tanh(x))=1−tanh2(x)x=2:1−tanh2(2)≈1−0.9642≈1−0.929≈0.071The gradient is 0.071 which is "squashed" and smaller than 1
  1. If Γu is small, more of the past state c<t−1> is preserved, which means less change to the gradient. This helps it avoid shrinking too fast.
  2. 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/