![]() |
VOOZH | about |
Long Short-Term Memory (LSTM) where designed to address the vanishing gradient issue faced by traditional RNNs in learning from long-term dependencies in sequential data. LSTMs are capable of maintaining information over extended periods because of memory cells and gating mechanisms. These memory cells are managed by three primary gates: the input gate, the forget gate and the output gate.
In this step, we will import the necessary libraries like pandas, numpy, matplotlib, scikit-learn and tensorflow. Here tensorflow library is used to create the LSTM Model.
Here we are using a dataset of monthly milk production using LSTM. You can download dataset from here.
Here we generate sequences of input data and split the dataset into training and testing sets.
This step involves defining and building the LSTM model architecture.
In this step, we train the model on the training data and evaluate its performance.
Output:
Model trains for 100 epochs with a batch size of 32, using 10% of the training data for validation.
In this step, we visualize the actual vs predicted values. A plot is generated to compare the actual milk production against the predicted values, allowing us to evaluate how well the model performs over time.
Output:
The LSTM model successfully captures the trends and patterns in the time series data. As observed, the predicted values closely follow the actual values with small variations during transitions between peaks and lows. This demonstrates the effectiveness of LSTM for time series prediction tasks such as forecasting milk production.
You can download source code from here.
You can implement LSTM using PyTorch also: Long Short Term Memory (LSTM) Networks using PyTorch