Skip to main content
. 2025 Mar 30;25(7):2197. doi: 10.3390/s25072197
Algorithm 1 Proposed BiLSTM-GRU with Attention Mechanism
Input: Sequential input data X={x1,x2,,xT}, where each xt is a feature vector at time t
Output: Predicted output sequence y^={y1,y2,,yT} after applying the BiLSTM-GRU model with attention
  •  1:

    Initialization: Initialize model weights and biases: Wlstm, Ulstm, blstm, Wa, ba, Wgru, Ugru, bgru, Wdense, bdense

  •  2:

    Initialize hidden states and cell states: ht, ht, ct, ct, ht1GRU

  •  3:

    Bidirectional LSTM Layer:

  •  4:

    for  (t=0,1,,T1)  do

  •  5:

     Forward LSTM step: Compute forward hidden state ht and cell state ct

  •  6:

     Backward LSTM step: Compute backward hidden state ht and cell state ct

  •  7:

     Concatenate forward and backward hidden states: HBiLSTMt

  •  8:

    end for

  •  9:

    Attention Mechanism:

  • 10:

    for (t=0,1,,T1) do

  • 11:

     Compute attention weights for each time step: et=tanh(WaHBiLSTMt+ba)

  • 12:

     Normalize attention weights: αt=softmax(et)

  • 13:

     Compute context vector as a weighted sum of hidden states: ct=i=0T1αi·HBiLSTMti

  • 14:

     Concatenate context vector with current BiLSTM hidden state: hatt_t=[ct;HBiLSTMt]

  • 15:

    end for

  • 16:

    Gated Recurrent Unit (GRU) Layer:

  • 17:

    for (t=0,1,,T1) do

  • 18:

     GRU forward step: Compute update gate zt, reset gate rt, candidate hidden state h˜t

  • 19:

     Update hidden state: htGRU using hatt_t as input

  • 20:

    end for

  • 21:

    Dense Layer:

  • 22:

    Perform linear transformation: z=htGRU·Wdense+bdense

  • 23:

    Apply activation function: y=softmax(z)

  • 24:

    Training, Evaluation, and Prediction:

  • 25:

    Train model using backpropagation and optimization algorithm

  • 26:

    Evaluate model performance using appropriate metrics

  • 27:

    Make predictions on new sequential data