This project demonstrates how to train an LSTM model to predict stock prices using historical data. The model is trained on Google Colab and deployed using AWS SageMaker. It will demonstrate how to deploy an LSTM model for stock price prediction using AWS SageMaker. It includes preprocessing a CSV file (test_input.csv), scaling it, and invoking a SageMaker endpoint for inference.
.
├── SageMaker.py # Loads test_input.csv, scales & reshapes it, and invokes endpoint
├── predict.py # Used inside SageMaker container for inference
├── scaler.pkl # Fitted scaler for transforming input features
├── test_input.csv # New input data (e.g., Open, High, Low columns)
└── lstm_model_deploy.tar.gz # Trained LSTM model to be deployed on SageMaker
- Model Type: LSTM (Long Short-Term Memory)
- Framework: TensorFlow/Keras
- Deployment: AWS SageMaker Endpoint
- Input Format: CSV file containing columns like
Open,High, andLow - Output: Predicted stock prices (scaled or unscaled based on implementation)
This script runs inside the SageMaker container and:
- Loads the LSTM model from
/opt/ml/model - Accepts preprocessed input via JSON
- Returns predictions as a JSON response
This is run on your SageMaker Notebook Instance, and it:
- Reads
test_input.csv - Uses
scaler.pklto transform the data - Builds time-step input for the LSTM
- Sends the preprocessed data to the deployed endpoint
- Receives predictions and optionally inverse-scales them
Your CSV file should contain at least the following columns:
Open,High,Low
100,105,98
102,108,101
...
⚠️ At least 60 rows are required to form one LSTM input sample if your time step is set to 60.
Upload the following files to your S3 bucket:
lstm_model_deploy.tar.gzpredict.py(used in the model container)test_input.csv- scaler.pkl
You can upload using AWS CLI:
aws s3 cp lstm_model_deploy.tar.gz s3://your-bucket/your-folder/
aws s3 cp scaler.pkl s3://your-bucket/your-folder/
aws s3 cp predict.py s3://your-bucket/your-folder/
aws s3 cp test_input.csv s3://your-bucket/your-folder/Or use Python boto3:
import boto3
s3 = boto3.client('s3')
bucket = 'your-bucket'
prefix = 'your-folder/'
s3.upload_file('lstm_model_deploy.tar.gz', bucket, prefix + 'lstm_model_deploy.tar.gz')
s3.upload_file('scaler.pkl', bucket, prefix + 'scaler.pkl')
s3.upload_file('predict.py', bucket, prefix + 'predict.py')
s3.upload_file('test_input.csv', bucket, prefix + 'test_input.csv')On SageMaker Notebook, run:
python SageMaker.pyThis will:
- Deploy the model to a real-time SageMaker endpoint
- Send
test_input.csv(after scaling) as a request - Print the predicted values
- Python 3.8+
boto3sagemakerpandas,numpy,tensorflow,scikit-learn
- Ensure your LSTM model expects input shape
(samples, 60, 3)or adjust accordingly. - Input preprocessing, including scaling and reshaping, is handled in SageMaker.py before the request is sent to the endpoint. The predict.py file inside the SageMaker model container only performs inference on already-preprocessed input.
- Add visualization for predicted vs. actual prices
- Automate daily prediction pipeline
- Deploy with API Gateway for public access