DEEP LEARNING LESSON 12 LSTM AND GRU

Build LSTM With Python

Now let's build a simple LSTM neural network using Python and TensorFlow/Keras. We will use a small sequence dataset to understand how an LSTM receives sequence data and learns to make predictions.

What Are We Building?

We will build a model that looks at the previous numbers in a sequence and predicts the next number.

For example:

Input:
[1, 2, 3]

Target:
4

Another example:

Input:
[2, 3, 4]

Target:
5

So the model needs to learn the pattern:

1, 2, 3 → 4
2, 3, 4 → 5
3, 4, 5 → 6
4, 5, 6 → 7

This is a very simple example, but it helps us understand how an LSTM processes sequence data.

Step 1 — Install TensorFlow

If TensorFlow is not installed, install it with:

pip install tensorflow

Then we can import the required classes:

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

Step 2 — Create the Training Data

Let's create a simple sequence:

data = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8],
    [9],
    [10]
], dtype=float)

We want the model to look at three values and predict the next value.

[1, 2, 3] → 4
[2, 3, 4] → 5
[3, 4, 5] → 6
[4, 5, 6] → 7
...

Step 3 — Create Input and Target Sequences

We can create the training sequences using:

X = []
y = []

sequence_length = 3

for i in range(len(data) - sequence_length):
    X.append(data[i:i + sequence_length])
    y.append(data[i + sequence_length])

X = np.array(X)
y = np.array(y)

print(X)
print(y)

The input sequences will look like:

X:

[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
...

And the targets will be:

y:

4
5
6
7
8
...

Step 4 — Understand the Input Shape

This is one of the most important parts when working with LSTM.

LSTM expects sequence data in this form:

(samples, time_steps, features)

For our example:

(7, 3, 1)

This means:

7
↓
Number of training sequences


3
↓
Number of time steps
in each sequence


1
↓
Number of features
at each time step

For example:

[1]
[2]
[3]

contains three time steps and one feature at each time step.

Step 5 — Create the LSTM Model

Now we create the neural network:

model = Sequential([
    LSTM(32, input_shape=(3, 1)),
    Dense(1)
])

There are two layers here.

LSTM(32)
    ↓
Learns patterns from the sequence

Dense(1)
    ↓
Produces one prediction

The number 32 means the LSTM has 32 hidden units.

It does not mean the model has 32 layers. It means the LSTM layer has 32 hidden units.

Step 6 — Compile the Model

Before training, we compile the model:

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

Here:

optimizer="adam"
↓
Controls how weights are updated


loss="mse"
↓
Measures prediction error

MSE means Mean Squared Error.

Step 7 — Train the LSTM

Now we train the model:

model.fit(
    X,
    y,
    epochs=200,
    verbose=0
)

The model repeatedly sees the training sequences.

Input
  ↓
LSTM
  ↓
Prediction
  ↓
Calculate Loss
  ↓
Backpropagation
  ↓
Update Weights
  ↓
Next Training Step

The process repeats for 200 epochs.

Step 8 — Make a Prediction

After training, we can give the model a new sequence:

test_sequence = np.array([
    [[8],
     [9],
     [10]]
], dtype=float)

Then ask the model to predict the next number:

prediction = model.predict(test_sequence)

print(prediction)

The expected answer is approximately:

11

The exact result will not necessarily be exactly 11, especially with such a tiny dataset.

Complete Python Code

Here is the complete example in one place:

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense


# 1. Create data
data = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8],
    [9],
    [10]
], dtype=float)


# 2. Create sequences
X = []
y = []

sequence_length = 3

for i in range(len(data) - sequence_length):
    X.append(data[i:i + sequence_length])
    y.append(data[i + sequence_length])

X = np.array(X)
y = np.array(y)


# 3. Create LSTM model
model = Sequential([
    LSTM(32, input_shape=(3, 1)),
    Dense(1)
])


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


# 5. Train model
model.fit(
    X,
    y,
    epochs=200,
    verbose=0
)


# 6. Test the model
test_sequence = np.array([
    [[8],
     [9],
     [10]]
], dtype=float)


# 7. Make prediction
prediction = model.predict(test_sequence)

print("Predicted value:", prediction[0][0])

Understand the Complete Flow

Raw Data
   ↓
Create Sequences
   ↓
X = Input Sequences
y = Target Values
   ↓
LSTM Layer
   ↓
Learn Sequence Patterns
   ↓
Dense Layer
   ↓
Prediction
   ↓
Calculate Loss
   ↓
Adam Optimizer
   ↓
Update Weights
   ↓
Repeat for Many Epochs
   ↓
Trained LSTM

Let's Understand One Example

Suppose the training example is:

X = [4, 5, 6]

y = 7

The LSTM receives the values one time step at a time:

Time Step 1
Input = 4
   ↓
LSTM


Time Step 2
Input = 5
   ↓
LSTM


Time Step 3
Input = 6
   ↓
LSTM


Final Hidden State
   ↓
Dense Layer
   ↓
Prediction

Suppose the model predicts:

Prediction = 6.5

But the correct answer is:

Target = 7

The loss function measures the difference.

Prediction
     ↓
   6.5

Target
     ↓
   7.0

     ↓

Calculate Loss

     ↓

Backpropagation

     ↓

Update Weights

After many training examples and epochs, the LSTM learns the relationship between the sequence and the target.

Where Is the LSTM Actually Used?

Our number example is intentionally simple. Real LSTM applications can involve much more meaningful sequence data.

Text
↓
LSTM
↓
Next word prediction


Stock / sensor data
↓
LSTM
↓
Future value prediction


Speech sequence
↓
LSTM
↓
Speech recognition


Time-series data
↓
LSTM
↓
Future prediction

The important idea is that the input has an order and the previous information can influence later predictions.

What Happens Inside LSTM(32)?

When you write:

LSTM(32)

Keras creates an LSTM layer with 32 hidden units.

Internally, the LSTM contains its gates:

Input
  ↓
┌──────────────────────┐
│        LSTM          │
│                      │
│  Forget Gate         │
│  Input Gate          │
│  Output Gate         │
│                      │
│  Cell State          │
│  Hidden State        │
└──────────────────────┘
  ↓
Output

You do not need to manually implement these gates when using Keras.

Keras performs those calculations for you.

Remember the Three-Dimensional Input

One of the most common beginner mistakes is providing the wrong input shape.

(samples, time_steps, features)

Our example:

(7, 3, 1)

Means:

7 samples
3 time steps
1 feature

For example:

Sample 1

Time 1 → [1]
Time 2 → [2]
Time 3 → [3]

Target → 4

And:

Sample 2

Time 1 → [2]
Time 2 → [3]
Time 3 → [4]

Target → 5

Our LSTM Architecture

Input
Shape:
(3, 1)
   │
   ↓
┌───────────────┐
│   LSTM(32)    │
│               │
│ 32 hidden     │
│ units         │
└───────────────┘
   │
   ↓
┌───────────────┐
│   Dense(1)    │
└───────────────┘
   │
   ↓
One Prediction

It is a small model intentionally. You do not need a huge network to understand how an LSTM works.

Common Beginner Mistakes

Mistake 1 — Confusing time steps with features.

(samples, time_steps, features)

(7, 3, 1)

7 = samples
3 = time steps
1 = features

Mistake 2 — Thinking LSTM automatically predicts correctly.

The model must be trained on enough useful data. Our sequence example is only for learning the architecture.

Mistake 3 — Thinking LSTM is only for text.

LSTM works with many types of sequential data, including time series, sensor readings, speech, and text.

Final Summary

1. Create sequence data
        ↓
2. Create X and y
        ↓
3. Build LSTM
        ↓
4. Compile model
        ↓
5. Train model
        ↓
6. Give new sequence
        ↓
7. Make prediction

The most important code is:

model = Sequential([
    LSTM(32, input_shape=(3, 1)),
    Dense(1)
])

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

model.fit(
    X,
    y,
    epochs=200
)

In plain English:

LSTM
→ Learn patterns from sequences

Dense
→ Produce the prediction

Loss
→ Measure how wrong the prediction is

Adam
→ Update the model's weights

Epochs
→ Repeat the learning process
QUICK CHECK

Check Your Understanding

1. What does an LSTM receive as input?
Sequence data represented as (samples, time_steps, features).

2. What does LSTM(32) mean?
It creates an LSTM layer with 32 hidden units.

3. Why do we use Dense(1)?
To produce one output value for our prediction.

4. Why do we use Adam?
Adam is the optimizer that updates the model's weights during training.

5. What does the LSTM learn?
It learns useful patterns and relationships in the sequence data from the training examples.

6. What does (7, 3, 1) mean?
7 training samples, 3 time steps per sample, and 1 feature at each time step.