arXiv:2610.08066cs.CROct 2026

Manuscript Offprint

arXiv:2610.08066

Systematically Optimized CNN-Transformer with Focal Loss for Imbalanced Intrusion Detection on NSL-KDD

Kartick Sutradhar, Ranjitha Venkatesh

Abstract

Intrusion Detection Systems (IDS) struggle with imbalanced datasets like NSL-KDD, especially in detecting rare R2L and U2R attacks. This work describes a systematically optimized and explainable framework using a CNN-Transformer architecture to improve performance on highly imbalanced data. We decided to use XGBoost for feature selection and Focal Loss as the main mechanism for minority class learning. We used Optuna for end-to-end hyperparameter optimization (finding top_k=35, heads=4, ff_dim=256, learning rate=0.00042, batch size=256, and Focal Loss γ\gamma), using a thoughtful data splitting process to prevent data leakage. Critically, our ablation studies pointed out that while Focal Loss (optimally at γ=1.5\gamma = 1.5) substantially enhanced minority recall, adding oversampling methods like SMOTE caused precision degradation via over-correction. Our final model, using only tuned Focal Loss, achieved 98.73% overall accuracy on the NSL-KDD test data, with significantly balanced F1-scores of 84.63% (R2L) and 69.66% (U2R) over baseline approaches. Additionally, we performed SHAP analysis to understand model predictions, understand salient features (e.g., service_http, logged_in), and explain the persistent U2R precision challenge due to feature overlap. This study presents a comprehensive pipeline for imbalanced IDS, providing methodological insights, and demonstrates that tuned Focal Loss can be a sufficient and effective strategy for class balancing.

Keywords Intrusion Detection System · Hyperparameter Optimization · CNN-Transformer · Deep Learning

Corresponding author: Kartick Sutradhar

Indian Institute of Information Technology Sri City, India

E-mail: kartick.sutradhar@gmail.com

Ranjitha Venkatesh

Gandhi Institute of Technology and Management,

Bengaluru, Karnataka, India

Kartick Sutradhar, Ranjitha Venkatesh

1 Introduction

The increased dependence on digital networks in every part of society has resulted in rapid progress, but it has also exposed critical infrastructures to an increasing number of sophisticated cyber threats. Intrusion Detection Systems (IDS) act as a basic layer of defense, monitoring network traffic for malicious activity or policy violation, a consideration that is at the core of contemporary network security [1]. IDS are vital in the availability, integrity, and confidentiality of networks. Recently, Machine Learning (ML) and Deep Learning (DL) methods have gained traction in developing intelligent IDS that can detect attack patterns that signature-based methods alone may miss [2] and [3]. Despite advancements in intrusion detection systems (IDS) methods, many state-of-the-art models are hindered when deployed on realistic networks and faced with very imbalanced datasets. The NSL-KDD dataset [4] is an example of a often-used benchmark derived from the original KDD Cup 1999 dataset, and while NSL-KDD addresses a few of the issues from KDD Cup, it remains heavily precipitation. For example, normal traffic and common attacks Denial-of-Service (DoS) are overwhelmingly present while detecting critical and more subtle attacks Remote-to-Local (R2L) and User-to-Root (U2R) is a challenge. This often leads to overall system accuracy very high because once the model has classified the majority attack types correctly, it does so incorrectly for the less frequent minority attack types. As a result, overall accuracy becomes meaningless and misrepresents the system's capability to identify meaningful breaches in system security. Research that attempts to mitigate class imbalance typically uses techniques based on SMOTE [5] and its variations [6, 7]. Unfortunately, these techniques are often used ad-hoc, and there is limited understanding of how they interact with the loss function. In all of this, there's also an increase in the use of Deep Learning methods that are increasingly complex, leading to the use of opaque "black boxes," which are not conducive to the explainability or trust needed in security scenarios.

This study presents a systematically optimized and interpretable framework for intrusion detection on imbalanced datasets, using NSL-KDD as a case study. This framework combines several significant components: (1) Intelligent feature selection using XGBoost to reduce noise and dimensionality to the set of features created. (2) A hybrid CNN-Transformer architecture specifically designed to learn both local spatial patterns, as well as long-range dependencies in the network traffic data. (3) A selective exploration of Focal Loss as the main method to deal with class imbalance at the model level. (4) An extensive hyperparameter optimization of the entire pipeline using Optuna for reproducibility and locating the best settings. (5) Implementation of Explainable AI (XAI) methods, specifically SHAP, to provide explanations for model predictions and explanations of feature importance for specific variable contributions, especially in relation to the challenging minority classes. The novelties of this work can be shown as follows.

– A presentation of a resilient method of developing deep learning based IDS, highlighting the importance of accurately handling your data to avoid data leakage.

– An insightful examination of the interaction between Focal Loss and data oversampling (SMOTE), showing that Focal Loss tuned on its own can outperform combined techniques in specific optimized contexts, avoiding problems associated with over-correction.

– The implementation of systematic hyperparameter optimization (Optuna) to simultaneously tune the feature selection approach, model architecture, and loss function parameters for improved balanced performance.

– The implementation of SHAP to obtain fine-grained explanations for model behaviour, specifically providing understanding of feature importance and the issues faced, specifically in detecting rare attack classes such as U2R.

Our model obtains high overall performance on NSL-KDD, and considerably improves F1 scores for R2L and U2R critical classes versus baseline approaches without specialized methods to address imbalance. The XAI analysis adds clarity to the strengths of the model and the challenges in distinguishing certain rare attacks.

This paper is organized as follows. Section II reviews related work in IDS, class imbalance techniques, and explainability. Section III explains our proposed methodology, including data preprocessing, model architecture, optimization strategy, and evaluation metrics. Section IV presents the experimental results, including ablation studies and comparisons. Section V discusses the findings, including the crucial SHAP analysis. Finally, Section VI concludes the paper and projects potential future work.

References (47)