Build LSTM Models for Stock Price Prediction with TensorFlow
In the last lesson, we explored the basics of Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks. We learned how RNNs handle sequential data and why LSTMs are better at capturing long-term dependencies. We also discussed the structure of LSTM cells, which use gates to control the flow of information. This foundation is crucial for understanding how to apply LSTMs to real-world problems like stock price prediction.
Now, let’s dive into the practical side of things. In this lesson, we’ll build an LSTM model to predict stock prices using TensorFlow and Keras. This is a hands-on tutorial, so I’ll walk you through every step, from preparing the data to fine-tuning the model.
Use-Case: Predicting Stock Prices with LSTMs
I recently worked on a project where I had to predict the stock prices of a tech company. The goal was to forecast the next day’s closing price based on historical data. I chose LSTMs because they excel at handling time-series data, which is sequential and often has patterns that repeat over time.
The dataset I used contained daily stock prices, including the opening price, closing price, highest price, lowest price, and trading volume. My challenge was to prepare this data for the LSTM model and ensure it could learn meaningful patterns. I faced issues like overfitting and poor performance initially, but by fine-tuning the model, I achieved better results.
Preparing Time-Series Data for LSTM Models
The first step in building an LSTM model is preparing the data. Time-series data needs to be structured in a way that the model can understand. For stock price prediction, we use historical prices to predict future values. Here’s how I did it:
-
Load the Data: I started by loading the stock price data into a Pandas DataFrame. The data included columns like Date, Open, Close, High, Low, and Volume.
-
Normalize the Data: LSTMs perform better when the input data is scaled. I normalized the data using Min-Max scaling, which transforms the values to a range between 0 and 1.
-
Create Sequences: LSTMs require input data in the form of sequences. I created sequences of 60 days of historical data to predict the next day’s closing price. For example, the first sequence included days 1 to 60, and the target was day 61.
Here’s a code snippet to create sequences:
import numpy as np
def create_sequences(data, seq_length):
X = []
y = []
for i in range(seq_length, len(data)):
X.append(data[i-seq_length:i, 0])
y.append(data[i, 0])
return np.array(X), np.array(y)
seq_length = 60
X, y = create_sequences(scaled_data, seq_length)
Building the LSTM Model
Once the data was ready, I built the LSTM model using Keras. Here’s how I structured the model:
Input Layer: The input layer takes sequences of 60 time steps with one feature (closing price).
LSTM Layers: I added two LSTM layers with 50 units each. These layers capture the patterns in the data.
Dense Layer: The output layer is a Dense layer with one unit, which predicts the next day’s closing price.
Here’s the code for the model:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(X.shape[1], 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
Training the Model
Training the model is straightforward. I split the data into training and testing sets and trained the model for 20 epochs. Here’s the code:
model.fit(X_train, y_train, batch_size=32, epochs=20)
During training, I monitored the loss to ensure the model was learning. Initially, the loss was high, but it decreased over time, indicating that the model was improving.
Evaluating Model Performance
After training, I evaluated the model on the test data. I used Mean Squared Error (MSE) to measure performance. The model performed well on the training data but struggled with the test data, indicating overfitting. To address this, I added Dropou layers and reduced the number of units in the LSTM layers.
Here’s the updated model:
from tensorflow.keras.layers import Dropout
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(X.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(50, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(1))
Fine-Tuning the Model
Fine-tuning is crucial for improving model performance. I experimented with different hyperparameters, such as the number of LSTM units, learning rate, and dropout rate. I also tried increasing the sequence length to capture longer-term patterns.
After several iterations, the model’s performance improved significantly. The predictions were closer to the actual stock prices, and the MSE decreased.
Conclusion
In this tutorial, we walked through the process of building and training an LSTM model for stock price prediction. We started by preparing the time-series data, then built and trained the model using TensorFlow and Keras. We also discussed how to evaluate and fine-tune the model for better performance.
LSTMs are powerful tools for time-series forecasting, and with practice, you can apply them to various real-world problems. In the next lesson, we’ll explore sequence modeling for text generation using LSTMs. This will build on the concepts we’ve covered here.
Comments
There are no comments yet.