← Back to list

One Real World Example of RNN (Recurrent Neural Networks)

Understanding Recurrent Neural Networks (RNNs)

Harsh Gala · 2026-04-21 03:48 · 55 claps · 5.4 min read
#rnn #deep-learning #machine-learning #imdb #sentiment-analysis
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning

One Real World Example of RNN (Recurrent Neural Networks)

Understanding Recurrent Neural Networks (RNNs)

A Recurrent Neural Network (RNN) is a class of artificial neural networks specifically designed to process sequential data. While traditional feedforward neural networks assume that all inputs (and outputs) are independent of each other, RNNs are designed to recognize patterns in sequences — such as text, genomes, handwriting, spoken word, or numerical time series data.

The defining feature of an RNN is its “memory,” which allows it to take information from prior inputs to influence the current input and output.

The Core Architecture: The “Loop”:

In a standard neural network, information flows from the input layer, through the hidden layers, to the output layer. In an RNN, the information cycles through a loop. When it makes a decision, it considers the current input and also what it has learned from the inputs it received previously.

Why RNNs are Necessary: The Context Factor:

To understand why RNNs are revolutionary, consider the task of Sentiment Analysis.

Take the sentence: “The movie was not very good.”

  • A traditional network might see the word “good” and classify the sentence as positive.
  • An RNN processes the words one by one. By the time it reaches “good,” its internal hidden state still carries the context of the word “not.” It understands that “not” modifies “good,” resulting in a negative sentiment classification.

Recurrent Neural Networks (RNNs)

Recurrent Neural Networks (RNNs)

Step-by-Step Process:

  1. Input Sentence “The movie was really amazing”

2. Convert Words into Vectors

Each word is transformed into numbers (word embeddings)

3. Sequential Processing

RNN reads one word at a time:

“The” → “movie” → “was” → “really” → “amazing”

4. Memory Mechanism

Keeps track of important words like “amazing”

5. Final Output Predicts sentiment: Positive

Positive Review

Positive Review

Real-World Problem:

Movie reviews are messy. They aren’t just simple lists of words; they are complex sequences. This is why the IMDb dataset is specifically used to demonstrate the power of RNNs.

Example A: The Sequential Challenge

“The acting was great, but the plot was so boring I fell asleep.”

A simple “Bag of Words” model might see the word “great” and think the review is positive. However, an RNN reads the sequence. It remembers the context of the word “but” and realizes that the sentiment shifts to negative by the end of the sentence.

What is the IMDb Dataset?

In a real-world business context, the IMDb dataset represents a massive collection of customer feedback. It consists of 50,000 movie reviews that have been manually labeled by humans.

  • 25,000 reviews for training: Used to teach the RNN what “angry” vs. “happy” looks like.
  • 25,000 reviews for testing: Used to see if the RNN can accurately guess the sentiment of a review it has never seen before.
  • Binary Labels: Each review is marked as either Positive (1) or Negative (0).

How the Data is Structured (Preprocessing)

In our code, we likely notice max_features = 10000. This is a real-world optimization technique:

  1. Integer Encoding: Machines can’t read “The movie was great.” Each word is assigned a unique number based on its frequency. For example, “The” might be 1, "movie" might be 15, and "great" might be 42.
  2. Vocabulary Limit: By setting max_features, we only keep the 10,000 most common words. Rare words (like a specific actor's obscure name) are discarded because they don't help the model learn general sentiment.
  3. Padding: Some reviews are 10 words long; others are 1,000. RNNs need a consistent shape. We use Padding to make every review exactly 500 words long by adding zeros or cutting off the extra text.

Hands-On Implementation: Sentiment Analysis:

In this technical walkthrough, we implement an RNN to perform sentiment analysis on the IMDb Movie Review Dataset.

Step 1: Preprocessing the Sequence

Computers don’t understand words; they understand numbers. We convert text into sequences of integers and use Padding to ensure every “review” is exactly the same length (500 words) for the model to process it.

import numpy as np
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence

# Configuration
max_features = 10000 # Only use the top 10k most frequent words 
maxlen = 500         # Cut off or pad reviews to 500 words

# Loading the dataset 
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)

# Padding sequences
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)

Step 2: Designing the Network

Our architecture consists of three layers:

Embedding Layer: Converts word indexes into dense vectors.

SimpleRNN Layer: The core “memory” unit with 32 units.

Dense Layer: A single neuron with a Sigmoid activation to output a 0 (negative) or 1 (positive)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense

model = Sequential()
# Layer 1: Embedding (Input size, Output vector size)
model.add(Embedding(max_features, 32)) 

# Layer 2: SimpleRNN (32 memory units)
model.add(SimpleRNN(32)) 

# Layer 3: Dense (Binary output: 0 or 1)
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) [cite: 26, 28, 29]

Based on experimental data, the model was trained for 5 epochs

history = model.fit(x_train, y_train,
epochs=5,
batch_size=64,
validation_split=0.2)

Output

Output

The Results:

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {test_acc:.2f}")

Output

Output

  • Test Accuracy: 0.79%

Advantages of RNNs:

  • Sequential Awareness: Unlike standard networks that treat a review as a “bag of words,” RNNs process words in order. This allows the model to understand that “not good” is the opposite of “good.”
  • Variable Length Handling: IMDb reviews can be 10 words or 500 words long. RNNs are designed to process inputs of varying lengths by using the same weights across every time step.
  • Contextual Memory: The “Hidden State” acts as a memory bank. It stores information from the beginning of a review (e.g., “I hated the director’s last film…”) to help interpret the end of the review (…but this one was a masterpiece”).
  • Parameter Sharing: Because the same hidden layer is used for every word in the sequence, the model has fewer parameters to learn compared to a massive deep-dense network, making it more efficient for text.

Disadvantages of RNNs:

  • The Vanishing Gradient Problem: This is the biggest hurdle. In long IMDb reviews, the “gradient” (the signal used to train the model) becomes so small that the network “forgets” the beginning of the review. It ends up only making a decision based on the last few words.
  • Slow Computation: Because RNNs are sequential (word 2 cannot be processed until word 1 is finished), they cannot be easily parallelized like CNNs or Transformers. This makes training on large datasets like IMDb slower.
  • Difficulty with Long-Term Dependencies: Even with memory, a “Vanilla” RNN struggles to connect two related pieces of information if they are separated by many sentences.
  • Risk of Overfitting: As seen in your experiment (98% training vs. 82% test accuracy), RNNs can easily memorize noise in the training reviews rather than learning the actual sentiment.

Conclusion:

The use of RNNs for the IMDb dataset marks a significant shift from simple statistics to contextual understanding. While “Vanilla” RNNs are prone to forgetting long-term context and suffer from vanishing gradients, they laid the groundwork for modern AI.

In your specific lab results, the 82% accuracy proves that even a simple RNN is highly capable of understanding the “vibe” of a movie review. However, to reach 90% or higher, the next logical step is upgrading to LSTMs (Long Short-Term Memory) or GRUs, which use “gates” to prevent the memory from fading over long sequences.

References:

[1] Dataset: IMDb Movie Reviews (Keras Dataset API).

[2] Theory: Hochreiter, S. (1991). The Vanishing Gradient Problem.


메타데이터
post_id
2eff2b45f55f
slug
one-real-world-example-of-rnn-recurrent-neural-networks-2eff2b45f55f
url
https://medium.com/@hitesh.harsh25/one-real-world-example-of-rnn-recurrent-neural-networks-2eff2b45f55f
canonical_url
https://medium.com/@hitesh.harsh25/one-real-world-example-of-rnn-recurrent-neural-networks-2eff2b45f55f
author_url
https://medium.com/@hitesh.harsh25
status
ok
fetched_at
2026-06-17 12:55:42