Skip to main content
Scientific Reports logoLink to Scientific Reports
. 2026 Feb 4;16:7217. doi: 10.1038/s41598-026-38668-4

Explainable attention based few shot LSTM for intrusion detection in imbalanced cyber physical system networks

Oluwadamilare Harazeem Abdulganiyu 1,, Oumaima Fadi 1, Youness Moukafih 1, Taha Ait Tchakoucht 2, Yakub Kayode Saheed 3,4, Joshua Ebere Chukwuere 4, Shuaibu Yau 5
PMCID: PMC12923649  PMID: 41639236

Abstract

Intrusion Detection Systems (IDS) play a critical role in securing Cyber-Physical Systems (CPS); however, many existing approaches struggle with imbalanced network traffic, high false positive rates, limited detection accuracy, and insufficient explainability. To address these challenges, this study proposes HeXAI-AttentionCPS, a hybrid Explainable AI–based IDS that combines an attention-enhanced few-shot Long Short-Term Memory (LSTM) network with focal loss and Principal Component Analysis (PCA). The proposed framework is designed to improve intrusion detection performance under severe class imbalance while maintaining model transparency. To enhance interpretability, SHapley Additive exPlanations (SHAP) are employed to provide insights into feature contributions influencing detection decisions. The proposed approach is evaluated using the benchmark ToN_IoT2020 dataset. The experimental results demonstrate that HeXAI-AttentionCPS achieves superior performance in terms of accuracy, precision, recall, and F1-score, while consistently maintaining a low false positive rate compared with state-of-the-art IDS techniques. These findings indicate that the proposed framework offers an effective and interpretable solution for robust intrusion detection in CPS environments.

Keywords: Intrusion detection system (IDS), Imbalanced network traffic, Explainable AI, Attention mechanism, Few-Shot learning, Long Short-Term memory (LSTM), Focal loss

Subject terms: Engineering, Mathematics and computing

Introduction

Cyber-Physical Systems (CPS) integrate computational intelligence with physical processes and communication networks to support critical infrastructures such as smart grids, industrial control systems, healthcare platforms, and intelligent transportation systems. The global CPS market has experienced rapid growth and is projected to expand at an annual rate of 9.7%, reaching approximately $9.6 billion by 20261. Increased interconnectivity and automation have significantly improved operational efficiency and real-time decision-making within CPS environments. However, this growing reliance on networked components has also expanded the attack surface, exposing CPS to a wide range of cyber threats that can compromise system availability, integrity, and safety2. High-profile incidents have demonstrated that successful attacks on CPS can lead to severe physical, economic, and societal consequences, highlighting the need for effective and adaptive security mechanisms.

Intrusion Detection Systems (IDS) have emerged as a fundamental component of CPS security architectures, enabling continuous monitoring of network traffic and early detection of malicious activities3. IDS solutions are commonly classified on the basis of deployment as host-based or network-based and on the basis of detection strategies such as signature-based or anomaly-based systems. Signature-based IDSs are effective at detecting known attack patterns but struggle to identify novel or evolving threats4. In contrast, anomaly-based IDS can detect previously unseen attacks by modeling normal system behavior, making them particularly suitable for dynamic CPS environments. However, anomaly-based approaches often suffer from high false positive rates and limited interpretability, which can hinder their practical adoption5.

Recent advances in machine learning and deep learning have significantly enhanced the detection capabilities of IDS by enabling automated feature learning and improved classification performance. Techniques such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), and Long Short-Term Memory (LSTM) networks have been widely applied to capture spatial and temporal patterns in network traffic. Despite these advances, deep learning-based IDSs faces several persistent challenges when deployed in CPS contexts6. CPS traffic is typically highly imbalanced, with normal traffic and certain attack classes dominating the dataset, whereas critical attack instances occur infrequently. This imbalance often biases learning models toward majority classes, resulting in poor detection of rare but impactful attacks. In practical CPS environments, the challenge is not the absence of data overall, but rather the scarcity of labeled samples for rare and evolving attack types, which motivates the adoption of few-shot learning paradigms at the class level6.

In addition to class imbalance, many deep learning-based IDS operate as black-box models, offering limited insight into how detection decisions are made. The lack of explainability reduces trust and limits the usability of such systems for security analysts and system operators who require transparent and justifiable alerts. Furthermore, conventional supervised learning approaches depend heavily on large volumes of labeled data, which are often unavailable for emerging or zero-day attacks in CPS environments. These limitations collectively highlight the need for intrusion detection frameworks that are not only accurate but also adaptive, robust to data imbalance, and interpretable.

Despite significant progress in intrusion detection research, existing IDS solutions for CPS continue to face persistent limitations. Many approaches struggle to handle severe class imbalance effectively, exhibit limited adaptability to novel or low-frequency attack patterns, and operate as opaque models with insufficient explainability. Although hybrid and learning-based IDS techniques attempt to address some of these issues, they often improve detection performance at the expense of transparency or fail to maintain low false positive rates under realistic CPS traffic conditions7. These unresolved challenges motivate the need for a unified IDS framework that integrates imbalance-aware learning, adaptive detection, and explainable decision-making, thereby improving overall detection performance.

To address these challenges, this study proposed HeXAI-AttentionCPS, a hybrid Explainable Artificial Intelligence-based intrusion detection framework for CPS networks. The proposed approach integrates an attention-enhanced few-shot LSTM model to capture temporal dependencies effectively and adapt to minority attack instances. Focal loss was employed to mitigate class imbalance by emphasizing hard-to-classify minority instances, whereas Principal Component Analysis (PCA) was used to reduce feature dimensionality and improve learning efficiency. To ensure transparency and trustworthiness, SHapley Additive exPlanations (SHAP) was incorporated to provide both global and local interpretations of model decisions.

The proposed framework was evaluated on the benchmark ToN_IoT2020 dataset, which reflects realistic CPS and IoT network traffic scenarios. The experimental results demonstrate that HeXAI-AttentionCPS consistently outperforms existing state-of-the-art IDS techniques in terms of accuracy, precision, recall, and F1-score, while maintaining a low false positive rate. These results highlight the effectiveness of the proposed framework as a robust, adaptive, and interpretable intrusion detection solution suitable for real-world CPS environments. The main contributions of this study are summarized as follows:

  1. We proposed HeXAI-AttentionCPS, a unified intrusion detection framework that systematically integrates attention-enhanced few-shot learning, focal loss, and dimensionality reduction within a single explainable architecture. Unlike existing IDS solutions that address imbalance, adaptability, or interpretability in isolation, the proposed framework jointly addresses these challenges in CPS network environments.

  2. A novel methodological contribution is to combine few-shot LSTM learning with focal loss to address severe class imbalance and limited attack instances simultaneously. This joint formulation enhances the detection of rare and low-frequency attacks, which remains a critical limitation of conventional deep learning-based IDSs.

  3. This study advances explainable intrusion detection by embedding SHAP-based explanations into the detection pipeline, enabling both global interpretability and local interpretability of the model predictions. This integration provides actionable insights into feature contributions, improving transparency, and trust in IDS decisions for CPS operators.

  4. Extensive experiments conducted on the ToN_IoT2020 benchmark dataset demonstrate that the proposed framework consistently outperforms state-of-the-art IDS approaches across multiple evaluation metrics, which include accuracy, precision, recall, F1-score, and false positive rate. The results empirically validate the effectiveness and robustness of the proposed approach under realistic CPS traffic conditions.

The remainder of this paper is organized as follows. Section 2 reviews related work on CPS intrusion detection, focusing on class imbalance, deep learning approaches, and explainable AI. Section 3 describes the proposed HeXAI-AttentionCPS framework. Section 4 presents the experimental setup, evaluation of metrics, performance analysis, ablation study, and comparative results. Section 5 discusses the key findings and their implications, followed by limitations and threats to validity. Section 7 concludes the paper and outlines future research directions.

Literature review

Imbalance-aware machine learning based intrusion detection

Class imbalances are the main challenges in machine learning, particularly in intrusion detection, because they complicate the analysis of highly skewed network traffic groups, where minority class attacks are often underrepresented in network traffic. This problem has led to a large body of work focused on improving how accurately intrusion detection systems (IDSs) identify threats in CPS when dealing with imbalanced network traffic. To address this, the authors8 introduced a framework, that integrates a variational autoencoder (VAE), focal loss (FL), and XGBoost. This hybrid approach enhances the detection of underrepresented intrusions by generating synthetic samples specifically aimed at these minority classes. Evaluations conducted demonstrated that the model outperforms conventional classifiers, achieving 99.67% precision, an F1 score of 94.74%, and a recall of 89.41%. Although the high precision reflects the model’s ability to reduce false alarms, the relatively lower recall suggests that some rare attacks may still be missed. This limitation highlights a key trade-off in synthetic data-driven approaches: while they enhance precision and stability, they may struggle to generalize highly sparse or previously unseen attack patterns. In cyber-physical systems, where even a small number of undetected intrusions can have severe physical and economic consequences, this shortcoming underscores the need for IDS models that can learn robust representations from limited real attack samples rather than relying primarily on data augmentation.

The IE-DBN model proposed in9 employs information gain for feature reduction in conjunction with SMOTE to rebalance the training data, achieving high overall detection accuracy. Nevertheless, its effectiveness decreases when it is applied to large-scale datasets, particularly in identifying minority attack classes, highlighting the scalability limitations of traditional oversampling techniques in highly imbalanced environments. Similarly, researchers10 developed an IDS that combines adaptive synthetic sampling (ADASYN) with LightGBM, reporting accuracy values of 99.86%, 89.79%, and 83.98% on the CICIDS2017, NSL-KDD, and UNSWNB15 datasets, respectively. While ADASYN improved the detection rate of underrepresented attack classes by focusing on difficult-to-learn samples, this gain was accompanied by an increase in false positive rates, indicating a trade-off between minority-class recall and classification stability. Such behavior may reduce the practical applicability of the model in operational CPS environments, where excessive false alarms can hinder timely response.

In11, a comparative evaluation of multiple machine learning classifiers on the UNSW-NB15 dataset demonstrated that Random Forest benefited from SMOTE-based rebalancing, achieving an accuracy of 95.1%. However, the same rebalancing strategy adversely affects the performance of Artificial Neural Networks and Logistic Regression, suggesting that oversampling techniques do not uniformly benefit all classifiers and may introduce bias or noise that degrades model generalizability. Further investigations into refined oversampling approaches include the use of Borderline-SMOTE in IoT intrusion detection scenarios12, where improved detection of rare attack types was observed by concentrating synthetic sample generation near class decision boundaries. Despite its effectiveness in class-sensitive contexts, this approach remains vulnerable to noise and overlapping class distributions. To address these limitations, generative models have been introduced as alternatives. Studies in13 utilized Generative Adversarial Networks (GANs) to synthesize rare attack instances, leading to improved performance of CNN-based IDS models. Building on this14, employed Wasserstein Conditional GANs (WCGANs) to generate minority-class samples, further enhancing the detection rate of rare intrusions via XGBoost classifiers. Although GAN-based methods improve minority-class representation, they often incur significant computational overhead and rely heavily on the quality of generated samples, which may limit their scalability and robustness in real-world CPS deployments.

Further efforts to mitigate class imbalance have focused on more sophisticated sampling and hybrid learning strategies4. introduced the Geometric Synthetic Minority Oversampling Technique (G-SMOTE), which extends conventional SMOTE by incorporating geometric constraints and kernel density estimation to generate more representative synthetic samples. This approach demonstrated strong multiclass classification performance, reporting accuracy values of 86.39%, and 99.94% across different evaluation settings. However, despite its improved sample generation strategy, the G-SMOTE remains a data-level augmentation technique and may still be sensitive to overlapping class distributions and noise in high-dimensional CPS traffic.

To further enhance minority-class recognition15, proposed the Difficult Set Sampling Technique (DSSTE), which leverages the Edited Nearest Neighbor (ENN) to partition training data into “simple” and “difficult” subsets. By reducing dominant class instances through K-Means clustering and selectively increasing the number of samples from underrepresented classes, DSSTE improved detection performance, achieving 96.29% accuracy on the CSECIC-IDS2018 dataset and 80.69% accuracy with a 79.34% F1-score on NSL-KDD. While effective, the method introduces additional computational complexity due to clustering and instance selection, which may limit scalability in large-scale CPS deployments. In a related direction16, reported a modest improvement in precision of approximately 2% for minority-class detection via a semi-supervised learning framework. Their approach integrates Principal Component Analysis (PCA), multiple feature-filtering strategies, and an enhanced Tri-Light Gradient Boosting Machine (Tri-LightGBM) combined with stratified sampling. Although this framework improves robustness by reducing feature redundancy and leveraging unlabeled data, its reliance on a fixed training distribution may constrain adaptability to evolving or previously unseen attack patterns. More recently17, proposed a deep learning-based IDS that addresses class imbalance via Tomek Links and ADASYN, achieving 99.8% accuracy in binary classification and 99.98% accuracy in multi-class scenarios. Despite these high accuracy figures, the approach remains dependent on extensive resampling and does not explicitly address model interpretability or generalizability under extreme data sparsity. Collectively, these studies demonstrate continued progress in imbalance handling but also reveal persistent limitations related to scalability, adaptability, and explainability in CPS-oriented intrusion detection systems.

Most existing intrusion detection systems addressing class imbalance rely on data-level solutions such as oversampling, SMOTE variants, or generative models such as GANs to artificially increase minority class samples. While effective in some scenarios, these approaches often introduce synthetic noise, increase computational overhead, and struggle to generalize previously unseen or evolving attack patterns. In contrast, Few-shot learning (FSL), in contrast, aims to enable models to learn discriminative representations from only a limited number of labeled samples per class. Despite its success in domains such as computer vision and natural language processing, few-shot learning remains underexplored in intrusion detection, particularly in cyber-physical systems. Existing IDS studies largely assume the availability of sufficient labeled attack data, which is unrealistic for rare or zero-day attacks common in CPS environments. This gap highlights the need for IDS frameworks that can generalize from scarce attack samples without relying on aggressive data augmentation, motivating the integration of few-shot learning into deep sequential models for imbalanced network traffic.

Few-shot learning based intrusion detection

Few-Shot Learning (FSL) has emerged as a promising paradigm to address the strong dependence of deep learning–based intrusion detection systems (IDS) on large volumes of labeled data, particularly rare or zero-day attacks. Unlike conventional supervised learning, FSL focuses on learning transferable representations that enable models to generalize from a limited number of labeled samples, typically through episodic training and metric-based classification. This characteristic makes FSL particularly suitable for intrusion detection scenarios characterized by severe class imbalance and data scarcity.

Several studies have explored the application of FSL in IDS contexts. The researcher18 introduced the FS-IDS, an episodic training framework that reduces reliance on large, labeled datasets by learning task-level representations rather than class-specific patterns. While the approach demonstrates competitive performance with significantly fewer malicious samples, its evaluation is largely limited to benchmark and simulated datasets, leaving questions about scalability and robustness in real CPS environments.

In IoT-focused IDS, a CNN–prototypical network architecture has been proposed19 to enable few-shot intrusion detection with limited training data. Although high accuracy is reported, the approach primarily emphasizes overall classification performance, with limited consideration of class imbalance, traffic variability, and computational constraints typical of real-world IoT and CPS deployments. The FS-MCL framework proposed by the researcher20 enhances few-shot intrusion detection by modeling dense feature relationships via mutual centralized learning and Markov-based associations. While strong detection performance is achieved across multiple datasets, the reliance on traffic-to-image transformation and increased model complexity may hinder interpretability and real-time applicability, which are critical requirements in CPS networks.

A few-shot IDS proposed by21 for industrial internet environments integrates a 1D-CNN encoder with attention mechanisms within a prototypical network to improve discrimination under data-scarce conditions. Although the method improves the detection of unseen attacks, its evaluation is confined to simulation datasets, and its performance remains moderate compared with that of recent deep learning–based IDS solutions. More recently, researchers22 introduced BFS-NID to extend few-shot learning to a class-incremental intrusion detection setting by leveraging a vision transformer and self-supervised feature learning. While effective for incremental attack detection, the computational overhead associated with transformer-based architectures may limit practical deployment in resource-constrained or latency-sensitive CPS environments.

The existing few-shot learning-based IDS approaches demonstrate the potential of FSL for addressing data scarcity and novel attack detection. However, most studies focus primarily on detection performance, with limited attention to severe class imbalance, temporal modeling of CPS traffic, explainability, and deployment efficiency. These limitations motivate the proposed HeXAI-AttentionCPS, which integrates episodic few-shot learning with attention-enhanced temporal modeling, focal loss for imbalance mitigation, and SHAP-based explainability within a unified CPS-oriented IDS framework.

Explainable AI-based intrusion detection

Explainable Artificial Intelligence (XAI) has emerged as a critical requirement for IDS deployment in safety-critical CPS environments, where security decisions must be interpretable and trustworthy. Model-agnostic explanation techniques such as Shapley Additive explanations (SHAP) and Local Interpretable Model-agnostic Explanations (LIME) have been increasingly adopted to interpret complex machine learning models by quantifying feature-level contributions to predictions7. Among these, SHAP is particularly attractive for IDS because of its solid theoretical foundation, consistency guarantees, and ability to provide both global and local explanations. However, most existing IDS studies either focus solely on improving detection accuracy without interpretability or apply XAI techniques as a post hoc analysis without explicitly addressing class imbalance7.

As summarized in Table 1, very few works simultaneously consider imbalance-aware deep learning and explainability, and even fewer investigate their interaction in CPS-specific intrusion scenarios. This reveals a clear research gap in the design of IDS that is not only accurate under severe class imbalance but also transparent and interpretable. The proposed HeXAI-AttentionCPS framework addresses this gap by jointly integrating few-shot learning, focal loss-based imbalance mitigation, attention mechanisms, and SHAP-based explainability within a unified intrusion detection architecture. An overview of existing approaches is presented in Table 1.

Table 1.

Summary of related work on anomaly-based techniques.

Study Year Dataset Classifier Balancing Technique XAI Limitation
8 2024 NSLKDD; CSE-CIC-IDS2018 XGBoost CWFL-VAE x The lower recall suggests that, although the framework performs well in terms of precision and minimizing false alarms, it may fail to detect a notable number of attacks from underrepresented classes.
17 2023 NSLKDD MLP Tomek Links & ADASYN x Challenges such as overfitting, overlapping class distributions, and high computational demands can hinder the scalability and efficiency of NIDS.
16 2022 UNSW-NB15 and CIC-IDS-2017 GBM Tri-LightGBM x Relying on a fixed training dataset can limit the system’s adaptability to evolving network traffic patterns. Without support for incremental learning, the classifier’s accuracy and performance may decline when exposed to new or unseen data.
9 2021 KDDCup99 DBN SMOTE x Difficulties in detecting minority classes within enormous data.
4 2023 NSL-KDD and N-BaIoT Soft Voting Ensemble G-SMOTE x Minimal precision in identifying minority attack instances and significant computational complexity.
13 2022 KDDCup99, UNSW-NB15 CNN GAN x Although using CNNs can benefit from the 2D picture representation, it may oversimplify network traffic aspects and miss intricate patterns in the data.
20 2025 ISCX-IDS-2012, CICIDS2017, CICIDS2018 FS-MCL x Increased model complexity may hinder interpretability and real-time applicability
21 2024 CIC-IDS 2017 FS 1DCNN x The computational overhead associated with transformer-based architectures may limit practical deployment in resource-constrained or latency-sensitive CPS environments
Propose 2025 ToN_IoT2020 FS-LSTM AM + FL

Methodology

This study introduced HeXAI-AttentionCPS; an anomaly-based IDS designed to mitigate the issues caused by imbalanced network traffic in CPS. The model employs a few-shot LSTM model enhanced by attention mechanisms to focus on critical temporal patterns and improve the detection of anomalies. To rectify the imbalance in network traffic data, the model also integrates focal loss to ensure sensitivity to minority class intrusions while maintaining robust performance for majority classes, reducing false positives while maintaining high accuracy for both unknown and known threats. This section outlines the architectural design, data processing pipeline, and the algorithms employed in developing the HeXAI-AttentionCPS. Figure 1 details how the hybrid model synergizes to create a robust system capable of detecting diverse intrusion types, improving detection accuracy, and ensuring computational efficiency.

Fig. 1.

Fig. 1

Architecture of the proposed HeXAI-AttentionCPS model.

Attention-enhanced few-shot LSTM with focal loss

This section maps out the proposed anomaly-based approach, which is designed for anomaly detection in imbalanced network traffic as depicted in Algorithm 1. The approach integrates the sequential learning strengths of LSTM networks with an attention mechanism that emphasizes important time steps, ensuring that the model concentrates on the most pertinent features of the data. Additionally, the incorporation of focal loss to address class imbalance places more emphasis on rare attack instances. The following subsections detail the integration of these components to achieve the proposed anomaly detection approach.

Algorithm 1.

Algorithm 1

Attention-Enhanced Few-Shot LSTM with Focal Loss.

Data preprocessing

In the preprocessing stage, we applied the min-max normalization technique to scale all the attributes, ensuring that they were on the same scale for consistent modeling. Additionally, we utilized PCA for feature extraction, effectively minimizing the dimensionality of the data while maintaining its most significant variance.

  1. Min-Max Normalization Technique.

Min–Max normalization23 adjusts feature values to a uniform range between 0 and 1. This scaling procedure ensures data consistency and optimizes performance during classification. Equation (1) presents the corresponding formula.

graphic file with name d33e671.gif 1

Here Inline graphic denotes the normalized value of a given feature, whereas Inline graphic and Inline graphic represent the minimum and maximum values of that feature, respectively.

  • b.

    Feature Extraction with PCA.

Principal Component Analysis (PCA) was employed to reduce the dataset’s dimensionality by identifying the principal components that capture most of the data’s variance24. By selecting the top Inline graphic components, we reduce the computational complexity for subsequent tasks while preserving most of the critical information about the patterns in the dataset. This process ensures that the reduced data retain the essential features necessary for differentiating between instances of attacks and normal traffic of the imbalanced network traffic, making it an efficient preprocessing step for the proposed HeXAI-AttentionCPS model.

Given that the data have Inline graphic samples and Inline graphic features. These data were represented as a matrix Inline graphic, where:

graphic file with name d33e724.gif 2

where Inline graphic represents the Inline graphic-th feature value for the Inline graphic-th sample. To ensure that PCA focuses on the variance rather than the mean, the data are centered by subtracting the mean of each feature:

graphic file with name d33e742.gif 3

The centered data Inline graphic are then computed as:

graphic file with name d33e752.gif 4

where:

graphic file with name d33e758.gif

The covariance matrix Inline graphic is then calculated to measure the relationships between features:

graphic file with name d33e768.gif 5

where the diagonal elements of Inline graphic which are symmetric, reflect the variance of individual features, whereas the off-diagonal elements indicate the covariances between feature parts. To find the principal components, the covariance matrix Inline graphic must then undergo eigenvalue decomposition:

graphic file with name d33e782.gif 6

where:

  • Where Inline graphic is the Inline graphic-th eigenvalue of Inline graphic, which represents the variance captured by the corresponding Inline graphic-th principal component.

  • Inline graphic is the corresponding Inline graphic-th eigenvector, indicating the direction of the Inline graphic-th principal component.

The eigenvalues and eigenvectors are computed such that:

graphic file with name d33e827.gif 7

where:

  • Inline graphic is an eigenvalue diagonal matrix.

  • Inline graphic is the eigenvectors matrix.

Next, the eigenvalues Inline graphic are arranged in descending order: Inline graphic. The top Inline graphic eigenvectors corresponding to the Inline graphic-largest eigenvalues are selected to form the projection matrix Inline graphic:

graphic file with name d33e872.gif 8

The choice of Inline graphic depends on the desired amount of variance to retain:

graphic file with name d33e882.gif 9

Finally, the original data are transformed into the lower-dimensional space defined by the top Inline graphic principal components. Therefore, the original data are then projected onto the new Inline graphic-dimensional subspace formed by the selected eigenvectors:

graphic file with name d33e896.gif 10

where:

Inline graphic is the reduced representation of the transformed data with reduced dimensions and each row of Inline graphic represents a sample in the reduced Inline graphic-dimensional space?

Few-shot long short-term memory (LSTM)

LSTM is a special type of recurrent neural network (RNN)25 developed to overcome the problem of long-term dependency in sequential data. It achieves this through gating mechanisms, namely, the forget, input, and output gates, which regulate information flow within the LSTM, enabling selective memory retention and update. Figure 2 provides a summary of the LSTM and attention mechanism.

Fig. 2.

Fig. 2

Component of LSTM and the Attention Mechanism.

The forward propagation process of the LSTM model is defined by the following equation. In this formulation, Inline graphic and Inline graphic represent the model’s hidden and cell states. The sigmoid and hyperbolic tangent activation functions are denoted by Inline graphic and Inline graphic, respectively. The input, forget, and output gates are expressed as Inline graphic, where Inline graphic denotes the weight matrices associated with different peephole connections. The forget gate determines which information from the previous cell state should be discarded, and its output is computed using the equation below.

graphic file with name d33e964.gif 11

In this expression, Inline graphic​ denotes the output of the forget gate, Inline graphic​ represents the hidden state from the preceding time step, and Inline graphic​ corresponds to the current input. The parameters Inline graphic and Inline graphic​ refer to the associated weight matrix and bias term. The next step involves updating the cell state with newly acquired information.

graphic file with name d33e991.gif 12
graphic file with name d33e995.gif 13

In this equation, Inline graphic​ controls the extent to which new information affects the cell state, whereas Inline graphic​ represents the candidate cell state. The updated memory cell state is then obtained by combining the outputs of the forget and input gates.

graphic file with name d33e1009.gif 14

The output corresponding to the current time step was subsequently computed and updated accordingly

graphic file with name d33e1015.gif 15
graphic file with name d33e1019.gif 16

.

The classification engine was the proposed HeXAI-AttentionCPS system’s most vital element, and we integrated it with LSTM. LSTM captures temporal dependencies in network traffic, enabling it to model sequences and uncover correlations between past and present behavior, which is critical for anomaly detection in the CPS network environment26. It can retain historical traffic information while analyzing current data. During network attacks, attackers often perform actions sequentially, so individual traffic events may seem benign or disconnected from previous activities27.

While LSTM is highly effective at modeling sequential dependencies, it is typically not well-suited for detecting rare attack instances, particularly in a highly imbalanced network of traffic. To address this, we integrate few-shot learning into the LSTM framework to enable the model to perform well even with very few labeled samples for minority classes.

Unlike conventional batch-based training, episodic training is adopted to explicitly simulate low-data learning scenarios encountered by minority attack classes in CPS networks. By repeatedly sampling tasks composed of limited support samples and corresponding query sets, episodic training forces the model to learn transferable representations rather than memorizing majority-class patterns. This training strategy is particularly effective in imbalanced intrusion detection, where rare attack instances are insufficient to support traditional supervised learning. As a result, episodic training enhances the model’s ability to generalize to sparsely represented and previously unseen attack patterns without relying on extensive synthetic data generation.

In few-shot learning, the model is exposed to episodic training tasks, where each task consists of a small, labeled support set:

graphic file with name d33e1040.gif 17

In this work, episodic training is constructed such that the support set samples are drawn exclusively from minority attack classes.

graphic file with name d33e1047.gif

Each training episode follows an Inline graphic-way Inline graphic-shot formulation, where Inline graphic denotes the number of minority attack classes and where Inline graphic represents the limited number of labeled samples per class in the support set. In each episode, the support set contains Inline graphicsamples per minority class, where Inline graphic, thereby enforcing a few-shot learning regime at the class level. An unlabeled query set is defined as:

graphic file with name d33e1077.gif 18

where Inline graphic is the number of samples in the query set.

The support set contains a few labeled samples per class, which the model uses to learn how to classify unseen instances from the query set. This approach is particularly useful for rare attack types, where there are very few instances in the training data. During training, the model learns from episodic tasks in which the support set provides the few labeled samples to compute class prototypes:

graphic file with name d33e1089.gif 19

where Inline graphic is the class prototype computed from the attention-weighted representations of the support set. while the query set evaluates the model’s generalization to unseen data, where the model uses the prototypes to classify the query set samples by comparing their representations to the prototypes via a distance metric:

graphic file with name d33e1099.gif 20

where Inline graphic represents the distance between the query sample and the class prototype. From Eq. (20), the negative squared distances are treated as class logits and normalized via a SoftMax function to obtain class posterior probabilities, which are subsequently optimized via focal loss28.

graphic file with name d33e1113.gif 21
graphic file with name d33e1117.gif 22

Consequently, focal loss is applied exclusively to query samples during training, whereas prototype computation remains loss-free, ensuring a clear separation between metric-based representation learning and imbalance-aware optimization.

Attention mechanism

The Attention Mechanism enhances deep learning models by allowing them to focus on the most relevant information in a sequence29. This is analogous to human attention, where the brain prioritizes important details. In network intrusion detection, attention enables the model to emphasize critical time steps in the traffic sequence. The attention mechanism employed in this work follows the Bahdanau-style additive attention formulation, where attention scores are computed via a learnable feedforward compatibility function applied to the LSTM hidden states and normalized via a SoftMax operation. The attention weight Inline graphic for a given time step Inline graphic is computed as:

graphic file with name d33e1139.gif 23

In this context, Inline graphic​ is the hidden state at time Inline graphic, whereas Inline graphic​ corresponds to the learnable weight vector. The calculation of Inline graphic ​ is given by:

graphic file with name d33e1161.gif 24

Here, Inline graphic and Inline graphic​ denote the trainable weight matrix and bias term, respectively. After calculating the attention probabilities at each time step, the weighted feature vector Inline graphic, which encodes information about the network traffic, is computed as follows:

graphic file with name d33e1179.gif 25

Finally, the predicted label is obtained by applying the SoftMax function:

graphic file with name d33e1185.gif 26

Here, Inline graphic and Inline graphic​ denote the weight matrix and bias parameters used for classification, respectively.

The justification for selecting this classifier lies in the LSTM’s ability to effectively learn and combine historical data patterns with real-time network traffic features, enabling accurate classification. Additionally, the attention mechanism enhances this ability by identifying and emphasizing the most critical aspects of network traffic, thereby improving the overall classification performance of the LSTM model30.

Class-wise focal loss (FL)

FL was employed in this study to solve the imbalance issue in anomaly detection8. The loss for each class is defined as:

graphic file with name d33e1213.gif 27

The variable Inline graphic​ indicates the class weight, Inline graphic is the focusing factor, and Inline graphic is the predicted probability corresponding to the true label. The total loss for a batch of size Inline graphic is computed using:

graphic file with name d33e1235.gif 28

where:

graphic file with name d33e1241.gif

Here, the predicted probability Inline graphic corresponds to the SoftMax probability Inline graphic obtained from the prototype-based classification of query samples defined in Eq. (21).

The model weights were updated iteratively during training using mini-batch stochastic gradient descent together with the Adam optimizer. The focal loss was able to improve the model’s focus on minority attack instances. The focal loss formulations in Eqs. (27) and (28) are sufficiently applied to the query-level SoftMax probabilities defined in Eq. (21) during episodic training.

Explainable A.I mechanism

In this research, we employed the Shapley Additive Explanations (SHAP)31 to elucidate the judgment rendered by our proposed HeXAI-AttentionCPS IDS model. SHAP clarifies the predictions of an instance by measuring the role of each feature in the ultimate prediction. The contribution can have either a positive or negative impact32. Equation (36) can be utilized to acquire the SHAP explanation of a given instance:

graphic file with name d33e1290.gif 27

The SHAP formula Inline graphic expresses a model’s prediction as the sum of a baseline value (Inline graphic) and the contributions of individual features (Inline graphic). Here, Inline graphicis the number of features, Inline graphic represents the simplified presence of feature Inline graphic, and Inline graphic quantifies the feature’s impact on the prediction. Positive values of Inline graphic indicate that the feature increases the output, whereas negative values indicate a decreasing effect. This formulation allows the model’s prediction to be interpreted in terms of feature importance, providing a transparent and consistent explanation of complex machine learning models.

Experimental evaluations

This section starts with a summary of the experimental setup, detailing the datasets employed and the evaluation of the metrics used. It then provides a comprehensive analysis of the proposed model’s performance. Finally, an ablation study is presented to highlight the effectiveness of the HeXAI-AttentionCPS approach.

Experimental setup

The HeXAI-AttentionCPS model was simulated on a machine with an Intel Core i7 1.60 GHz processor, 16 GB of RAM, and a 515 GB SSD. Python 3.9 served as the programming environment, and TensorFlow alongside Scikit-Learn was used for model implementation. Table 2 presents the configuration of the parameters used in the proposed model. A series of comprehensive experiments are carried out, in which different combinations of hyperparameters are explored to increase the detection accuracy.

Table 2.

Design principles of proposed HeXAI-AttentionCPS Model.

Parameters Value
FS-LSTM-AM Epochs 150
Layers 7
Batch Size 64
Dropout 0.3
Learning Rate 0.001
Activation Function Tanh (Hidden Layer) and Sigmoid (Output Layer)
Attention Weight Softmax
Optimizer Adam
Batch Size 64
FSL N-Way 4
K-Shot 5
Query 10
Episode 3000
FL Alpha Value 0.25, 0.75 and 0.23, 0.57
Gama Value 1.3, 1.9 and 1.2, 1.7
PCA Num to select 23
Threshold 0.7
Parameter ranking True
Variance 1.932

These settings highlight the flexibility and adaptability of the proposed HeXAI-AttentionCPS model in addressing imbalanced network traffic and effectively detecting both known and unknown intrusions. All reported results correspond to a single deterministic experimental run using fixed random seeds for data splitting and model initialization. This configuration ensures the exact reproducibility of the reported metrics. Owing to the large-scale nature of the dataset and the stability of the training process, repeated runs produced negligible variance and are therefore not reported separately. Episodic training was performed over 3,000 randomly sampled 4-way 5-shot episodes, which were sufficient to ensure stable convergence while preserving the few-shot learning regime for minority attack classes.

Dataset description

The study employed a publicly available subset of the ToN_IoT2020 dataset, which exhibits severe class imbalance33. The dataset was derived in 2020 from a CPS environment that mirrors modern industrial networks providing insights into operational complexities and attack vectors unique to CPS. It combines heterogeneous data sources, including IoT/IIoT telemetry, operating system logs, and network traffic, collected from large-scale, realistic testbeds mimicking modern industrial environments34. It combines heterogeneous data sources, including IoT/IIoT telemetry, operating system logs, and network traffic. The subset used in this study includes normal traffic and nine cyber-attack categories, namely backdoor, XSS, scanning, password, MITM, ransomware, injection, DoS, and DDoS. Its diversity and realism make it invaluable for developing a robust IDS tailored to the unique security challenges of CPS. No additional sampling or class balancing was applied beyond the use of this predefined dataset subset. The classwise distribution of this provider-released subset is reported in Table 3.

Table 3.

Distribution of the ToN_IoT Dataset.

Attack Type Distribution
Backdoor 50,000
Normal 20,000
Password 20,000
Ransomware 20,000
MITM 1043
Scanning 20,000
DoS 20,000
Injection 20,000
DDoS 20,000
XSS 20,000

The ToN_IoT2020 dataset was partitioned into training and testing sets via a stratified splitting strategy to preserve the original class distribution, which is critical given the highly imbalanced nature of CPS network traffic. Specifically, 80% of the data was used for training and 20% were used for testing, with no overlap between the two sets to prevent data leakage. Stratification ensures that minority attack classes are adequately represented during both training and evaluation, enabling a fair assessment of detection performance. To ensure reproducibility, a fixed random seed was applied during data splitting, model initialization, and training.

Although the ToN_IoT2020 dataset is large in total volume, it exhibits extreme class imbalance, where certain attack categories, most notably the MITM class, contain only a small number of labeled samples relative to the majority classes. In this work, the few-shot learning paradigm is applied at the class level rather than the dataset level. Specifically, minority attack classes are treated as few-shot tasks, reflecting realistic CPS environments in which rare or emerging attacks are sparsely labeled. This setting aligns with the fundamental assumption of few-shot learning, where the objective is to generalize effectively from limited labeled samples for specific classes, despite the availability of abundant data for other categories.

Metrics used for performance evaluation

To evaluate the effectiveness of the proposed HeXAI-AttentionCPS model in identifying attacks within imbalanced network traffic in cyber-physical systems (CPS), six performance metrics were utilized as outlined in Table 4. The metrics assess how effectively the model can distinguish normal traffic from anomalies. True positives (TP) represent the number of attack instances correctly detected by the model, whereas false negatives (FN) refer to attack instances that were not accurately identified. True negatives (TN) correspond to normal traffic correctly classified as nonmalicious, whereas false positives (FP) indicate normal traffic that is incorrectly flagged as malicious. This evaluation framework helps determine the model’s reliability and robustness in handling the challenges posed by imbalanced networks in intrusion detection.

Table 4.

Metrics used for performance Evaluation.

Metrics Formula Description
Accuracy Inline graphic The classifier’s ability to correctly distinguish between network data representing an attack or normal activity.
Precision Inline graphic The proportion of correctly identified attack instances among all instances predicted as attacks.
Recall Inline graphic Measures the attack detection capability of the IDS by quantifying the proportion of malicious instances correctly classified.
F1-Score Inline graphic A metric that combines both recall and precision into a single value
Specificity Inline graphic Measures the ability of the IDS to correctly classify normal traffic
FAR Inline graphic Determine the proportion of normal instances that have been incorrectly labeled as attacks by the model.

Result analysis

This section reports the outcomes of the conducted experiments. The performance of the proposed HeXAI-AttentionCPS model was assessed on the ToN_IoT2020 dataset. Furthermore, an ablation study is presented to analyze the contributions of different components of the proposed method.

PCA for dimensionality reduction

The selection of 23 components, as shown in Table 5, is a deliberate strategy grounded in the explained variance of the principal components. The explained variance reflects how well the selected components capture the original data’s critical information, which is crucial for effective dimensionality reduction. The 23 selected components collectively explain 93.2% of the variance, demonstrating that a substantial amount of the dataset information is retained while reducing dimensionality, as illustrated in Fig. 3. This selection strikes a balance between minimizing data complexity and preserving the essential features that contribute to robust intrusion detection. The high percentages of the explained variance indicate a strong retention of informative features, enabling the proposed model to effectively capture critical patterns and anomalies in the datasets. By aligning component selection with the explained variance, this approach enhances model performance by reducing noise and redundancy while maintaining the integrity of significant data attributes, thus contributing to efficient and accurate intrusion detection across diverse network instances.

Table 5.

Explained variance levels of selected components.

Principal Components Relative Variance Principal Components Relative Variance
PC1 0.1424 PC13 0.0236
PC2 0.1335 PC14 0.0236
PC3 0.0882 PC15 0.0220
PC4 0.0509 PC16 0.0209
PC5 0.0494 PC17 0.0201
PC6 0.0469 PC18 0.0194
PC7 0.0446 PC19 0.0171
PC8 0.0419 PC20 0.0147
PC9 0.0403 PC21 0.0132
PC10 0.0361 PC22 0.0125
PC11 0.0311 PC23 0.0110
PC12 0.0291 Summed variance 0.9324 (93.2%)
Fig. 3.

Fig. 3

Percentage of explained variance of features.

Experimental evaluation of the overall performance of the proposed model

The proposed model achieved impressive performance across all traffic classes in the ToN_IoT2020 dataset, showing high accuracy, precision, recall, and F1-scores, even in the presence of class imbalance. In highly imbalanced CPS intrusion detection tasks, aggregate accuracy is often dominated by majority classes; therefore, minority-class behavior and false-positive control provide more meaningful indicators of IDS effectiveness. The model’s robustness is particularly evident in its ability to handle the MITM attack class, which is very challenging due to its rarity and low representation in the dataset. For MITM, the model demonstrated a significant improvement in the recall and F1-score, effectively capturing subtle patterns associated with this class while maintaining a low false alarm rate. The model achieved balanced performance across all the metrics, indicating its capacity to detect these infrequent attacks without compromising precision or introducing excessive false positives. These results highlight the effectiveness of incorporating focal loss and attention mechanisms into the model. These techniques enable the prioritization of learning from underrepresented classes while leveraging advanced feature selection through PCA to reduce noise and emphasize critical features. By addressing the challenges of class imbalance, the proposed model delivers a comprehensive solution capable of accurately detecting both common and rare attack types in the ToN_IoT2020 dataset, depicting the model’s significance in detecting intrusions in CPS imbalanced network traffic as depicted in Table 6.

Table 6.

Multiclass performance of the proposed model (ToN_IoT2020).

Classes Accuracy Precision Recall F1-Score Specificity FAR
Normal 99.9 99.9 99.8 99.8 99.9 0.02
Backdoor 99.7 99.6 99.6 99.5 99.6 0.11
DDoS 99.9 99.8 99.7 99.7 99.6 0.17
DoS 99.9 99.7 99.6 99.7 99.8 0.32
Injection 99.8 99.7 99.7 99.7 99.8 0.35
MITM 97.5 96.9 97.2 97.0 97.0 0.57
Password 99.1 98.9 98.7 98.9 98.9 0.12
Ransomware 99.6 99.6 99.7 99.6 99.7 0.10
Scanning 98.9 98.9 98.7 98.7 98.6 0.27
XSS 99.4 99.1 99.0 99.1 99.3 0.11

Ablation study

We conducted an ablation study, and the results of the experimental analysis are presented in Table 7. Evaluating the various models on the ToN_IoT2020 dataset provided valuable insights into their relative performance. For all ablation experiments, identical training hyperparameters, including the learning rate, optimizer, batch size, and number of training epochs, were used, and only the model components under investigation were varied. The baseline LSTM model demonstrated modest overall performance but faced challenges with recall and F1-score, primarily due to the impact of class imbalance. Introducing the attention mechanism in the AM+LSTM model improved the precision, recall, and F1-score by enabling the model to focus on critical features. Furthermore, incorporating focal loss in the AM+LSTM + FL model significantly enhances the recall and F1-score by effectively addressing the issue of class imbalance, particularly for rare attack instances. The integration of focal loss with LSTM (LSTM + FL) also led to performance improvements, although the absence of PCA limited the model’s ability to select and utilize the most important features efficiently. On the other hand, the PCA+LSTM model benefits from dimensionality reduction, achieving better accuracy and recall by reducing noise and emphasizing relevant features. Combining PCA with the attention mechanism in the PCA + AM-LSTM model further increased the overall performance, yielding significant gains in both precision and recall. Finally, the proposed PCA + AM-LSTM + FL model, which incorporates episodic few-shot training in conjunction with focal loss during model optimization, delivered the best overall performance. Compared with standard LSTM and attention-based variants trained via conventional batch learning, the episodic few-shot formulation significantly improved the recall and F1-score for minority attack classes, particularly MITM. This empirical evidence confirms that episodic training plays a critical role in enhancing generalization under severe class imbalance, thereby justifying its adoption within the proposed framework as depicted in Table 7; Fig. 4. The performance gain between PCA + AM+LSTM and PCA + AM+LSTM + FL reflects the combined effect of episodic few-shot training and focal loss, where few-shot learning primarily enhances minority-class recall, whereas focal loss stabilizes optimization under imbalance.

Table 7.

Performance comparison of the proposed model (ToN_IoT2020).

Model Accuracy Precision Recall F1-Score Specificity FAR
LSTM 59.2 58.1 54.2 55.7 57.9 2.81
AM+LSTM 68.7 67.9 67.4 68.0 69.2 2.63
AM+LSTM + FL 83.4 81.2 79.9 80.3 83.2 1.89
LSTM + FL 64.2 59.5 59.3 60.3 63.5 2.43
PCA+LSTM 84.1 81.7 81.9 82.2 83.2 1.67
PCA + AM+LSTM 98.9 97.2 95.7 96.9 98.3 0.51
PCA + AM+LSTM + FL 99.8 99.7 98.8 99.2 99.7 0.01
Fig. 4.

Fig. 4

Multiclass Performance of the Proposed Model (ToN_IoT2020).

The ablation results indicate that PCA provides a substantial improvement in overall performance by reducing feature redundancy and enhancing global class separability. However, the impact of few-shot learning is not primarily reflected in the aggregate accuracy of the metrics. Few-shot learning is designed to improve generalization for minority attack classes, whose influence on overall accuracy is limited due to severe class imbalance. As observed in the classwise results in Table 6, the few-shot learning component contributes to stabilizing the detection performance for underrepresented attacks such as MITM, complementing PCA’s global feature optimization role. These results demonstrate that PCA enhances global feature representation, whereas few-shot learning and focal loss jointly improve robustness for minority attack detection and false-positive control under severe class imbalance.

SHAP explainable artificial intelligence

SHAP explanations were generated via the model-agnostic kernel SHAP approach. Owing to the sequential nature of the LSTM-based architecture, the input sequences were flattened prior to explanation, allowing SHAP to quantify feature-level contributions to the final detection decision. While this approach does not provide fine-grained temporal attribution, it offers reliable post-hoc interpretability for complex attention-enhanced models without requiring gradient access. All explanations were generated via the trained attention-enhanced LSTM model.

The color gradients denote the direction in which the feature affects the prediction. The SHAP value plot in Fig. 6 shows an in-depth interpretability framework for our machine learning model designed to detect cyber intrusions via the ToN_IoT2020 data. This global explanation provides a transparent breakdown of how individual features influence the model’s predictions across various attack classes. The key components of the SHAP plot are described as follows:

Fig. 5.

Fig. 5

Performance Evaluation of the Proposed Model (ToNs_IoT2020).

  1. Feature Importance (Y-Axis): The y-axis lists features in descending order on the basis of their mean SHAP values, indicating their overall contribution to the model’s decisions. Features such as dst_port, src_port, src_ip, and dst_ip dominate the ranking, indicating that port activity and IP addresses are critical for intrusion classification in CPS network environments.

  2. SHAP Value Magnitude (X-Axis): The x-axis represents the mean absolute SHAP value, which quantifies each feature’s average influence on the model output. The longer the bar is, the more important the feature is in shaping predictions. For instance, dst_port has the highest SHAP value, highlighting its strong influence on detecting various forms of network behavior or anomalies.

  3. Class-Specific Feature Contributions (Color Coding): Each bar is color-coded according to attack class labels (e.g., Class 0–9), allowing us to visualize how different features contribute to predictions for specific intrusion categories. For example, dst_port is heavily influential for Class 6 (blue) and Class 9 (pink), whereas src_port contributes across multiple classes including Class 7 (red) and Class 8 (green). This multiclass support underscores the feature’s discriminative power across diverse attack scenarios.

  4. Dominant Features in CPS Intrusion Detection: The analysis reveals the following:

  1. Network endpoint features such as dst_port, src_port, src_ip, and dst_ip are vital. Their high SHAP values suggest that attack vectors often exploit specific IP-port combinations in CPSs.

  2. Temporal and traffic volume indicators such as duration, src_ip_bytes, and dst_ip_bytes are also prominent, reflecting that timing and byte-level behaviors can distinguish between benign and malicious activities.

  3. Connection state features (e.g., conn_state_S0, conn_state_REJ) play a significant role in identifying dropped, rejected, or suspicious connections.

  4. Protocol and service-related fields such as service_http and dns_query have moderate but class-specific importance, reinforcing the contextual relevance of protocol usage in attack patterns.

  • 5.

    Cross-Class Feature Relevance: Many features have bars segmented into multiple colors, which implies that they contribute to the classification of multiple attack types. For example, src_port and dst_ip have multicolored distributions, indicating that their predictive utility is not restricted to a single intrusion category. This cross-class influence is essential for building robust multiclass intrusion detection systems in heterogeneous IoT networks.

The SHAP summary visualization for the ToN_IoT2020 data demonstrates that network port activity, IP address patterns, and connection states are the most influential predictors for detecting intrusions in CPS network environments. By leveraging SHAP-based interpretability, this analysis enhanced our understanding of the model behavior, ensured trustworthiness, and guided feature engineering for improved detection accuracy.

Fig. 6.

Fig. 6

SHAP Global explanation on the ToN_IoT2020.

Comparison with state-of-the-art techniques

Table 8 provides a qualitative comparison between the proposed HeXAI-AttentionCPS framework and representative intrusion detection approaches reported in the literature that address class imbalance in network traffic. Importantly, that the compared studies were evaluated on heterogeneous datasets and under distinct experimental settings. Therefore, the reported performance metrics should not be interpreted as direct quantitative comparisons or as evidence of absolute superiority. Instead, this comparison is intended to contextualize the proposed approach within existing research trends, highlighting differences in modeling strategies, imbalance handling mechanisms, and the inclusion of explainability components. As summarized in Table 8, many existing works achieve high detection performance but do not incorporate explainable AI (XAI) mechanisms, which can limit their practical deployment in cyber-physical systems where transparency and trust are critical. The results of the proposed HeXAI-AttentionCPS model, which is evaluated exclusively on the ToN_IoT2020 dataset, demonstrate that it achieves competitive performance while simultaneously addressing class imbalance, temporal dependency modeling, and explainability within a unified framework. The inclusion of SHAP-based explanations distinguishes the proposed approach from several prior methods, providing additional interpretability without compromising detection effectiveness. Furthermore, while many prior works rely on data augmentation techniques to mitigate class imbalance, this study adopts an imbalance-aware learning strategy based on episodic few-shot training and focal loss, without modifying the underlying dataset distribution.

Table 8.

Comparison of the proposed HeXAI-AttentionCPS with existing works.

Models Accuracy Precision Recall F1-Score Specificity FAR XAI
8 99.79 99.67 89.41 94.74 99.59 0.07 x
17 99.8 97.0 97.0 97.0 N/A - x
4 83.39 73.62 70.22 71.49 N/A - x
HeXAI-AttentionCPS 99.8** 99.7* 98.8* 99.2* 99.7* 0.01*

Discussion of findings

The evaluation of the HeXAI-AttentionCPS demonstrates its effectiveness in addressing the persistent challenges of IDS design for CPS with imbalanced traffic. Unlike traditional oversampling-based approaches as established in the literature9,10,12, which improve recall at the cost of more false alarms, the HeXAI-AttentionCPS model integrates focal loss and attention to achieve balanced performance across accuracy, recall, and false positive rates. In particular, the improved recall for minority attack classes such as MITM shows that few-shot learning combined with focal loss can effectively mitigate the limitations of GAN or SMOTE-based approaches13,17. Moreover, by incorporating SHAP, the model provides interpretability, a dimension largely absent in previous IDS frameworks4,8,16. This aligns with recent calls in the literature for IDSs that are both accurate and explainable, ensuring trust and adoption in real-world CPSs.

Crucially, the addition of SHAP-based explainability extends this contribution beyond accuracy metrics by addressing one of the most pressing barriers to IDS adoption: the lack of transparency in deep learning models. While SHAP itself is a well-established interpretability tool, its application within the HeXAI-AttentionCPS framework is distinctive for three reasons. First, it clarifies how the proposed hybrid model detects rare and complex attack classes, such as MITM, by revealing which traffic features drive these decisions. Second, it bridges the gap between high-performing but opaque anomaly based systems and the operational need for trust and accountability in real-world CPS environments. Third, by combining SHAP with attention-enhanced few-shot LSTM and focal loss, the system achieves not only balanced detection performance but also interpretable outputs, providing security analysts with actionable insights. This integration marks a shift from explainability as an afterthought to explainability as a core design principle of IDS. Unlike prior works that evaluated interpretability in isolation, HeXAI-AttentionCPS demonstrates that explainability can coexist with improved accuracy, low false positives, and computational efficiency, making the model more practical for deployment in modern CPS settings.

The results also reinforce the importance of dimensionality reduction; PCA not only reduces computational overhead but also enhances the attention mechanism’s ability to focus on relevant traffic patterns, echoing findings from semi supervised frameworks16. Collectively, these contributions position the HeXAI-AttentionCPS as a significant advancement over existing IDS models, offering both technical robustness and practical usability.

Limitations, challenges, and recommendations

Resource overhead

Although the proposed HeXAI-AttentionCPS achieves high detection accuracy and interpretability, the integration of multiple components, such as attention mechanisms, focal loss, PCA, and SHAP explainability may introduce additional computational and memory demands. This may limit deployment on resource-constrained devices such as IoT edge nodes, where lightweight IDS solutions are often needed. Future optimization should explore model compression, pruning, or hardware-aware tuning to reduce the overhead without compromising the detection quality.

Scalability and performance tradeoff

While focal loss effectively mitigates class imbalance, its performance may degrade under extreme imbalance conditions, where minority attack instances are exceedingly rare. Similarly, real-time scalability remains a challenge: the combination of attention and SHAP-based feature attribution, although valuable for interpretability, increases the inference time. For large-scale or high-speed CPS networks, further work is needed to strike a balance between explainability, throughput, and latency.

Threat to validity

A few threats to the validity of the proposed HeXAI-AttentionCPS are outlined below.

Internal validity

Model performance is dependent on the quality and representativeness of the ToN_IoT2020 dataset. Although this dataset captures diverse CPS attack scenarios, real-world environments may introduce additional complexities such as encrypted traffic or evolving attack vectors. There is also a risk of overfitting when multiple components are integrated.

External validity

The results obtained in controlled experimental settings may not be fully generalizable to operational CPS environments. Diverse protocols, adaptive adversaries, and dynamic network topologies can affect performance.

Construct validity

The evaluation emphasizes classical performance metrics (accuracy, precision, recall, F1, FAR). While these provide a strong baseline, additional measures such as computational latency, memory footprint, and interpretability of usability for human operators should also be considered in future assessments.

Recommendations

Future research should focus on optimizing the HeXAI-AttentionCPS framework for real-world deployment as follows:

  1. The model is lightweighted through pruning, quantization, or knowledge distillation to enable operation on constrained devices.

  2. Enhancing scalability by parallelizing SHAP computations or exploring approximate explainability methods that reduce latency while retaining interpretive power.

  3. Extending validation to real CPS deployments with encrypted traffic, adaptive attackers, and live streaming data to strengthen external validity.

  4. Human-centered evaluation, assesses whether SHAP-based explanations improve analysts’ ability to detect, interpret, and respond to intrusions in operational environments.

Conclusions and future work

This research proposed and evaluated HeXAI-AttentionCPS, a hybrid anomaly based IDS that combines attention-enhanced few-shot LSTM, focal loss, PCA, and SHAP explainability to address the challenges of imbalanced CPS traffic. The experimental results on the ToN_IoT2020 dataset confirmed that the model not only achieves high accuracy and recall for both common and rare attacks but also maintains a consistently low false positive rate. A key contribution to this work lies in demonstrating that explainability can be seamlessly embedded within a high-performing IDS. By integrating SHAP into the detection pipeline, the HeXAI-AttentionCPS provides interpretable outputs that clarify feature importance across attack classes, enhancing trust and adoption potential. Unlike existing IDS approaches that treat interpretability and detection performance separately, this study shows that a unified design can deliver both. Future work will explore optimizing this framework for resource-constrained edge environments and extend the explainability mechanism to capture temporal reasoning within traffic sequences. This will further strengthen the practicality and transparency of IDSs in complex CPS ecosystems.

Author contributions

O.H.A was involved in conceptualization, investigation, methodology, validation, formal analysis, data curation, visualization, software, writing, and editing. O.F., Y.M. and S.Y. helped with editing and writing, visualization. T.A helped in conceptualization, resources, supervision, validation, formal analysis, and editing. Y.K.S and J.E.C contributed to validation, formal analysis, and editing.

Data availability

The ToN\_IoT2020 dataset used in this study is publicly available and can be accessed from the original source at [https://research.unsw.edu.au/projects/toniot-datasets](https:/research.unsw.edu.au/projects/toniot-datasets), whereas the exact subset used for evaluation can be accessed at [http://datasets.nyx.ca:8081/datasets/TON\_IoT/](http:/datasets.nyx.ca:8081/datasets/TON_IoT).

Code availability

The code used in this study is publicly available at: https://github.com/harazeem/HeXAI-AttentionCPS. A permanent archived version of the code can also be accessed via Zenodo at: 10.5281/zenodo.18232377.

Declarations

Competing interests

The authors declare no competing interests.

Footnotes

Publisher’s note

Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

References

  • 1.Luo, Y., Xiao, Y., Cheng, L., Peng, G. & Yao, D. D. Deep Learning-based anomaly detection in Cyber-physical systems. ACM Comput. Surv. (CSUR). 54, 1–36 (2021). [Google Scholar]
  • 2.Ferrari et al. P. Performance evaluation of full-cloud and edge-cloud architectures for Industrial IoT anomaly detection based on deep learning. In IEEE International Workshop on Metrology for Industry 4.0 and IoT (MetroInd4.0&IoT), 420–425 (Naples, Italy, 2019). 10.1109/METROI4.2019.8792860
  • 3.Abdulganiyu, O. H., Ait Tchakoucht, T. & Saheed, Y. K. A systematic literature review for network intrusion detection system (IDS). International J. Inform. Security, 22, 5, pp. 1125–1162, 2023/10/01 2023, 10.1007/s10207-023-00682-2
  • 4.Yang, Y., Gu, Y. & Yan, Y. Machine Learning-Based Intrusion Detection for Rare-Class Network Attacks, Electronics, vol. 12, no. 18, p. 3911, [Online]. (2023). Available: https://www.mdpi.com/2079-9292/12/18/3911
  • 5.Kayode Saheed, Y., Harazeem Abdulganiyu, O. & Ait Tchakoucht, T. A novel hybrid ensemble learning for anomaly detection in industrial sensor networks and SCADA systems for smart City infrastructures. Journal King Saud Univ. - Comput. Inform. Sciences, 35, 5, p. 101532, 2023/05/01/ 2023, doi: 10.1016/j.jksuci.2023.03.010
  • 6.aheed, Y. K., Abdulganiyu, O. H., Majikumna, K. U., Mustapha, M. & Workneh, A. D. Resnet50-1d-cnn: A new lightweight resnet50-one-dimensional convolution neural network transfer learning-based approach for improved intrusion detection in cyber-physical systems. Int. J. Crit. Infrastruct. Prot.45, 100674 (2024).
  • 7.Saheed, Y. K. & Chukwuere, J. E. XAIEnsembleTL-IoV: A new eXplainable artificial intelligence ensemble transfer learning for zero-day botnet attack detection in the internet of vehicles. Results Engineering, 24, p. 103171, 2024/12/01/ 2024, doi: 10.1016/j.rineng.2024.103171
  • 8.Abdulganiyu, O. H., Tchakoucht, T. A., Saheed, Y. K. & Ahmed, H. A. XIDINTFL-VAE: XGBoost-based intrusion detection of imbalance network traffic via class-wise focal loss variational autoencoder. The J. Supercomputing, 81, 1, p. 16, 2024/10/17 2024, 10.1007/s11227-024-06552-5
  • 9.Jia, H., Liu, J., Zhang, M., He, X. & Sun, W. Network intrusion detection based on IE-DBN model. Computer Communications, 178, pp. 131–140, 2021/10/01/ 2021, doi: 10.1016/j.comcom.2021.07.016
  • 10.Liu, J., Gao, Y. & Hu, F. A fast network intrusion detection system using adaptive synthetic oversampling and LightGBM. Computers & Security, 106, p. 102289, 2021/07/01/ 2021, doi: 10.1016/j.cose.2021.102289
  • 11.Hafiza Anisa, A., Anum, H. & Narmeen Zakaria, B. Network intrusion detection using oversampling technique and machine learning algorithms. PeerJ Comput. Sci.810.7717/peerj-cs.820 (2022). [DOI] [PMC free article] [PubMed]
  • 12.Zhang, Y. & Liu, Q. On IoT intrusion detection based on data augmentation for enhancing learning on unbalanced samples, Future Gener. Comput. Syst., vol. 133, no. C, pp. 213–227, (2022). 10.1016/j.future.2022.03.007
  • 13.Andresini, G., Appice, A., Rose, L. D. & Malerba, D. GAN augmentation to deal with imbalance in imaging-based intrusion detection. Future Gener Comput. Syst.123, 108–127 (2021). [Google Scholar]
  • 14.Kumar, V. & Sinha, D. Synthetic attack data generation model applying generative adversarial network for intrusion detection. Comput. Secur.125, 15. 10.1016/j.cose.2022.103054 (2023). [Google Scholar]
  • 15.Liu, L., Wang, P., Lin, J. & Liu, L. Intrusion detection of imbalanced network traffic based on machine learning and deep learning. IEEE Access.9, 7550–7563. 10.1109/ACCESS.2020.3048198 (2021). [Google Scholar]
  • 16.Li, J., Zhang, H., Liu, Y. & Liu, Z. Semi-supervised machine learning framework for network intrusion detection. The J. Supercomputing, 78, 11, pp. 13122–13144, 2022/07/01 2022, 10.1007/s11227-022-04390-x
  • 17.Abdelkhalek, A. & Mashaly, M. Addressing the class imbalance problem in network intrusion detection systems using data resampling and deep learning. The J. Supercomputing, 79, 10, pp. 10611–10644, 2023/07/01 2023, 10.1007/s11227-023-05073-x
  • 18.Yang, J., Li, H., Shao, S., Zou, F. & Wu, Y. FS-IDS: A framework for intrusion detection based on few-shot learning. Computers & Security, 122, p. 102899, 2022/11/01/ 2022, doi: 10.1016/j.cose.2022.102899
  • 19.Althiyabi, T., Ahmad, I. & Alassafi, M. O. Enhancing IoT Security: A Few-Shot Learning Approach for Intrusion Detection, Mathematics, vol. 12, no. 7, p. 105510.3390/math12071055
  • 20.Xu, C., Zhang, F., Yang, Z., Zhou, Z. & Zheng, Y. A few-shot network intrusion detection method based on mutual centralized learning, Scientific Reports, vol. 15, no. 1, p. 9848, 2025/03/21 2025. 10.1038/s41598-025-93185-0 [DOI] [PMC free article] [PubMed]
  • 21.Wang, Y., Zhang, Z., Zhao, K., Wang, P. & Wu, R. A few-shot learning based method for industrial internet intrusion detection. Int. J. Inf. Secur.23 (5), 3241–3252. 10.1007/s10207-024-00889-x (2024). /10/01 2024. [Google Scholar]
  • 22.Du, L., Gu, Z., Wang, Y., Wang, L. & Jia, Y. A Few-Shot Class-Incremental learning method for network intrusion detection. IEEE Trans. Netw. Serv. Manage.21 (2), 2389–2401. 10.1109/TNSM.2023.3332284 (2024). [Google Scholar]
  • 23.Georganos, S. et al. Normalization in Unsupervised Segmentation Parameter Optimization: A Solution Based on Local Regression Trend Analysis, Remote Sensing, vol. 10, no. 2, p. 222, [Online]. (2018). Available: https://www.mdpi.com/2072-4292/10/2/222
  • 24.Greenacre, M. et al. Principal component analysis, Nature Reviews Methods Primers, vol. 2, no. 1, p. 100, 2022/12/22 2022. 10.1038/s43586-022-00184-w
  • 25.Hochreiter, S. & Schmidhuber, J. Long Short-Term Memory, Neural Computation, vol. 9, no. 8, pp. 1735–1780, (1997). 10.1162/neco.1997.9.8.1735 [DOI] [PubMed]
  • 26.Abdulganiyu, O. H., Tchakoucht, T. A., Saheed, Y. K., Mouhtadi, M. E. & Alaoui, A. E. H. Modified variational autoencoder and attention Mechanism-Based long Short-Term memory for detecting intrusions in imbalanced network traffic. Secur. Priv.8 (3), e70044. 10.1002/spy2.70044 (2025). [Google Scholar]
  • 27.Saheed, Y. K., Abdulganiyu, O. H. & Tchakoucht, T. A. Modified genetic algorithm and fine-tuned long short-term memory network for intrusion detection in the internet of things networks with edge capabilities. Applied Soft Computing, 155, p. 111434, 2024/04/01/ 2024, doi: 10.1016/j.asoc.2024.111434
  • 28.Mustapha, M. et al. A focal loss and sequential analytics approach for liver disease classification and detection. Healthcare Analytics, 8, p. 100424, 2025/12/01/ 2025, doi: 10.1016/j.health.2025.100424
  • 29.Chorowski, J., Bahdanau, D., Serdyuk, D., Cho, K. & Bengio, Y. Attention-Based Models for Speech Recognition, in NIPS, (2015).
  • 30.Abdulganiyu, O. H., Ait Tchakoucht, T., Alaoui, A. E. H. & Saheed, Y. K. Attention-driven multi-model architecture for unbalanced network traffic intrusion detection via extreme gradient boosting. Intelligent Syst. Applications, 26, p. 200519, 2025/06/01/ 2025, doi: 10.1016/j.iswa.2025.200519
  • 31.Ferraro, A., Galli, A., Moscato, V. & Sperlí, G. Evaluating eXplainable artificial intelligence tools for hard disk drive predictive maintenance. Artif. Intell. Rev.56, 7279–7314 (2022). [Google Scholar]
  • 32.Saheed, Y. K., Omole, A. I. & Sabit, M. O. GA-mADAM-IIoT: A new lightweight threats detection in the industrial IoT via genetic algorithm with attention mechanism and LSTM on multivariate time series sensor data. Sensors International, 6, p. 100297, 2025/01/01/ 2025, doi: 10.1016/j.sintl.2024.100297
  • 33.Zhu, M., Ye, K., Wang, Y. & Xu, C. Z. A Deep Learning Approach for Network Anomaly Detection Based on AMF-LSTM: 15th IFIP WG 10.3 International Conference, NPC 2018, Muroran, Japan, November 29 – December 1, 2018, Proceedings, pp. 137–141. (2018).
  • 34.Moustafa, N., Keshk, M., Debie, E. S. & Janicke, H. Federated TON_IoT Windows Datasets for Evaluating AI-based Security Applications, IEEE 19th International Conference on Trust, Security and Privacy in Computing and Communications (TrustCom), pp. 848–855, 2020. pp. 848–855, 2020. (2020).

Associated Data

This section collects any data citations, data availability statements, or supplementary materials included in this article.

Data Availability Statement

The ToN\_IoT2020 dataset used in this study is publicly available and can be accessed from the original source at [https://research.unsw.edu.au/projects/toniot-datasets](https:/research.unsw.edu.au/projects/toniot-datasets), whereas the exact subset used for evaluation can be accessed at [http://datasets.nyx.ca:8081/datasets/TON\_IoT/](http:/datasets.nyx.ca:8081/datasets/TON_IoT).

The code used in this study is publicly available at: https://github.com/harazeem/HeXAI-AttentionCPS. A permanent archived version of the code can also be accessed via Zenodo at: 10.5281/zenodo.18232377.


Articles from Scientific Reports are provided here courtesy of Nature Publishing Group

RESOURCES