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.
National and Kapodistrian University of Athens, Department of Informatics and Telecommunications, Greece · Athens University of Economics and Business, Department of Informatics, Greece
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