Category: Machine Learning | Deep Learning | Data Science
- Introduction
Some data has a specific order, such as words in a sentence, daily temperatures or stock measurements over time.
In these situations, earlier information can help us understand later information.
A Recurrent Neural Network (RNN) is a type of deep learning model designed to process sequential data.
- What is an RNN?
A Recurrent Neural Network is an artificial neural network that processes a sequence one step at a time and maintains a hidden state.
This hidden state carries information from earlier steps and helps the model understand the current input using previous context.
Example: An RNN can process the words in a sentence one by one to understand its meaning.
- How Does an RNN Work?
An RNN processes information step by step.
At each time step:
The network receives the current input.
It uses the previous hidden state.
It calculates an updated hidden state.
It may produce an output.
The process continues for the next input.
The same network weights are reused across different time steps.
Training commonly uses a technique called Backpropagation Through Time (BPTT).
RNN Architecture
Input at Time 1 → Hidden State → Input at Time 2 → Updated Hidden State
Mathematical Formula
hₜ = f(Wₓxₜ + Wₕhₜ₋₁ + b)
Where:
hₜ: Current hidden state.
xₜ: Current input.
hₜ₋₁: Previous hidden state.
Wₓ: Input weight.
Wₕ: Recurrent weight.
b: Bias.
f: Activation function.
The current hidden state depends on both the current input and the previous hidden state.
- Types and Variants of RNN
A. Many-to-One
Used to classify an entire sequence into one output.
Example: Sentiment analysis of a customer review.
B. One-to-Many
Takes one input and generates a sequence of outputs.
Example: Generating a sequence from an image or initial input.
C. Many-to-Many
Maps an input sequence to an output sequence.
Example: Sequence labelling and language translation.
D. LSTM (Long Short-Term Memory)
LSTM is a special type of RNN that uses gates to help preserve useful information over longer sequences.
E. GRU (Gated Recurrent Unit)
GRU is another gated RNN variant designed to help manage information and long-term dependencies.
- Python Implementation
We can implement a simple RNN using TensorFlow and Keras.
In this example, we create a small model that learns to calculate the sum of five numbers in a sequence.
Step 1: Install Required Libraries
pip install numpy tensorflow
Step 2: Python Program
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Input, SimpleRNN, Dense
Create sample data
X = np.random.rand(100, 5, 1).astype("float32")
Target: sum of five values
y = X.sum(axis=1)
Create RNN model
model = Sequential([
Input(shape=(5, 1)),
SimpleRNN(16, activation="tanh"),
Dense(1)
])
Compile the model
model.compile(
optimizer="adam",
loss="mse"
)
Train the model
model.fit(
X, y,
epochs=10,
batch_size=8,
verbose=0
)
Make a prediction
sample = X[:1]
prediction = model.predict(sample, verbose=0)
print("Predicted sum:", prediction[0][0])
print("Actual sum:", y[0][0])
- Expected Output
The program prints two values:
Predicted sum: (model-generated value)
Actual sum: (actual sum of the five input values)
Output Explanation:
Predicted sum represents the value estimated by the trained RNN.
Actual sum represents the correct sum of the five input values.
The exact predicted value varies because the sample data and initial model weights are random.
- Real-Life Applications of RNN
RNNs are used in various applications:
Language modelling and text processing.
Speech recognition.
Time-series forecasting.
Sequence labelling.
Sensor-data analysis.
- Advantages of RNN
Processes ordered and sequential data.
Reuses weights across time steps.
Uses previous information as context.
Suitable for many sequence-learning tasks.
- Limitations of RNN
Basic RNNs can struggle with long-term dependencies.
Training can be slower because sequence steps are processed in order.
May experience vanishing or exploding gradients.
LSTM and GRU architectures help address some of these limitations.
- Conclusion
Recurrent Neural Networks introduced an effective way for neural networks to process sequential data and use information from earlier steps.
They are important in understanding sequence modelling, speech recognition, language processing and time-series analysis.
Learning RNNs provides a foundation for studying advanced deep learning architectures such as LSTM and GRU.
- References and Further Reading
TensorFlow Learning Resources
Scikit-learn Documentation
GeeksforGeeks – Computer Science
Top comments (0)