Ticker

6/recent/ticker-posts

Deep Learning Lab- BAI701-Program 6

 

Program 6

Design and implement a deep learning network for forecasting time series data.

Purpose of the Program

The program is designed to forecast future values of a time series using a deep learning model (LSTM).

It demonstrates the complete pipeline for time series forecasting, including:

  1. Data Preparation
    • Generates a synthetic time series (sine wave + trend + noise).
    • Normalizes (scales) the data.
    • Splits it into training, validation, and test sets.
    • Converts the series into sliding windows (past values → next value).
  2. Deep Learning Model (LSTM)
    • Builds an LSTM network in PyTorch.
    • Learns patterns from past values to predict the next time step.
  3. Training & Evaluation
    • Trains the LSTM model using MSE loss and the Adam optimizer.
    • Monitors validation loss to avoid overfitting.
    • Evaluates on test data using RMSE and MAE.
  4. Forecasting
    • Makes predictions on test data.
    • Can generate multi-step forecasts (predicting several future values by feeding predictions back into the model).
  5. Visualization & Saving
    • Plots actual vs. predicted values.
    • Saves the trained model for future use.

In short:
This program is a working template for building and training an LSTM deep learning model to forecast time series data. You can replace the synthetic sine-wave dataset with your own dataset (stock prices, weather data, sensor data, etc.) to use it for real forecasting tasks.

 

STM-Based Time Series Forecasting in Python

Step 1: Import Libraries

Component

Purpose

Role in Time Series Forecasting

numpy (np)

Numerical operations

Efficient array handling and reshaping

pandas (pd)

Data manipulation

Reading and organizing time series data

matplotlib.pyplot (plt)

Visualization

Plotting trends and predictions

MinMaxScaler from sklearn.preprocessing

Data normalization

Scales values to [0, 1] for stable LSTM training

Sequential from tensorflow.keras.models

Model architecture

Builds a linear stack of layers

LSTM from tensorflow.keras.layers

Recurrent layer

Captures temporal dependencies in sequences

Dense from tensorflow.keras.layers

Output layer

Produces final forecast value

Step 2: Generate Synthetic Time Series Data

Synthetic Time Series Generator

Component

Purpose

generate_data(n_points=500)

Defines a function to create a noisy sine wave with 500 data points

x = np.arange(n_points)

Generates a sequence of integers from 0 to 499 (time steps)

y = np.sin(0.02 * x)

Creates a smooth sine wave with a low frequency

+ 0.5 * np.random.normal(size=n_points)

Adds Gaussian noise to simulate real-world variability

return y

Returns the noisy time series

plt.plot(data)

Plots the generated time series

plt.title("Synthetic Time Series")

Adds a title to the plot

plt.show()

Displays the plot visually

Step 3: Preprocess Data

 Data Preparation for LSTM Forecasting

Step

Purpose

Explanation

MinMaxScaler()

Normalization

Scales data to the range [0, 1] to stabilize training and improve convergence.

data.reshape(-1, 1)

Reshape for scaler

Converts 1D array to 2D format required by MinMaxScaler.

create_sequences(data, seq_length)

Sequence generation

Splits the time series into overlapping input-output pairs for supervised learning.

seq_length = 20

Window size

Each input sequence contains 20 time steps; the target is the next value.

X = X.reshape((X.shape[0], X.shape[1], 1))

Reshape for LSTM

Formats data into 3D shape: [samples, time steps, features], which LSTM expects.

 

This setup transforms raw time series into a structured format that captures temporal dependencies—perfect for training an LSTM to forecast future values.

Step 4: Build LSTM Model

LSTM Model Summary

Line

Purpose

Explanation

Sequential([...])

Model architecture

Builds a linear stack of layers for the neural network.

LSTM(50, activation='relu', input_shape=(seq_length, 1))

Recurrent layer

Adds an LSTM layer with 50 units. It takes sequences of length seq_length with 1 feature per time step. The ReLU activation helps capture non-linear patterns.

Dense(1)

Output layer

Adds a fully connected layer that outputs a single value—used to predict the next time step.

model.compile(optimizer='adam', loss='mse')

Training configuration

Uses the Adam optimizer for efficient training and Mean Squared Error (MSE) as the loss function to measure prediction accuracy.

model.summary()

Model inspection

Prints a summary of the model architecture, including layer types, output shapes, and number of parameters.

This model is well-suited for univariate time series forecasting.

Step 5: Train the Model

You're telling your LSTM model to start learning from the time series data you've prepared. Specifically:

  • model.fit(...) is the command that kicks off training.
  • You're feeding it the input sequences (X) and the corresponding target values (y)—these are the examples it will learn from.
  • epochs=20 means the model will go through the entire dataset 20 times, refining its internal weights each time to improve accuracy.
  • batch_size=32 means it will process 32 samples at a time before updating its weights. This helps balance memory usage and training speed.
  • validation_split=0.2 means you're setting aside 20% of the data to test the model after each training round. This helps you monitor how well it's learning without overfitting to the training data.
  • The result is stored in a variable called history, which keeps track of how the model's performance changes over time—like a training diary.

In short: you're teaching the model to recognize patterns in your time series data so it can predict future values more accurately.

Step 6: Forecast and Visualize

Evaluate Performance

Post a Comment

0 Comments