CounterFlowNet: From Minimal Changes to Meaningful Counterfactual Explanations
Authors: Oleksii Furman, Patryk Marszałek, Jan Masłowski, Piotr Gaiński, Maciej Zięba, Marek Śmieja
Organizations: Wrocław University of Science and Technology, Wrocław, Poland · Faculty of Mathematics and Computer Science, Jagiellonian University, Kraków, Poland · Doctoral School of Exact and Natural Sciences, Jagiellonian University, Kraków, Poland · Tooploox Sp. z o.o., Poland
Counterfactual explanations (CFs) provide human-interpretable insights into model's predictions by identifying minimal changes to input features that would alter the model's output. However, existing methods struggle to generate multiple high-quality explanations that (1) affect only a small portion of the features, (2) can be applied to tabular data with heterogeneous features, and (3) are consistent with the user-defined constraints. We propose CounterFlowNet, a generative approach that formulates CF generation as sequential feature modification using conditional Generative Flow Networks (GFlowNet). CounterFlowNet is trained to sample CFs proportionally to a user-specified reward function that can encode key CF desiderata: validity, sparsity, proximity and plausibility, encouraging high-quality explanations. The sequential formulation yields highly sparse edits, while a unified action space seamlessly supports continuous and categorical features. Moreover, actionability constraints, such as immutability and monotonicity of features, can be enforced at inference time via action masking, without retraining. Experiments on eight datasets under two evaluation protocols demonstrate that CounterFlowNet achieves superior trade-offs between validity, sparsity, plausibility, and diversity with full satisfaction of the given constraints.
Figures & tables
Figure 1: CounterFlowNet generates multiple counterfactual explanations by framing feature modification as a sequential decision process. Given an original data point (left), CounterFlowNet samples multiple valid CFs (CF1–CF3) proportional to a composite reward, naturally balancing sparsity, proximity, plausibility, and validity without requiring separate optimization.
Figure 2: Overview of the CounterFlowNet generative process. Top: The forward policy PF constructs a counterfactual x′ from source instance x0 through sequential feature modifications until a stop action terminates the trajectory. Bottom: Each action is factorized into two stages: sampling a feature index dt∼PF(d∣st,x0,y′) ( Stage 1 , Eq. 13 ), then sampling its new value vt∼PF(v∣dt,st,x0,y′) ( Stage 2 , Eq. 15 ).
Method
Spars.(%) ↑
Div.(%) ↑
H. Mean(%) ↑
Val.(%) ↑
Cov.(%) ↑
Unary(%) ↑
German Credit (Logistic Regression)
L2C
61.35
37.31
46.39
100.00
100.00
99.06
DICE-R
88.23
15.29
26.06
100.00
100.00
90.81
DICE-G
43.45
37.56
40.29
62.87
90.24
56.66
COPA
57.88
18.88
28.47
44.00
44.00
84.31
MCCE
28.76
33.40
30.91
48.74
100.00
58.76
Table 1: Results on Protocol A datasets (discretized features, B=4 ). Best in bold , second-best underlined .
Method
Val. ↑
Prox.-Cont ↓
Spars.-Cat ↓
ϵ -Spars. ↓
LOF ↓
Div. ↑
Adult Income (Neural Network)
DICE
1.00
0.44
0.05
0.50
0.17
0.06
CCHVAE
1.00
0.50
0.06
0.98
0.22
0.03
DiCoFlex
1.00
0.85
0.49
0.97
0.31
0.40
CFN
1.00
0.19
0.27
0.36
0.20
0.21
Bank (Neural Network)
Table 2: Results on Protocol B datasets (discretized features, B=64 ). Best in bold , second-best underlined .
Figure 3: Effect of discretization granularity B on Adult dataset.
Figure 4: Reward component ablation on the Adult dataset. (a) Increasing plausibility weight λp improves LOF at the cost of proximity. (b-c) Higher proximity weight λd yields sparser, more plausible CFs with lower diversity. All CounterFlowNet configurations consistently dominate the baseline Pareto frontiers.
CFN
Val. ↑
Prox.-Cont ↓
Spars.-Cat ↓
ϵ -Spars. ↓
LOF ↓
Div. ↑
Adult Income - Unconstrained
Retrained
0.99
0.22
0.28
0.41
0.22
0.23
Adult Income - Constraints Set 1
Retrained
0.99
0.24
0.27
0.45
0.22
0.23
Inference
0.99
0.24
0.27
0.46
0.22
0.23
Adult Income - Constraints Set 2
Table 3: Impact of actionability constraints on Adult. CFs quality under three increasingly strict constraint sets, comparing inference-time constraint action masking to retraining with the same constraints applied during training.
Table 5: Description of quantitative evaluation metrics for Protocol A. For each test instance i∈{1,…,N} , we generate a set of K counterfactuals Ci={xi′(k)}k=1K . Let h denote the classifier, yi′ the desired target class, D the total number of features, and Fm the set of features subject to monotonic constraints.
Desideratum
Metric
Description
Formula
Validity ( ↑ )
Val.
Success rate of changing model predictions.
N1n=1∑N1[h(xn′)=y′]
Proximity ( ↓ )
Prox.-Cont
L1 distance on continuous features.
Nval1n=1∑Nvalxn,num0−xn,num′1
Sparsity ( ↓ )
Spars.-Cat
Proportion of changed categorical features.
Nval1n=1∑NvalDcatxn,cat0−xn,cat′0
ϵ -Spars.
Continuous features changed beyond ϵ⋅Vj ( ϵ=0.05 ).
Table 6: Description of quantitative evaluation metrics for Protocol B. We generate N counterfactuals {xn′}n=1N for a test set of N instances {xn0}n=1N , of which Nval are valid. Let h denote the classifier, Dcat and Dnum the number of categorical and continuous features, D=Dcat+Dnum the total, Vj the range of feature j , G the number of unique original instances (groups), Kg the number of counterfactuals for group g , and N , C the sets of numerical and categorical feature indices.
Figure 5: Ablation of λp and λd with fixed weights for other reward weights ( λv=1.0,λs=0.4 )
Figure 6: Visualization of feature utilization of CounterFlowNet method and other baseline methods on the Adult dataset. The plots show the frequency with which each feature is modified in the generated CFs. CounterFlowNet demonstrates a more balanced feature utilization compared to baselines.
Feature
Original Input
Counterfactual 1
Counterfactual 2
Age
25
25
29
Workclass
Self-emp-not-inc
Federal-gov
Self-emp-not-inc
Education
HS-grad
Prof-school
HS-grad
Marital Status
Never-married
Married-AF-spouse
Married-civ-spouse
Occupation
Sales
Sales
Sales
Relationship
Other-relative
Wife
Wife
Appendix
Table 7: Counterfactual examples for the Adult dataset. The original prediction is "low income," and the counterfactual prediction is "high income."
Feature
Original Input
Counterfactual 1
Counterfactual 2
Revolving Util. of Unsecured Lines
69
69
69
Age
45
21
56
Times 30-59 Days Past Due
1
0
3
Debt Ratio
162.57
162.57
162.57
Monthly Income
5488.28
5488.28
5488.28
Open Credit Lines & Loans
5
0
4
Appendix
Table 8: Counterfactual examples for the GMC dataset. The original prediction is "high risk," and the counterfactual prediction is "low risk."
Counterfactual explanations show how changing an input could alter a model’s prediction, supporting model understanding, auditing, and actionable recourse.
In the field of Explainable AI (XAI), counterfactual (CF) explanations interpret a model's decision by suggesting the changes to the input that would lead to a more favourable outcome. To be useful in practice, such an explanation should change few features and change them as little as possible, properties known as sparsity and proximity. We observe that existing methods remain limited in this respect, especially for numerical features, whether they are model-agnostic and amortised, or gradient-based with full access to the model. In this paper, we propose FlowCF, a model-agnostic generative method that frames CF generation as sparse transport from the factual to the target class. We solve this transport with flow matching, which we extend to mixed feature types with a novel mixed flow operator, and exploit the resulting geometry to optimise for sparsity through a gating network that minimises the number of features the transport changes. Extensive experiments on six benchmark datasets demonstrate that FlowCF produces the best numerical sparsity and proximity, changing 29% of the numerical features where the best baseline changes 89%, at 70% smaller displacement, while remaining comparable on the other desiderata.
Emmanouil Panagiotou, Eirini Ntoutsi
Freie Universität Berlin · University of the Bundeswehr Munich
Counterfactual (CF) explanations identify changes that alter an input's classification. While existing methods produce realistic and low-cost CFs, they often fail to ensure feasibility, by suggesting non-constructive modifications or incompatible with future changes (e.g., changing an individual's race to secure a job offer). We introduce a refinement of CF explanations that explicitly enforces feasibility. Our approach is the first to efficiently generate CFs that are realistic, low-cost and feasible. We accommodate both hard feasible constraints, specified by domain knowledge users, and soft feasible constraints, inferred automatically via causal inference from the dataset. Our method, Feasible Counterfactual Explanations (FCx), is based on a modified Variational Autoencoder (VAE) optimized with a multi-factor loss function. We measure the cost of a change based on the absolute change in values (proximity) as well as the number of features changed (sparsity) while realism is measured based on the LOF for density estimation, guaranteeing that CFs reside in densely populated regions. Extensive experiments on four public datasets show that our approach matches state-of-the-art performance across multiple metrics while guaranteeing feasibility.
Kleopatra Markou, Vana Kalogeraki, Dimitrios Gunopulos
National and Kapodistrian University of Athens, Department of Informatics and Telecommunications, Greece · Athens University of Economics and Business, Department of Informatics, Greece
Counterfactual explanations (CFEs) are essential for interpreting black-box models, yet they often become invalid when models are slightly changed. Existing methods for generating robust CFEs are often limited to specific types of models, require costly tuning, or inflexible robustness controls. We propose a novel approach that jointly models the data distribution and the space of plausible model decisions to ensure robustness to model changes. Using a probabilistic consensus over a model ensemble, we train a conditional normalizing flow that captures the data density under varying levels of classifier agreement. At inference time, a single interpretable parameter controls the robustness level; it specifies the minimum fraction of models that should agree on the target class without retraining the generative model. Our method effectively pushes CFEs toward regions that are both plausible and stable across model changes. Experimental results demonstrate that our approach achieves superior empirical robustness while also maintaining good performance across other evaluation measures.
Marcin Kostrzewa, Maciej Zięba, Jerzy Stefanowski
Wrocław University of Science and Technology, Wrocław 50-347, Poland · Tooploox, Wrocław 53-601, Poland · Poznań University of Technology, Poznań 61-131, Poland