Long Short-Term Memory (LSTM) Example

This is a simple example of Long Short-Term Memory (LSTM) using Python and TensorFlow/Keras.

Long Short-Term Memory (LSTM) Overview

Long Short-Term Memory (LSTM) networks are a type of recurrent neural network (RNN) designed to address the vanishing gradient problem and capture long-term dependencies in sequential data. LSTMs use memory cells with self-gating mechanisms to selectively store and retrieve information over extended sequences, making them well-suited for tasks such as time series prediction, natural language processing, and more.

Key concepts of Long Short-Term Memory:

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 LSTM, 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 LSTM
X = X.reshape(X.shape[0], X.shape[1], 1)

# Build an LSTM model
model = Sequential([
    LSTM(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='LSTM Predictions', marker='x')
plt.title('Long Short-Term Memory (LSTM) Example')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.show()

Explanation: