Practical Implementation of RNNs and LSTMs with TensorFlow - Time Series Forecasting and NLP


In this follow-up article, we will take a hands-on approach to implementing RNNs and LSTMs using TensorFlow. We’ll go through two real-world examples: time series forecasting and text classification. By the end, you will understand how to structure and train these models for sequential data tasks.


Table of Contents

  1. Time Series Forecasting Using LSTM
  2. Text Classification Using RNN and LSTM
  3. Importance of Hyperparameter Tuning in RNNs and LSTMs
  4. Conclusion

1. Time Series Forecasting Using LSTM

Time series forecasting is a common task for RNNs and LSTMs. We’ll use the daily minimum temperature dataset (available on Kaggle) to predict future temperatures based on past observations.

1.1 Dataset Preparation

We begin by loading and preprocessing the time series data.

import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler

# Load dataset
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv'
df = pd.read_csv(url, parse_dates=['Date'], index_col='Date')

# Prepare the data
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(df.values)

# Create sequences of time steps (input) and future values (target)
def create_sequences(data, seq_length):
    x, y = [], []
    for i in range(len(data) - seq_length):
        x.append(data[i:i + seq_length])
        y.append(data[i + seq_length])
    return np.array(x), np.array(y)

seq_length = 30  # Use the past 30 days to predict the next day's temperature
x, y = create_sequences(scaled_data, seq_length)

# Split the dataset into training and test sets
train_size = int(len(x) * 0.8)
x_train, x_test = x[:train_size], x[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

1.2 Building the LSTM Model

Next, we’ll create an LSTM-based model in TensorFlow for forecasting the future temperatures.

# Build the LSTM model
model = tf.keras.Sequential([
    tf.keras.layers.LSTM(50, return_sequences=True, input_shape=(seq_length, 1)),
    tf.keras.layers.LSTM(50),
    tf.keras.layers.Dense(1)
])

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

# Train the model
history = model.fit(x_train, y_train, epochs=20, batch_size=32, validation_data=(x_test, y_test))

# Evaluate the model
test_loss = model.evaluate(x_test, y_test)
print(f'Test Loss: {test_loss}')

1.3 Making Predictions

After training, we can use the model to predict future temperatures.

# Make predictions
predicted_temps = model.predict(x_test)
predicted_temps = scaler.inverse_transform(predicted_temps)

# Plot the actual vs predicted temperatures
import matplotlib.pyplot as plt

plt.plot(df.index[-len(y_test):], scaler.inverse_transform(y_test.reshape(-1, 1)), label='Actual')
plt.plot(df.index[-len(y_test):], predicted_temps, label='Predicted')
plt.legend()
plt.show()

1.4 Real-World Applications

This approach is commonly used in financial markets for stock price prediction, energy consumption forecasting, and weather predictions. The ability of LSTMs to capture long-term dependencies in time series makes them well-suited for these tasks.


2. Text Classification Using RNN and LSTM

Text classification is a popular NLP task where RNNs and LSTMs excel. In this section, we’ll classify movie reviews as positive or negative using the IMDb dataset.

2.1 Dataset Preparation

We will use TensorFlow’s built-in IMDb dataset, which contains 50,000 movie reviews.

# Load the IMDb dataset
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences

# Load and preprocess the data
vocab_size = 10000  # Use the top 10,000 words in the dataset
max_len = 200  # Maximum length of each review

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)

# Pad sequences to ensure uniform input size
x_train = pad_sequences(x_train, maxlen=max_len)
x_test = pad_sequences(x_test, maxlen=max_len)

2.2 Building the RNN and LSTM Model

We’ll build a simple model with an embedding layer followed by an LSTM for text classification.

# Build the RNN/LSTM model
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, 128, input_length=max_len),
    tf.keras.layers.LSTM(64),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

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

# Train the model
history = model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))

# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
print(f'Test Accuracy: {accuracy}')

2.3 Predicting Sentiment

Now we can use the trained model to predict the sentiment of new reviews.

# Example review for prediction
new_review = "The movie was fantastic! I really enjoyed the acting and plot."

# Preprocess and predict sentiment (0: negative, 1: positive)
def preprocess_review(review, tokenizer):
    tokens = tokenizer.texts_to_sequences([review])
    return pad_sequences(tokens, maxlen=max_len)

prediction = model.predict(preprocess_review(new_review, imdb.get_word_index()))
print(f'Predicted Sentiment: {"Positive" if prediction[0] > 0.5 else "Negative"}')

2.4 Real-World Applications

Text classification using LSTMs is widely used in spam detection, sentiment analysis, and language translation. The ability of LSTMs to capture context and sequence information in text makes them ideal for these applications.


3. Importance of Hyperparameter Tuning in RNNs and LSTMs

Properly tuning the hyperparameters in RNNs and LSTMs is essential for improving model performance. Critical hyperparameters like the learning rate, number of hidden units, dropout rate, and batch size can dramatically affect the model’s ability to learn patterns from sequential data.

Key Hyperparameters to Tune:

  • Learning Rate: Controls how much the model weights are adjusted after each step.
  • Dropout Rate: Used to prevent overfitting by randomly dropping units during training.
  • Number of LSTM Units: Determines the capacity of the network to capture long-term dependencies.
  • Sequence Length: Affects the range of time steps that the model considers for predictions.

4. Conclusion

This article provided a hands-on guide to implementing RNNs and LSTMs using TensorFlow for real-world tasks like time series forecasting and text classification. LSTMs’ ability to capture long-term dependencies makes them powerful tools for sequential data, while RNNs are still effective for shorter sequences.

By carefully tuning hyperparameters and experimenting with different architectures, you can significantly improve your model’s performance. Feel free to explore more advanced topics, such as bidirectional LSTMs or attention mechanisms, for even better results!

© 2024 Dominic Kneup