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
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
- Input sequence – at each time step t the RNN reads one item (a word, a day’s price).
- Hidden state – its memory. It is updated from the current input and the previous hidden state:
hₜ = tanh(Wₓ·xₜ + Wₕ·hₜ₋₁ + b). - Loop – the new hidden state is passed to the next step along with the next input.
- Output – at each step (or only at the end) the RNN can give a prediction.
- Training – the network is “unrolled” over the sequence and trained with Backpropagation Through Time (BPTT).
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
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.
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
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
- Set
Whto 0 in the example. What happens to the memory? - Run the example with the sequence [1, 1, 1, 1, 1] and observe the hidden state.
- 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
Machine Learning Tutorials, Courses and Certifications