Skip to main content
Course/Module 5/Topic 3 of 4Beginner

Recurrent Neural Networks (RNN)

Explore Recurrent Neural Networks for sequential data processing.

60 minBy Priygop TeamLast updated: Feb 2026

What are RNNs?

Recurrent Neural Networks (RNNs) are designed to work with sequential data. They have connections that form directed cycles, allowing them to maintain internal memory.

Key Components

  • Hidden State: Memory of previous inputs
  • Input Gate: Controls new information
  • Forget Gate: Controls what to forget
  • Output Gate: Controls what to output

Implementation

Example
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

# Generate sample sequential data
np.random.seed(42)
X = np.random.randn(1000, 50, 10)  # 1000 sequences, 50 time steps, 10 features
y = np.random.randint(0, 5, 1000)  # 5 classes

# Convert to one-hot encoding
y = tf.keras.utils.to_categorical(y, 5)

# Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create RNN model
model = keras.Sequential([
    keras.layers.LSTM(64, return_sequences=True, input_shape=(50, 10)),
    keras.layers.LSTM(32),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(5, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
history = model.fit(X_train, y_train,
                    epochs=10,
                    batch_size=32,
                    validation_split=0.2,
                    verbose=1)

# Evaluate the model
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test accuracy: {test_accuracy:.4f}")

Try It Yourself — Recurrent Neural Networks (RNN)

Try It Yourself — Recurrent Neural Networks (RNN)JavaScript
JavaScript Editor
✓ ValidTab = 2 spaces
JavaScript|33 lines|986 chars|✓ Valid syntax
UTF-8

Additional Resources

Recommended Reading

  • Deep Learning by Ian Goodfellow
  • Neural Networks and Deep Learning by Michael Nielsen
  • TensorFlow and Keras Documentation

Online Resources

  • TensorFlow Tutorials
  • Keras Documentation
  • CNN Architecture Guide
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep