DEEP LEARNING LESSON 12 LSTM AND GRU

What Is GRU?

GRU stands for Gated Recurrent Unit. It is a type of recurrent neural network designed to remember useful information from earlier parts of a sequence and ignore information that is no longer useful.

What Is a GRU?

A GRU is an improved version of a basic RNN.

Like an RNN, a GRU processes sequence data one step at a time. But unlike a basic RNN, it has gates that help it control what information should be remembered and what information should be forgotten.

Basic RNN
    ↓
Processes sequence
    ↓
Has difficulty remembering
long-term information


GRU
    ↓
Processes sequence
    ↓
Uses gates
    ↓
Controls memory
    ↓
Can handle longer dependencies better

GRU is commonly used for sequence problems such as text, time-series data, speech, and other ordered data.

Simple Example

Imagine the GRU is reading this sentence:

"John lives in London.
He works as a developer.
He likes football."

When the GRU reads:

John lives in London.

it may learn that London is important information.

When it reads the next sentences, it can decide whether information from the past should continue to influence the current state.

Input 1
   ↓
GRU
   ↓
Hidden State
   ↓
Input 2
   ↓
GRU
   ↓
Updated Hidden State
   ↓
Input 3
   ↓
GRU
   ↓
Updated Hidden State

The hidden state acts as the GRU's memory.

Why Do We Need GRU?

Basic RNNs can struggle when important information is far away from the current input.

For example:

"Sarah was born in India.
...
many words...
...
many words...
...
Where was Sarah born?"

The useful information:

India

appeared much earlier.

A basic RNN may have difficulty carrying this information across many time steps.

GRU uses gates to control the flow of information and can therefore handle long-term dependencies better than a basic RNN.

GRU Uses Two Main Gates

The main difference between GRU and a basic RNN is the use of gates.

A GRU has two main gates:

1. Update Gate
2. Reset Gate

These gates control how information flows through the GRU.

                 GRU

              Current Input
                   │
                   ↓
          ┌─────────────────┐
          │                 │
          │   Update Gate   │
          │                 │
          └─────────────────┘
                   │
                   ↓
          Controls old/new
              information


          ┌─────────────────┐
          │                 │
          │    Reset Gate   │
          │                 │
          └─────────────────┘
                   │
                   ↓
          Controls previous
             information

Update Gate

The Update Gate decides how much of the previous information should be kept and how much new information should be used.

Think of it as asking:

"Should I keep my old memory,
or should I update it with new information?"

For example:

Previous information:
London

New information:
Paris

If the new information is important, the update gate can allow the GRU to replace more of the old information.

If the old information is still useful, the GRU can keep more of it.

Old Information
      │
      ├──────────────┐
      │              │
      ↓              ↓
   Keep it       Update it
      │              │
      └──────┬───────┘
             ↓
       New Hidden State

Reset Gate

The Reset Gate decides how much previous information should be considered when creating new information.

Think of it as asking:

"How much of the old information
should I use right now?"

Suppose a sequence changes to a completely new topic.

Old topic:
Weather

New topic:
Football

The reset gate can reduce the influence of the old information when processing the new topic.

Previous State
      ↓
Reset Gate
      ↓
Reduce unnecessary old information
      ↓
Process current input
      ↓
New candidate information

How Does GRU Remember Information?

A GRU does not have a separate cell state like an LSTM.

Instead, the GRU uses its hidden state to carry information through the sequence.

Input
  ↓
GRU
  ↓
Hidden State
  ↓
Next Input
  ↓
GRU
  ↓
Updated Hidden State
  ↓
Next Input

So the hidden state serves as the GRU's memory.

GRU vs Basic RNN

A basic RNN has a relatively simple structure.

Input
  ↓
RNN
  ↓
Hidden State
  ↓
Next Input

A GRU adds gates:

Input
  ↓
┌───────────────┐
│      GRU      │
│               │
│ Update Gate   │
│ Reset Gate    │
│               │
└───────────────┘
  ↓
Hidden State
  ↓
Next Input

The gates give the GRU more control over what information should flow through the network.

GRU vs LSTM

GRU and LSTM solve a similar problem: improving the ability of recurrent networks to handle longer-term dependencies.

LSTM
├── Forget Gate
├── Input Gate
├── Output Gate
├── Cell State
└── Hidden State


GRU
├── Update Gate
├── Reset Gate
└── Hidden State

The GRU is simpler because it does not maintain a separate cell state.

This often makes GRUs computationally lighter than LSTMs, although which one works better depends on the particular problem and dataset.

Easy Real-Life Analogy

Imagine you are taking notes while listening to a long conversation.

The GRU has two decisions to make.

Update Gate

"Should I replace my current notes
with this new information?"

And:

Reset Gate

"How much of my previous notes
should I consider right now?"

This is a useful mental model, but remember that the real GRU performs numerical operations on vectors.

Simple Numerical Example

Suppose a simplified GRU has:

Old hidden state = 10

Update gate = 0.2

A small update-gate value can be thought of as keeping more of the previous state.

Now suppose:

New candidate information = 20

A simplified interpolation might look like:

New State
=
(1 - 0.2) × Old State
+
0.2 × New Information

=
0.8 × 10
+
0.2 × 20

=
8 + 4

=
12

This is only an intuition-building example. Real GRUs perform this operation element-by-element on vectors and use learned parameters to calculate the gates and candidate state.

Complete GRU Flow

Previous Hidden State
          │
          ├──────────────────┐
          │                  │
          ↓                  ↓
     Reset Gate         Update Gate
          │                  │
          ↓                  ↓
Control old           Control how much
information            old/new information
for current step       should be used
          │                  │
          ↓                  │
Candidate State             │
          │                  │
          └────────┬─────────┘
                   ↓
            New Hidden State
                   │
                   ↓
              Next Time Step

Build a GRU With Python

In Keras, we can create a GRU using the GRU layer.

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import GRU, Dense

model = Sequential([
    GRU(32, input_shape=(10, 1)),
    Dense(1)
])

model.compile(
    optimizer="adam",
    loss="mse"
)

model.summary()

Here is what each important part means:

GRU(32)
↓
Creates a GRU layer
with 32 hidden units

input_shape=(10, 1)
↓
10 time steps
1 feature at each time step

Dense(1)
↓
Produces one output

The GRU layer automatically handles the update gate, reset gate, hidden state, and related calculations.

Example: Predicting the Next Word

Suppose a model sees:

"I love eating"

The GRU processes the sequence:

"I"
 ↓
GRU
 ↓
Hidden State

"love"
 ↓
GRU
 ↓
Updated Hidden State

"eating"
 ↓
GRU
 ↓
Updated Hidden State

The final hidden state contains information from the sequence that can be used to predict the next word.

Final Hidden State
        ↓
Dense Layer
        ↓
Prediction
        ↓
"pizza"

The actual prediction depends on the training data and vocabulary.

Important Point

GRU is not simply a "faster RNN."

Its important advantage is that the gates give it better control over information flow.

Basic RNN
    ↓
Simple memory mechanism


GRU
    ↓
Gated memory mechanism
    ↓
Better control over
long-term information

Final Summary

GRU
↓
Gated Recurrent Unit

It is a type of RNN
↓
Designed for sequence data

Main gates:
↓
Update Gate
Reset Gate

Memory:
↓
Hidden State

Update Gate:
↓
Controls how much old/new
information is used

Reset Gate:
↓
Controls how much previous
information is considered

Main advantage:
↓
Better handling of long-term
dependencies than a basic RNN

The simplest way to remember GRU is:

GRU = RNN + Gates

Update Gate
→ "How much should I update?"

Reset Gate
→ "How much old information
   should I use?"
QUICK CHECK

Check Your Understanding

1. What does GRU stand for?
Gated Recurrent Unit.

2. What problem does GRU help solve?
It helps recurrent networks handle information and dependencies across longer sequences.

3. What are the two main GRU gates?
Update Gate and Reset Gate.

4. Does GRU have a separate Cell State like LSTM?
No. GRU uses its hidden state to carry memory.

5. What does the Update Gate do?
It controls how much old information is kept versus how much new information is used.

6. What does the Reset Gate do?
It controls how much previous information is considered when processing the current input.