Define LSTM class model

Now, we defined a PyTorch network using LSTM architecture. The class consist of LSTM layer and linear layer. In LSTMModel class, we initialized parameters-

  • input_size : number of features in the input data at each time step
  • hidden_size : hidden units in LSTM layer
  • num_layers : number of LSTM layers
  • batch_first= True: input data will have the batch size as the first dimension

The function super(LSTMModel, self).__init__() initializes the parent class for building the neural network.

The forward method defines the forward pass of the model, where the input x is processed through the layers of the model to produce an output.

Python3




class LSTMModel(nn.Module):
      # input_size : number of features in input at each time step
      # hidden_size : Number of LSTM units
      # num_layers : number of LSTM layers
    def __init__(self, input_size, hidden_size, num_layers):
        super(LSTMModel, self).__init__() #initializes the parent class nn.Module
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
        self.linear = nn.Linear(hidden_size, 1)
 
    def forward(self, x): # defines forward pass of the neural network
        out, _ = self.lstm(x)
        out = self.linear(out)
        return out


Check Hardware Availability

For the PyTorch code, we need to check the hardware resources.

Python3




device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)


Output:

cuda

Defining the model

Now, we define the model, loss function and optimizer for the forecasting. We have adjusted the hyperparameters of the model and set the loss fuction to mean squared error. To optimize the parameters during the training, we have considered Adam optimizer.

Python3




input_size = 1
num_layers = 2
hidden_size = 64
output_size = 1
 
# Define the model, loss function, and optimizer
model = LSTMModel(input_size, hidden_size, num_layers).to(device)
 
loss_fn = torch.nn.MSELoss(reduction='mean')
 
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
print(model)


Output:

LSTMModel(
(lstm): LSTM(1, 32, num_layers=2, batch_first=True)
(linear): Linear(in_features=32, out_features=1, bias=True)
)

Time Series Forecasting using Pytorch

Time series forecasting plays a major role in data analysis, with applications ranging from anticipating stock market trends to forecasting weather patterns. In this article, we’ll dive into the field of time series forecasting using PyTorch and LSTM (Long Short-Term Memory) neural networks. We’ll uncover the critical preprocessing procedures that underpin the accuracy of our forecasts along the way.

Table of Content

  • Time Series Forecasting
  • Implementation of Time Series Forecasting:
  • Step 1: Import the necessary libraries
  • Step2: Loading the Dataset
  • Step 3: Data Preprocessing
  • Step 4: Define LSTM class model
  • Step 5: Creating Data Loader for batch training
  • Step 6: Model Training & Evaluations
  • Step 7: Forecasting

Similar Reads

Time Series Forecasting

...

Implementation of Time Series Forecasting:

Time series data is essentially a set of observations taken at regular periods of time. Time series forecasting attempts to estimate future values based on patterns and trends detected in historical data....

Step 1: Import the necessary libraries

Prerequisite...

Step2: Loading the Dataset

Python3 import pandas as pd import numpy as np import math import matplotlib.pyplot as plt # Visualization import matplotlib.dates as mdates # Formatting dates import seaborn as sns # Visualization from sklearn.preprocessing import MinMaxScaler import torch # Library for implementing Deep Neural Network import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data import Dataset, DataLoader...

Step 3: Data Preprocessing

...

Step 4: Define LSTM class model

In this step, we are using ‘yfinance’ library to download historical stock market data for Apple Inc. (AAPL) from Yahoo Finance....

Step 5: Creating Data Loader for batch training

...

Step 6: Model Training & Evaluations

Plot the time series trend using Matplotlib...

Step 7: Forecasting

...