Recurrent Neural Networks (RNN) Example

This is a simple example of Recurrent Neural Networks (RNN) using Python and TensorFlow/Keras.

Recurrent Neural Networks Overview

Recurrent Neural Networks (RNNs) are a class of neural networks designed for sequence data. Unlike traditional feedforward neural networks, RNNs have connections that form cycles, allowing them to maintain a hidden state and capture dependencies across time steps. RNNs are widely used in natural language processing, time series analysis, and other tasks involving sequential data.

Key concepts of Recurrent Neural Networks:

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
from sklearn.metrics import mean_squared_error

# Generate synthetic time series data
np.random.seed(42)
time = np.arange(0, 100, 1)
sinusoid = np.sin(0.1 * time) + 0.1 * np.random.randn(100)

# Create input sequences and corresponding target values
X, y = [], []
sequence_length = 10

for i in range(len(sinusoid) - sequence_length):
    X.append(sinusoid[i:i+sequence_length])
    y.append(sinusoid[i+sequence_length])

X, y = np.array(X), np.array(y)

# Reshape the input data for RNN
X = X.reshape(X.shape[0], X.shape[1], 1)

# Build an RNN model
model = Sequential([
    SimpleRNN(10, activation='relu', input_shape=(sequence_length, 1)),
    Dense(1)
])

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model
model.fit(X, y, epochs=50, verbose=0)

# Make predictions on the entire time series
y_pred = model.predict(X)

# Plot the results
plt.plot(time, sinusoid, label='Original Data', marker='o')
plt.plot(time[sequence_length:], y_pred, label='RNN Predictions', marker='x')
plt.title('Recurrent Neural Networks (RNN) Example')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.show()

Explanation: