Self-Counterfactual Explainable Detector (SCFED)
- SCFED is a unified detection approach that couples prediction with built-in counterfactual explanations to delineate essential attack logic from benign code.
- It employs graph neural networks, MLP explainers, and Gumbel-Max relaxation to partition semantic relation graphs into factual and counterfactual subgraphs.
- The integrated method boosts robustness, generalizability, and interpretability, making it applicable across security, sequential, and visual domains.
Self-Counterfactual Explainable Detector (SCFED) denotes a detector architecture in which prediction and counterfactual explanation are coupled within the same model or pipeline, rather than delegated to a separate post-hoc stage. The term appears explicitly in the SEASONED framework for detecting Adversarial Exploiter Contracts (AECs), where SCFED classifies semantic relation graphs (SRGs) derived from contract bytecode and generates factual and counterfactual subgraphs that highlight the core attack logic (Ai et al., 6 Sep 2025). In a broader methodological sense, adjacent work supplies the main ingredients of the SCFED paradigm: fine-grained anomalous entry detection by asking which entries must be removed to make a sequence normal (Cheng et al., 2022), self-aware discriminant counterfactual maps that combine predicted-class evidence, counter-class suppression, and confidence (Wang et al., 2020), counterfactual what-if analysis for anomaly detection in multivariate time series (Cummins et al., 2024), and self-explainable models with integrated closed-form counterfactuals in latent space (Haselhoff et al., 2024).
1. Conceptual definition and scope
Within the available literature, SCFED is best understood as a detector that does not treat explanation as an afterthought. In SEASONED, the detector receives an SRG and returns both a binary decision, AEC or benign, and an explanation in the form of subgraphs that separate attack-relevant logic from irrelevant or benign logic. This makes explanation part of the operational output of detection itself, not merely an auxiliary visualization (Ai et al., 6 Sep 2025).
Across related work, counterfactual explanation has a specific semantics. In CFDet, the question is which minimum subset of sequence entries, if removed, would have caused the sequence to appear normal. In SCOUT, the question is which image regions are informative for the predicted class but not for a specified counterfactual class, while also lying in regions of high classifier confidence. In the predictive-maintenance setting, counterfactuals are presented as what-if analyses that ask what changes would move a time window from “anomalous” to “healthy.” In GdVAE, counterfactuals are explicit traversals in latent space toward a user-specified target output. Taken together, these formulations indicate that “self-counterfactual” combines two commitments: the detector must produce its own explanation, and the explanation must be expressed as a minimally altered alternative reality under which the prediction changes (Cheng et al., 2022, Wang et al., 2020, Cummins et al., 2024, Haselhoff et al., 2024).
A plausible implication is that SCFED is not restricted to one data modality. The explicit instantiation in SEASONED is graph-based and security-oriented, but the design principles recur in sequential anomaly detection, visual discrimination, and generative classification.
2. SCFED in SEASONED: graph-based detection and subgraph generation
In SEASONED, the end-to-end pipeline is bytecode SRG SCFED [AEC/Benign, Explanation]. The SRG constructor operates on EVM bytecode without requiring source code. It disassembles bytecode to an opcode sequence, converts it to register transfer language, constructs a control flow graph over basic blocks, and then builds an SRG whose edges encode control relations, data relations, and effect relations. The resulting graph is the input to SCFED (Ai et al., 6 Sep 2025).
The SCFED module itself has three components: a graph neural network encoder , an MLP explainer , and an output layer . The encoder computes node embeddings, the explainer assigns edge importance weights, and the output layer aggregates representations for classification. At the edge level, the formulation given for SEASONED is
where is the edge-node incidence matrix. Edge assignment is then sampled with a Gumbel-Max relaxation:
If , the edge is assigned to the factual subgraph; otherwise it is assigned to the counterfactual subgraph (Ai et al., 6 Sep 2025).
The partition is constrained so that 0 and 1, with the corresponding node sets satisfying 2 and 3. In this construction, the factual subgraph 4 contains edges and nodes essential for malicious classification, whereas the counterfactual subgraph 5 contains edges and nodes which, if removed, would change the classification outcome and are described as irrelevant or benign logic. This decomposition makes the explanatory object structurally aligned with the detector’s input representation (Ai et al., 6 Sep 2025).
3. Counterfactual semantics, losses, and optimization
The operational meaning of explanation in SCFED is not generic feature importance. In SEASONED, explanation is a partition of the input graph into a factual component that is necessary and sufficient for malicious prediction and a counterfactual component that should not, by itself, sustain that prediction. This is enforced through a collection of coupled objectives rather than a single attribution score (Ai et al., 6 Sep 2025).
The sparsity term is explicitly
6
and is used to encourage concise explanations. A counterfactual loss 7 enforces the desired behavior of the factual and counterfactual subgraphs through the malicious probabilities 8 and 9. A classification loss 0 is applied to the final prediction, where the model combines the embedding of the full graph 1 with the embedding of the factual subgraph 2 into 3. A mutual information loss 4, estimated with MINE, reduces redundancy between 5 and 6 so that the factual subgraph contributes informative and less spurious signal (Ai et al., 6 Sep 2025).
Training is bi-level. The outer loop optimizes 7, 8, and 9, while the inner loop optimizes the mutual-information estimator. This places explanation generation inside the learning problem itself. The significance is methodological: SCFED does not merely explain a frozen detector; it uses explanatory structure to improve robustness, generalizability, and data efficiency. SEASONED states that extracting representative information from these explanations further enhances those three properties (Ai et al., 6 Sep 2025).
A useful contrast emerges when SCFED is set beside CFDet. CFDet also learns a decomposition, but in sequential data: the anomalous sequence 0 is split into an anomalous subsequence 1 and a counterfactual normal subsequence 2. Its learning objective combines normality, comprehensiveness, continuity, and sparsity, and because the indicator vector is binary and non-differentiable, it uses policy gradient for training. This suggests that SCFED-like formulations can arise either as graph partitioning or as discrete sequence selection, provided the counterfactual object is structurally embedded in the model (Cheng et al., 2022).
4. Information-theoretic interpretation
SEASONED gives SCFED an explicit Graph Information Bottleneck interpretation. The ideal detector is formulated as
3
where 4 is the label. A tractable lower bound is written as
5
The first term promotes predictive relevance, while the second penalizes redundant overlap between the factual subgraph representation and the full-graph representation (Ai et al., 6 Sep 2025).
The role of this formulation is twofold. First, it justifies using the factual explanation as an active signal for prediction rather than as a passive after-the-fact artifact. Second, it formalizes the intuition that the detector should isolate “core” attack information that is complementary and implementation-agnostic with respect to the full graph representation. SEASONED states that this helps filter out irrelevant or attack-evading patterns while preserving predictive power and explanation fidelity (Ai et al., 6 Sep 2025).
A plausible implication is that SCFED can be read as an information-selection mechanism with causal flavor: it seeks the smallest substructure that still carries the label-relevant content of the input while suppressing decoys. That interpretation is especially natural in security settings, where benign-looking padding or decoy code may obscure the attack path.
5. Precedents and adjacent formulations across modalities
The closest sequential precedent is CFDet, which proposes a framework for fine-grained anomalous entry detection in sequential data. CFDet first detects anomalous sequences with Deep SVDD using an LSTM encoder and a hypersphere in latent space, with anomaly score
6
For a detected anomalous sequence, it then learns an entry detector 7 so that removing the entries marked by the indicator vector 8 yields a counterfactual subsequence 9 that is normal. Its objective encodes normality, comprehensiveness, continuity, and sparsity. The method is explicitly presented as an interpretable machine learning approach to identifying anomalous entries inside anomalous sequences (Cheng et al., 2022).
In computer vision, SCOUT develops self-aware discriminant counterfactual explanations. Given a predicted class 0 and counterfactual class 1, the discriminant heatmap is
2
This combines evidence for the predicted class, suppression of evidence for the counter class, and an attribution map of classifier confidence. SCOUT argues that self-awareness, namely the ability to produce classification confidence scores, is important for the computation of discriminant explanations. It also emphasizes optimization-free computation and a quantitative evaluation protocol based on part or attribute localization (Wang et al., 2020).
The predictive-maintenance work on counterfactual driven what-if analysis provides a model-agnostic anomaly-detection perspective. It uses a Temporal Convolutional Network to classify windows of sensor data as healthy or anomalous, and adopts CoMTE for counterfactual generation via k-d trees and greedy sequential feature replacement. The optimization target is
3
with the goal of finding the minimal feasible change that flips the prediction to “healthy.” The paper explicitly notes that counterfactual suggestions are not always actionable, which motivates asking “what if we do this instead?” (Cummins et al., 2024).
GdVAE supplies the most explicit self-explainable counterpart. It integrates a CVAE with a GDA classifier, treats class means in latent space as prototypes, and derives closed-form counterfactuals from the linear discriminant
4
For a desired target 5, the counterfactual latent code is
6
The explanation mechanism is intrinsic: no extra explainer parameters are required, and counterfactuals are generated analytically in latent space and decoded back to the input domain (Haselhoff et al., 2024).
6. Empirical profile, interpretive boundaries, and recurring misconceptions
In its explicit AEC-detection instantiation, SCFED is associated with strong reported performance. SEASONED states that it achieves 7, that its counterfactual explanations have the highest or near-highest Probability of Necessity and Probability of Sufficiency, that it remains accurate and robust under strong adversarial graph attack scenarios including node and edge injections, label flips, and structural perturbations, that it generalizes to newly emerged temporally out-of-distribution AECs, and that nearly full accuracy is achieved with as few as 50 training samples. The framework also releases a dataset of 359 AECs (Ai et al., 6 Sep 2025).
Related empirical results show that SCFED-like principles are not confined to one domain. CFDet reports that entry-level detection on Thunderbird and BGL attains 8, and that counterfactual normal subsequences cluster tightly with known normal sequences under t-SNE. The PRONOSTIA what-if analysis reports TCN accuracy greater than 98% in cross-validation and separate test splits. SCOUT reports 50x–1000x speedups over exhaustive-search-based visual counterfactual methods and raises mean student accuracy from chance level to 95% in a teaching experiment. GdVAE reports top-2 performance in consistency, proximity, and realism on MNIST and CelebA while preserving transparency (Cheng et al., 2022, Cummins et al., 2024, Wang et al., 2020, Haselhoff et al., 2024).
Several interpretive boundaries recur across these papers. First, counterfactual explanation is not equivalent to attention or saliency alone. CFDet explicitly notes that attention scores do not directly correspond to anomaly contributions regarding sequence-level anomaly labels, and SCOUT’s formulation adds counter-class suppression and confidence rather than relying on a single attribution map. Second, self-counterfactual explanation does not necessarily imply per-query optimization. SCOUT is optimization free, and GdVAE uses a closed-form latent solution, whereas CFDet requires policy gradient because its entry-selection variable is discrete. Third, actionability is not guaranteed by the existence of a counterfactual. The predictive-maintenance study explicitly notes that suggestions are not always actionable, which is why it reinterprets counterfactuals as what-if analysis rather than direct prescriptions (Cheng et al., 2022, Wang et al., 2020, Cummins et al., 2024, Haselhoff et al., 2024).
Taken together, the literature presents SCFED as a detector family organized around a shared principle: explanations should identify the minimal, prediction-changing structure within the detector’s own representational space. In SEASONED that structure is a factual subgraph extracted from an SRG; in sequential anomaly detection it is a minimal subsequence removal; in self-aware vision it is a discriminant confidence-weighted region; and in self-explainable generative modeling it is a closed-form latent traversal. The common thread is not a single architecture, but the integration of detection and counterfactual reasoning into one explanatory mechanism.