ST-GAT: A Leakage-Free Spatiotemporal Graph Attention Network for Improved Intrusion Detection

ST-GAT eliminates evaluation bias via non-overlapping windows and independent preprocessing, significantly improving intrusion detection via spatiotemporal graph modeling.
The study highlights temporal leakage and preprocessing contamination in intrusion detection evaluation and proposes ST-GAT, which models traffic as graph snapshot sequences using k-NN edges, multi-head GAT, and BiLSTM with temporal attention. With non-overlapping windows and training-only preprocessing, it achieves 96.65% Macro-F1 and 99.14% accuracy on CSE-CIC-IDS-2018, 14.3 points above a per-flow random forest baseline; balanced sampling reduces Macro-F1 by 2.3 points, showing loss weighting is more robust.
Intrusion detection systems are a core component of modern network defense. Deep learning models have reported strong results on anomaly detection tasks, but evaluations on standard benchmarks such as CSE-CIC-IDS-2018 are often inflated by two methodological issues: temporal leakage caused by overlapping sliding windows, and data contamination from fitting preprocessing before the train/test split. In addition, per-flow classifiers ignore the spatiotemporal dependencies between network flow sequences.
To address these issues, researchers propose the Spatiotemporal Graph Attention Network (ST-GAT), which represents traffic as a sequence of graph snapshots. The framework constructs k-nearest-neighbor (k-NN) edges within each window, applies multi-head Graph Attention Network (GAT) layers for spatial aggregation, and uses a bidirectional long short-term memory network (BiLSTM) with temporal attention for sequence modeling. To prevent leakage, non-overlapping temporal windows are used (stride equal to window length), and all preprocessing is fitted only on the training set.

Temporal Leakage and Preprocessing Contamination: Overlooked Evaluation Pitfalls
In deep learning research on intrusion detection, the way time-series data is split has a decisive impact on the credibility of results. When overlapping sliding windows are used, the training and test sets share some time steps, and the model may "memorize" local patterns of test samples, thereby inflating metrics. Likewise, if global data is standardized or feature-selected before splitting the dataset, statistics from the test set leak into the training process, causing preprocessing contamination. ST-GAT's design explicitly avoids these two pitfalls: non-overlapping window splitting eliminates temporal overlap, and preprocessing fitted independently on the training set cuts off the information leakage path. This rigorous experimental setup makes the reported performance metrics more meaningful as a reference.
Spatiotemporal Graph Modeling: From Per-Flow Classification to Graph Snapshot Sequences
Traditional per-flow classifiers process each network flow independently, losing the spatial correlations and temporal evolution information between flows. ST-GAT models traffic over a period as a sequence of graph snapshots: within each time window, edges between nodes (flows) are constructed using k-NN to capture spatial relationships between similar flows; multi-head GAT layers aggregate neighbor information to generate a spatial representation for each node; then a BiLSTM processes the graph snapshot sequence along the temporal dimension and, through a temporal attention mechanism, focuses on key time steps. This "spatial aggregation + temporal modeling" architecture enables the model to simultaneously leverage intra-flow features, inter-flow topology, and temporal dynamics.

k-Nearest-Neighbor (k-NN) graph construction: In ST-GAT, network flows within each time window are treated as graph nodes, and the k-NN algorithm connects each node to its k nearest neighbors in feature space to form edges. This method requires no predefined graph structure and can adaptively capture similarity relationships between flows, serving as the foundation for constructing dynamic graph snapshots.
Multi-head Graph Attention Network (GAT): GAT uses an attention mechanism to assign different weights to each node's neighbors, aggregating neighbor information to update node representations. The multi-head mechanism runs multiple attention heads in parallel, enhancing the model's ability to capture diverse relationships and improving the robustness of spatial aggregation.
Bidirectional Long Short-Term Memory Network (BiLSTM) with temporal attention: BiLSTM processes sequence data from both forward and backward directions, capturing context dependencies. The temporal attention mechanism dynamically assigns weights to different time steps in the sequence, enabling the model to focus on key time points and effectively model the temporal dynamics of graph snapshot sequences.
Comparison of Class Imbalance Strategies: Loss Weighting Outperforms Resampling
Network traffic data naturally exhibits severe class imbalance, with attack samples far fewer than normal traffic. The ST-GAT study compared three strategies: inverse class frequency weighting, dynamic balanced sampling, and few-shot metric prototypes. In experiments with three random seeds, ST-GAT with inverse class weighting achieved a Macro-F1 of 96.65% (±0.87) and an accuracy of 99.14%. This is a 14.3 percentage point improvement over the per-flow random forest baseline's 82.34% Macro-F1. Interestingly, balanced sampling reduced Macro-F1 by 2.3 percentage points relative to loss weighting (down to 94.37%). This suggests that aggressive resampling of a small and noisy minority class may actually harm detection performance—in highly imbalanced traffic scenarios, loss weighting is the more robust choice.
Macro-F1: Macro-F1 is the arithmetic mean of the F1 scores for each class, giving equal weight to every class. It is therefore sensitive to performance changes in minority classes and is suitable for evaluating model performance in class-imbalanced scenarios, preventing the majority class from dominating the metric.
Performance Interpretation and Practical Implications
ST-GAT achieves a Macro-F1 of 96.65% and an accuracy of 99.14%, with a standard deviation of only 0.87 across three random seeds, indicating good stability. Compared with the random forest baseline, the 14.3 percentage point improvement in Macro-F1 is mainly attributable to the capture of sequence dependencies through spatiotemporal graph modeling and to the credibility brought by leakage-free evaluation. For practical deployment, the study conveys two messages: first, temporal windows and preprocessing pipelines must be strictly isolated when evaluating intrusion detection models, otherwise comparisons become meaningless; second, for extremely imbalanced traffic data, prioritize class weighting at the loss function level rather than simply oversampling the minority class. These findings provide methodological guidance for building more robust and reproducible intrusion detection systems.
One-line Summary
ST-GAT eliminates evaluation bias through non-overlapping windows and independent preprocessing, and significantly improves intrusion detection performance through spatiotemporal graph modeling.
Paragraph Summary
This study identifies common temporal leakage and preprocessing contamination issues in intrusion detection system evaluation and proposes the Spatiotemporal Graph Attention Network (ST-GAT) framework. ST-GAT represents network traffic as a sequence of graph snapshots, using k-NN to construct edges, multi-head GAT for spatial aggregation, and BiLSTM with temporal attention to model temporal dependencies. Through non-overlapping temporal windows and preprocessing fitted only on the training set, it ensures leakage-free evaluation. Regarding class imbalance, it compares loss weighting, balanced sampling, and few-shot metric prototypes, finding that inverse class weighting achieves 96.65% Macro-F1 and 99.14% accuracy, 14.3 percentage points higher than the per-flow random forest baseline, while balanced sampling instead reduces Macro-F1 by 2.3 percentage points, indicating that loss weighting is more robust. The study provides methodological guidance for building robust and reproducible intrusion detection systems.
Related articles

Abnormal Error Rate Spike in Claude Mythos 5.2 and Fable 5.2
Claude Mythos 5.2 and Claude Fable 5.2 show an abnormal spike in request error rates, marked as investigating on the official status page. This article covers the incident overview, practical impact on developers, and recommended responses.

ReAct Runtime Context Trimming: Why Claude Code Acts at 70%
A deep dive into ReAct runtime context trimming: why Claude Code compresses at ~70% usage, why tool calls dominate, why messages must be dropped in pairs, and why codebases don't need RAG.

AI Agent vs LLM: How Cursor and Claude Code Actually Work
An in-depth analysis of the essential difference between AI Agents and LLMs: LLMs generate code suggestions, while Agents modify files and run tests via tool calls. Using a Spring Boot bug fix as an example, it breaks down the workflows, permission control, and verification mechanisms of Cursor and Claude Code.