Classify the real data using LSTM

Module for evaluating multiple LSTM models (base, robust, batchnorm, attention) on real-world 5G network data, with the option to fine-tune the attention model.

This script includes
  • Definition of a custom attention layer,

  • Real-data class weight computation,

  • Sequence generation for LSTM input,

  • Model accuracy evaluation via classification report,

  • Optional fine-tuning of the attention-based LSTM model.

class lstm_results_real_data.AttentionLayer(*args, **kwargs)

Bases: Layer

Custom attention layer compatible with LSTM outputs. Outputs a weighted sum across the time dimension.

Source: https://www.geeksforgeeks.org/adding-attention-layer-to-a-bi-lstm/?

build(input_shape)

Build the attention layer.

Args
  • input_shape: Shape of the input tensor.

Returns

None

call(x)

Apply the attention mechanism to the input tensor.

Args
  • x: Input tensor of shape (batch_size, timesteps, features).

Returns
  • output: Weighted sum of the input tensor across the time dimension.

compute_output_shape(input_shape)

Compute the output shape of the attention layer.

Args
  • input_shape: Shape of the input tensor.

Returns
  • output_shape: Shape of the output tensor.

lstm_results_real_data.create_sequences(X, y, seq_len=60)

Converts flattened input arrays into sliding window sequences for LSTM input.

Args
  • X (np.ndarray): Input features of shape (samples, features).

  • y (np.ndarray): Target labels corresponding to input samples.

  • seq_len (int): Length of each sequence window. Default is 60.

Returns
  • tuple: (X_seq, y_seq) where X_seq has shape (samples - seq_len, seq_len, features) and y_seq has shape (samples - seq_len,).

lstm_results_real_data.evaluate_model(model, X_seq, y_seq, name)

Evaluates a trained model on given sequential data and prints classification report.

Args
  • model (keras.Model): The trained Keras model to evaluate.

  • X_seq (np.ndarray): Sequential input data (samples, seq_len, features).

  • y_seq (np.ndarray): True class labels.

  • name (str): Name of the model for display purposes.

Returns

None

lstm_results_real_data.load_and_preprocess_data()

Loads the real-world labeled dataset and applies categorical mappings.

Args

None

Returns
  • pd.DataFrame: Preprocessed dataset with mapped categorical columns and timestamps.

lstm_results_real_data.run_evaluation_and_finetuning()

Main function to evaluate four trained LSTM models and fine-tune the attention model using real labeled data.

Args

None

Returns

None