Skip to content

Shreyas8612/Embedded-Systems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weather Forecasting with Concurrent Deep Learning Models

This project implements and compares neural network architectures (LSTM, GRU, RNN) for weather forecasting, with a focus on implementing concurrent execution patterns for improved time efficiency. The system demonstrates advanced concurrency concepts including thread synchronization, producer-consumer patterns, and resource management.

System Architecture

Project Overview

  1. Concurrent execution: Using threads to create the ability to run multiple tasks simultaneously
  2. Thread synchronization: Ensuring safe resource sharing
  3. Producer-consumer patterns: Efficiently managing data flow
  4. Performance comparison: Evaluating multiple model architectures

Notebooks in this Project

Notebook Description
Temperature_Prediction_Basic.ipynb Sequential temperature prediction baseline
Temperature_Prediction_Concurrent.ipynb Concurrent implementation for temperature prediction
Precipitation_Prediction_Basic.ipynb Sequential precipitation prediction baseline
Precipitation_Prediction_Concurrent.ipynb Concurrent implementation for precipitation prediction
Comparison_of_Models.ipynb Multi-model concurrent comparison (LSTM vs RNN vs GRU)

Sequential vs. Concurrent Processing Journey

1. Temperature Prediction (Sequential)

The Temperature_Prediction_Basic.ipynb implements a standard LSTM model for temperature forecasting with sequential execution. Each stage (preprocessing, training, inference, analysis) waits for the previous to complete.

Key implementation aspects:

  • Single-threaded execution through sequential operations
  • Sliding window approach for time series forecasting
  • Standard LSTM architecture for sequence modeling

Performance Results:

Metrics calculated - MAE: 1.026, MSE: 1.674, RMSE: 1.294
Error statistics - Mean: -0.469, Std: 1.206
Total operation time: 123.80 seconds

2. Temperature Prediction (Concurrent)

The Temperature_Prediction_Concurrent.ipynb performs the temperature forecasting pipeline with concurrent execution using threads, semaphores, and synchronization events.

Key implementation aspects:

  • Multithreaded execution with producer-consumer pattern
  • Thread synchronization using events and semaphores
  • Buffer-based data flow between components

Performance Results:

[ANALYSIS] Metrics - MAE: 0.958, MSE: 1.549, RMSE: 1.245
[ANALYSIS] Error stats - Mean: 0.006, Std: 1.245
Total execution time: 101.63 seconds

Improvement: The concurrent implementation achieved better accuracy (6.6% lower MAE) while reducing execution time by 18%.

3. Precipitation Prediction (Sequential)

The Precipitation_Prediction_Basic.ipynb tackles the more challenging task of precipitation forecasting. Precipitation patterns are inherently more complex due to their high variability.

Key implementation aspects:

  • Feature engineering specifically for precipitation
  • LSTM model with an attention mechanism
  • Asymmetric loss function penalizing underestimation

Performance Results:

Mean Squared Error (MSE): 0.2017
Root Mean Squared Error (RMSE): 0.4491
Mean Absolute Error (MAE): 0.3373
R² Score: 0.6746
Total execution time: 145.69 seconds

4. Precipitation Prediction (Concurrent)

The Precipitation_Prediction_Concurrent.ipynb builds on the precipitation model with a fully concurrent implementation, demonstrating advanced thread synchronization.

Key implementation aspects:

  • Thread-safe buffer management
  • Mutex-protected shared dictionaries
  • Event-based causality enforcement
  • Enhanced feature engineering

Performance Results:

[ANALYSIS] Metrics - MAE: 0.333, MSE: 0.199, RMSE: 0.446, R²: 0.680
Total execution time: 81.24 seconds

Improvement: The concurrent implementation maintained similar accuracy while reducing execution time by 44.2%.

5. Multi-Model Comparison (Concurrent)

The Comparison_of_Models.ipynb showcases the full power of concurrency by training and evaluating three different neural network architectures simultaneously.

Key implementation aspects:

  • Parallel training of LSTM, RNN, and GRU models
  • Independent synchronization for each model
  • Shared preprocessing to ensure fair comparison
  • Comprehensive visualization and timing analysis

Performance Results:

=== Performance Timing Comparison ===
Model      Training Time (s)    Inference Time (s)   Total Time (s)
----------------------------------------------------------------------
LSTM       83.81                15.19                99.00
RNN        70.07                15.64                85.71
GRU        63.15                17.33                80.48

Model Accuracy Comparison:

GRU analysis:
[ANALYSIS] Metrics - MAE: 0.334, MSE: 0.200, RMSE: 0.447, R²: 0.678
[ANALYSIS] Error stats - Mean: -0.114, Std: 0.433

LSTM analysis:
[ANALYSIS] Metrics - MAE: 0.338, MSE: 0.204, RMSE: 0.452, R²: 0.671
[ANALYSIS] Error stats - Mean: -0.126, Std: 0.434

RNN analysis:
[ANALYSIS] Metrics - MAE: 0.373, MSE: 0.255, RMSE: 0.505, R²: 0.590
[ANALYSIS] Error stats - Mean: -0.102, Std: 0.494

Model Graph Comparison

Model Comparison

Concurrency Concepts Implemented

Concurrent Model Execution

The multimodel comparison demonstrates how multiple models can train simultaneously:

  • Parallel Training: Each model architecture (LSTM, RNN, GRU) trains in its own thread
  • Independent Progress: Models proceed at their natural pace without waiting for each other
      # GRU finishes first and begins inference while LSTM continues training, and RNN starts its inference after training
      [GRU TRAINING] Completed in 63.15 seconds
      [MAIN] Starting GRU inference
      [LSTM TRAINING] Read 3 batches from indices: 12 to 14
      [RNN TRAINING] Completed in 70.07 seconds
      [MAIN] Starting RNN inference
      [GRU INFERENCE] Completed in 17.33 seconds

Thread Synchronization

The system uses several synchronization mechanisms to enable safe concurrent execution:

  • Thread Events: Signal completion of critical stages

    preprocessing_done = threading.Event()
    training_done = threading.Event()
  • Wait Protocols: Ensure dependent tasks wait for prerequisites

    # Wait for training to provide a model
    training_done.wait()
  • Shared Locks: Provide safe access to results across threads

    with results_mutex:
        self.predictions = inference_results['predictions']

Producer-Consumer Pattern

The implementation follows a concurrent processing pattern with multiple producer-consumer relationships:

  • Buffer Management: Two buffer systems for training and inference data

    # Two buffers: one for Training, one for Inference
    data_buffer1 = [None] * CAPACITY  # Stores Training batches
    data_buffer2 = [None] * CAPACITY  # Stores Individual datapoints
  • Semaphore Control: Track available and filled buffer slots

    # Producer protocol
    empty1.acquire()  # Wait for empty slot
    mutex1.acquire()  # Acquire exclusive access lock
    data_buffer1[in_index1] = (batch_X, batch_y)  # Store data
    mutex1.release()  # Release lock
    full1.release()  # Signal that data is available
  • Mutex Protection: Ensure exclusive access to shared resources

    mutex1 = Semaphore(1)  # Protects access to buffer1
    mutex2 = Semaphore(1)  # Protects access to buffer2

Memory Management and Buffer Systems

Producer-Consumer Protocol

  • Producer (Preprocessing): Creates data batches and fills buffers
  • Consumer (Training/Inference): Reads and processes batches

Semaphore Signaling

  • Empty slots: Track available space in buffers
  • Full slots: Track filled buffer positions

LSTM Attention Mechanism for Weather Forecasting

  • Score Calculation: Each time step receives an attention score
  • Weight Normalization: Softmax converts scores to importance weights
  • Context Creation: Weighted combination emphasizes important time steps

This allows the model to focus on the most relevant past weather patterns for prediction.

Petri Net Representation

The concurrent system can be modeled as a Petri net to visualize its execution flow:

Petri Net Representation

Petri Net Concept Implementation
Places Buffer states (empty/filled), Thread states
Transitions Thread operations (preprocess, train, infer, analyze)
Tokens Data batches, Model states, Results
Arcs Data flow between operations

The analogy to Petri nets helps visualize the concurrent execution flow and resource management.

  • Fork: Preprocess happens once and the data is shared across all models (Similar to photocopying the same exam paper for multiple students)
  • Join: Waiting for multiple conditions before proceeding (Detachable)
  • Mutual Exclusion: Mutex locks ensure exclusive buffer access
  • Synchronization: Events establish causal dependencies between operations
  • Resource Allocation: Semaphores manage fixed buffer capacity

Conclusion and Key Insights

This project demonstrates the significant benefits of concurrent processing for deep learning workloads:

  1. Performance Improvements:

    • Temperature prediction: 18% faster execution
    • Precipitation prediction: 44% faster execution
  2. Model Effectiveness:

    • GRU showed the best performance-to-speed ratio
  3. Concurrency Benefits:

    • Reduced waiting time between pipeline stages
    • Parallel model training and evaluation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published