This repository contains various Python scripts for implementing and evaluating different machine learning models for time-series forecasting. The models include Convolutional Neural Networks (CNN), Long Short-Term Memory networks (LSTM), and Transformer models. The scripts are designed to forecast data at different time intervals: 10 minutes, 30 minutes, and 60 minutes.
Auto Data.py: Script for data preprocessing and automated data handling.CNN10 Minutes.py: CNN model for 10-minute interval forecasting.CNN30 Minutes.py: CNN model for 30-minute interval forecasting.CNN60 Minutes.py: CNN model for 60-minute interval forecasting.Hierarchal Transformer-Auto 10 Minutes-CNN.py: Hierarchical Transformer-Auto model with CNN for 10-minute interval forecasting.Hierarchal Transformer-Auto 30 Minutes-CNN.py: Hierarchical Transformer-Auto model with CNN for 30-minute interval forecasting.Hierarchal Transformer-Auto 60 Minutes-CNN.py: Hierarchical Transformer-Auto model with CNN for 60-minute interval forecasting.LSTM-Simple 10 Minutes.py: Simple LSTM model for 10-minute interval forecasting.LSTM-Simple 30 Minutes.py: Simple LSTM model for 30-minute interval forecasting.LSTM-Simple 60 Minutes.py: Simple LSTM model for 60-minute interval forecasting.Transformer-Auto 10 Minutes.py: Transformer-Auto model for 10-minute interval forecasting.Transformer-Auto 30 Minutes.py: Transformer-Auto model for 30-minute interval forecasting.Transformer-Auto 60 Minutes.py: Transformer-Auto model for 60-minute interval forecasting.Transformer-Autoencoder-ED.py: Transformer model with autoencoder for Encoder-Decoder structure.Transformer-Simple 10 Minutes.py: Simple Transformer model for 10-minute interval forecasting.Transformer-Simple 30 Minutes.py: Simple Transformer model for 30-minute interval forecasting.Transformer-Simple 60 Minutes.py: Simple Transformer model for 60-minute interval forecasting.
Ensure you have the following libraries installed:
- PyTorch
- NumPy
- Pandas
- Scikit-learn
- Matplotlib
- Plotly
You can install them using pip:
pip install torch numpy pandas scikit-learn matplotlib plotly- Data Preparation: Use
Auto Data.pyto preprocess your dataset. - Training the Models: Run the respective Python script for the model and time interval you wish to train. For example, to train a CNN model for 10-minute interval forecasting, use:
python CNN10\ Minutes.py - Evaluation: Each script will train the model and provide evaluation metrics on the test set.
The following is a brief explanation of the key components in the sample code:
-
Library Imports:
- The code starts by importing all necessary libraries, including PyTorch, NumPy, Pandas, and others for data manipulation, model building, and visualization.
-
Seed Initialization:
- A function
reset_random_seedsis defined to ensure reproducibility by setting the seed for random number generators.
- A function
-
Data Loading:
- Data is loaded from a pickle file using
pickle.load, and then it is split into training and testing sets.
- Data is loaded from a pickle file using
-
Normalization Function:
- Functions
reconstructDataandnormare defined to preprocess and normalize the data.
- Functions
-
Model Definition:
- Various classes are defined to build the models, including
PositionalEmbedding,MultiHeadAttention,TransformerBlock,TransformerEncoder,LSTMModel,Classifier,HierarchicalTransformerEncoder, andHierarchicalTransformerDecoder.
- Various classes are defined to build the models, including
-
Training Loop:
- A training loop is defined to train the models. It includes forward passes, loss calculation, backpropagation, and evaluation.
-
Evaluation:
- The models are evaluated on the validation and test sets, and performance metrics like accuracy, precision, recall, F1 score, and AUC are printed.
CNN10 Minutes.pyCNN30 Minutes.pyCNN60 Minutes.py
LSTM-Simple 10 Minutes.pyLSTM-Simple 30 Minutes.pyLSTM-Simple 60 Minutes.py
Transformer-Simple 10 Minutes.pyTransformer-Simple 30 Minutes.pyTransformer-Simple 60 Minutes.pyTransformer-Auto 10 Minutes.pyTransformer-Auto 30 Minutes.pyTransformer-Auto 60 Minutes.pyTransformer-Autoencoder-ED.py
Hierarchal Transformer-Auto 10 Minutes-CNN.pyHierarchal Transformer-Auto 30 Minutes-CNN.pyHierarchal Transformer-Auto 60 Minutes-CNN.py
Feel free to contribute by submitting a pull request. Please ensure your changes are well-documented and tested.
This project is licensed under the MIT License. See the LICENSE file for details.
For any questions or inquiries, please contact bluecodeindia@gmail.com.