Saturday , December 5 2026
Introduction to Recurrent Neural Network(RNN)

Introduction to Recurrent Neural Network(RNN)

Updated: 28 November 2026Level: BeginnerReading time: about 8 minutesConcepts · NumPy · tested

A Recurrent Neural Network (RNN) is a neural network for data where order matters, such as sentences, speech or stock prices over time. It has a “memory” (the hidden state) that carries information from earlier steps to later ones.

What You Will Learn

  • Why ordinary networks struggle with sequences
  • How the hidden state works as memory
  • How an RNN processes a sequence step by step, in NumPy
  • How RNNs are trained (backpropagation through time)
  • LSTM and GRU, and where RNNs are used

Prerequisites

Introduction to ANN.

Why Do We Need RNNs?

A normal ANN treats every input independently. To predict the next word in “I grew up in France, so I speak fluent ___”, the network must remember “France” from several words earlier. An RNN loops its output back into itself, so each step knows what came before.

How an RNN Works

  1. Input sequence – at each time step t the RNN reads one item (a word, a day’s price).
  2. Hidden state – its memory. It is updated from the current input and the previous hidden state: hₜ = tanh(Wₓ·xₜ + Wₕ·hₜ₋₁ + b).
  3. Loop – the new hidden state is passed to the next step along with the next input.
  4. Output – at each step (or only at the end) the RNN can give a prediction.
  5. Training – the network is “unrolled” over the sequence and trained with Backpropagation Through Time (BPTT).
Note: The same weights Wₓ, Wₕ and b are used at every time step. That is why an RNN can handle sequences of any length.

Example: An RNN Step by Step

Example
import numpy as np

Wx = np.array([[0.5]])     # input -> hidden
Wh = np.array([[0.8]])     # hidden -> hidden (memory)
b  = np.array([0.0])
h = np.zeros(1)            # starting memory

for t, x in enumerate([1.0, 0.5, -1.0, 0.0]):
    h = np.tanh(Wx @ [x] + Wh @ h + b)
    print(f"t={t}  x={x:+.1f}  h={h[0]:.3f}")

Explanation: At t=3 the input is 0, yet the hidden state is not 0: it still carries memory of earlier inputs through Wh. In a real RNN these weights are learned, and the hidden state is a vector of many numbers.

Output
t=0  x=+1.0  h=0.462
t=1  x=+0.5  h=0.551
t=2  x=-1.0  h=-0.059
t=3  x=+0.0  h=-0.047

The Vanishing Gradient Problem, LSTM and GRU

When sequences are long, gradients shrink as they flow back through many steps, so a simple RNN forgets things from far back. Two improved cells solve this:

Cell Idea Keras layer
Simple RNN Basic loop; short memory SimpleRNN
LSTM Gates decide what to remember, forget and output; long memory LSTM
GRU A lighter LSTM with two gates; often similar accuracy, faster GRU

Where Are RNNs Used?

  • Language translation and text generation
  • Speech recognition
  • Time-series forecasting (stock prices, weather, sales)
  • Sentiment analysis of reviews
  • Music generation
Note: For very long text, Transformers (the architecture behind modern large language models) have largely replaced RNNs, but RNNs and LSTMs are still widely used for time series and smaller tasks, and they are the best way to learn sequence modelling.

Series

Deep Learning series: Introduction to Deep Learning · AI vs ML vs DL · Introduction to ANN · ANN – Plan of Attack · ANN Practical · Introduction to CNN · CNN Steps · CNN Practically · Introduction to RNN · RNN Steps · RNN Practically

Interview Questions

Q: What is the hidden state?
A vector that stores information about the previous elements in the sequence; it is the RNN’s memory.

Q: What is BPTT?
Backpropagation Through Time: unrolling the RNN over all time steps and applying backpropagation.

Q: Why use LSTM instead of a simple RNN?
LSTM’s gates reduce the vanishing-gradient problem, so it can remember long-range dependencies.

Practice Exercises

  1. Set Wh to 0 in the example. What happens to the memory?
  2. Run the example with the sequence [1, 1, 1, 1, 1] and observe the hidden state.
  3. Name two problems where order matters and two where it does not.

Summary

An RNN processes a sequence one step at a time, keeping a hidden state that remembers earlier inputs. LSTM and GRU cells extend this memory to long sequences.

Next: RNN – Steps · RNN Practically

About Machine Learning

Check Also

LSTM and GRU Explained (NumPy LSTM Cell + Keras Example)

LSTM and GRU explained: vanishing gradients, cell state and gates, an LSTM step in NumPy, GRU comparison, parameter counts and a Keras sentiment model.