DEEP LEARNING LESSON 11 RECURRENT NEURAL NETWORKS

Why RNNs Are Used for Sequences

RNNs are useful for sequence data because they process data in order and carry information from previous steps to the next step.

What Is Sequence Data?

Sequence data is data where the order of the data matters.

Each item can be related to the items that came before it or after it.

For example:

Monday    → 100
Tuesday   → 105
Wednesday → 110
Thursday  → 115

Here, the order matters.

If we randomly rearrange the values, we lose part of the meaning of the sequence.

110 → 100 → 115 → 105

This is still the same numbers, but it is no longer the original sequence.

Examples of Sequence Data

Sequence data appears in many real-world problems.

Text
"I love machine learning"

Time Series
100 → 105 → 110 → 115

Speech
Sound₁ → Sound₂ → Sound₃ → Sound₄

Weather
Monday → Tuesday → Wednesday → Thursday

Stock Prices
Price₁ → Price₂ → Price₃ → Price₄

In all these examples, the order of the information matters.

Why Not Use a Normal Neural Network?

A basic feed-forward neural network normally processes an input without maintaining a hidden state from the previous input.

Input
  ↓
Neural Network
  ↓
Output

Suppose we give it these words:

I
love
Python

A basic neural network does not naturally carry information from the word "I" into the processing of "love".

For sequence problems, that previous information can be important.

Example: Understanding a Sentence

Consider this sentence:

The movie was not good

The word "not" is important when understanding the word "good".

Compare:

The movie was good

The movie was not good

The word "good" appears in both sentences, but the meaning is different because of the previous word "not".

This is why sequence order and previous information can matter.

How Does an RNN Handle This?

An RNN processes the sequence one step at a time.

At each step, it receives:

1. Current Input
2. Previous Hidden State

The previous hidden state contains information carried from earlier steps.

Current Input
      +
Previous Hidden State
      ↓
     RNN
      ↓
New Hidden State

The new hidden state is then passed to the next step.

Step-by-Step Example

Consider:

I love Python

The RNN processes the sequence like this:

Step 1

Input: "I"
Previous State: None
       ↓
      RNN
       ↓
Hidden State 1


Step 2

Input: "love"
Previous State: Hidden State 1
       ↓
      RNN
       ↓
Hidden State 2


Step 3

Input: "Python"
Previous State: Hidden State 2
       ↓
      RNN
       ↓
Hidden State 3

Information from earlier words can therefore influence the processing of later words.

Example: Time Series

RNNs are also useful for numerical sequences.

Imagine the following daily sales:

Monday    → 100
Tuesday   → 120
Wednesday → 130
Thursday  → 150
Friday    → ?

We want to predict Friday's sales.

An RNN can process the previous values in order:

100
 ↓
RNN
 ↓
Hidden State

120
 +
Previous Hidden State
 ↓
RNN
 ↓
New Hidden State

130
 +
Previous Hidden State
 ↓
RNN
 ↓
New Hidden State

150
 +
Previous Hidden State
 ↓
RNN
 ↓
Prediction for Friday

The model can therefore use information from earlier observations when making the prediction.

Why Does Order Matter?

Consider these two sequences:

Sequence A:
10 → 20 → 30

Sequence B:
30 → 20 → 10

The numbers are exactly the same.

But the sequences represent different patterns.

Sequence A is increasing:

10 → 20 → 30
     ↑
Increasing

Sequence B is decreasing:

30 → 20 → 10
     ↓
Decreasing

An RNN is designed to process these values in their original order.

Normal Neural Network vs RNN

Normal Neural Network

Input 1 → Network → Output
Input 2 → Network → Output
Input 3 → Network → Output

Each input is processed independently.


RNN

Input 1 → RNN → Hidden State 1
                    ↓
Input 2 → RNN → Hidden State 2
                    ↓
Input 3 → RNN → Hidden State 3

Information flows from one step to the next.

This difference is the main reason RNNs are useful for sequence data.

Simple Python Example

We can create an RNN using TensorFlow and Keras:

import tensorflow as tf


model = tf.keras.Sequential([
    tf.keras.layers.SimpleRNN(16)
])


model.build(input_shape=(None, 5, 1))

model.summary()

The important part is:

tf.keras.layers.SimpleRNN(16)

This creates an RNN with 16 hidden units.

The input shape:

(None, 5, 1)

means:

None → Number of samples
5    → Number of time steps
1    → Number of features at each step

Understanding the Input Shape

Suppose our sequence is:

10 → 20 → 30 → 40 → 50

There are:

5 time steps
1 feature at each time step

So one sample can be represented as:

[
    [10],
    [20],
    [30],
    [40],
    [50]
]

The RNN reads:

10 → 20 → 30 → 40 → 50

in that order.

Where Are RNNs Used?

RNNs are designed for problems involving ordered data.

Text
↓
Process words in order


Time Series
↓
Process values over time


Speech
↓
Process audio steps in order


Sensor Data
↓
Process measurements over time

The common idea is always the same: the order of the data contains useful information.

The Main Reason RNNs Are Used

The simplest way to remember this lesson is:

Sequence Data
      ↓
Order Matters
      ↓
Previous Information Matters
      ↓
RNN Carries Hidden State
      ↓
Current Input + Previous State
      ↓
New Hidden State

So an RNN is useful when the current prediction or understanding can depend on what happened earlier in the sequence.

Two Simple Examples to Remember

Example 1 — Sentence

The food was not good

The word "not" changes the meaning of "good". Previous words can therefore matter.

Example 2 — Temperature

Monday    → 25°C
Tuesday   → 27°C
Wednesday → 29°C
Thursday  → 31°C
Friday    → ?

Earlier temperature values may contain useful information for predicting a later value.

Final Summary

Sequence Data
↓
Data where order matters

Examples
↓
Text
Time Series
Speech
Sensor Data

Why RNN?
↓
Previous information can matter

How?
↓
Current Input
+
Previous Hidden State
↓
RNN
↓
New Hidden State

Main Idea
↓
RNN processes sequence step by step
while carrying information forward.

In one sentence:

RNNs are used for sequence data because
they process inputs in order and carry
information from previous steps to later steps.
QUICK CHECK

Check Your Understanding

1. What is sequence data?
Data where the order of the items matters.

2. Give one example of sequence data.
Text, time-series values, speech, or sensor data.

3. Why can a previous step matter?
Because previous information can affect the meaning or prediction of the current step.

4. How does an RNN carry previous information?
Through its hidden state.

5. What does an RNN receive at each step?
The current input and the previous hidden state.