HyConEx: Hypernetwork classifier with counterfactual explanations

Patryk Marszalek, Ulvi Movsum-zada, Oleksii Furman, Kamil Ksiazek, Przemyslaw Spurek, Marek Smieja · OpenReview

Abstract

In recent years, there has been a growing interest in explainable AI methods. We want not only to make accurate predictions using sophisticated neural networks but also to understand what the model's decision is based on. One of the fundamental levels of interpretability is to provide counterfactual examples explaining the rationale behind the decision and identifying which features, and to what extent, must be modified to alter the model's outcome. To address these requirements, we introduce HyConEx, a classification model based on deep hypernetworks specifically designed for tabular data. Owing to its unique architecture, HyConEx not only provides class predictions but also delivers local interpretations for individual data samples in the form of counterfactual examples that steer a given sample toward an alternative class. While many explainable methods generated counterfactuals for external models, there have been no interpretable classifiers simultaneously producing counterfactual samples so far. HyConEx achieves competitive performance on several metrics assessing classification accuracy and fulfilling the criteria of a proper counterfactual attack. This makes HyConEx a distinctive deep learning model, which combines predictions and explainers as an all-in-one neural network.

What Problem Does This Paper Address?

In high-stakes machine learning applications—such as finance, medicine, or law—knowing that a model made a decision is often insufficient; stakeholders need to know why. Most explainable AI (XAI) tools focus on feature importance (e.g., "your age was the most significant factor in this loan denial"). However, users frequently seek counterfactual explanations: "What is the smallest change I can make to my data to flip the model's decision?" (e.g., "How much more should I earn to get my loan approved?").

Currently, generating these "what if" scenarios is computationally expensive. Most methods are "post-hoc," meaning they are external tools applied to a pre-trained model. These tools usually require a time-consuming multi-step optimization process for every single data point, making them impractical for real-time applications. The authors of HyConEx set out to build a "self-explaining" model that handles both prediction and explanation in a single forward pass.

Comparison of HyConEx with traditional XAI methods: LIME/SHAP (feature importance) and PPCEF/CEGP (optimization-based counterfactuals).

Methodology

HyConEx (Hypernetwork Classifier with Counterfactual Explanations) achieves its "all-in-one" capability through three key architectural innovations:

  1. Hypernetworks for Instance-Specific Weights: Instead of using fixed weights for all inputs, HyConEx employs a "backbone" hypernetwork. This network takes an input $x$ and generates a unique set of weights $W$ for a local linear classifier tailored specifically to that data point.
  2. The "Weight-as-Direction" Trick: This is the core innovation. The researchers realized that the weights generated by the hypernetwork do more than just classify; they also represent the "direction" toward the decision boundary in the feature space. To create a counterfactual for a different class $m$, the model simply subtracts a specific row of the weight matrix from the original input ($x' = x - W_m$). This allows for counterfactual generation in a single forward pass.
  3. Invertible Normalizing Flows (INF): A common pitfall for counterfactuals is that they can be "unrealistic" (e.g., suggesting a negative age or impossible feature combinations). HyConEx integrates a Masked Autoregressive Flow (MAF) to model the underlying data density. This acts as a "plausibility filter," ensuring the suggested modifications result in data points that actually look like they belong to the target class distribution.

For categorical data, the model uses a specialized smoothing technique with a low-temperature softmax to ensure the generated counterfactuals respect one-hot encoding constraints.

Datasets and Evaluation

The model was rigorously evaluated on 18 diverse tabular datasets from the OpenML repository, ensuring robustness across different domains:

  • Binary Classification: Moons, Law, Audit, Heloc.
  • Multi-class Classification: Blobs, Digits, Wine.
  • Categorical Features: Adult, Credit-G, Credit-A.

These datasets represent a wide range of real-world scenarios, from credit scoring to medical diagnosis, providing a comprehensive testbed for both classification accuracy and explanation quality.

Key Results and Insights

  • Efficiency and Speed: By embedding the explanation mechanism directly into the architecture, HyConEx avoids the "optimization loop" required by methods like WACH or CEGP. On the "Moons" dataset, HyConEx generated counterfactuals in just 0.003 seconds, whereas the popular CEGP method took 4,485 seconds for the same task. This massive speedup makes HyConEx viable for real-time user interfaces.
  • Competitive Accuracy: HyConEx matches or nearly matches the predictive performance (AUROC) of state-of-the-art models like CatBoost and TabResNet. Crucially, adding the explanation capability does not significantly degrade its primary classification performance.
  • High Validity and Plausibility: The model consistently achieved near 100% "validity," meaning the suggested counterfactuals successfully flipped the model's prediction to the desired target class. Furthermore, the integration of Normalizing Flows ensures these counterfactuals are realistic, distinguishing them from simple adversarial attacks.

Limitations and Future Work

While HyConEx represents a significant advancement, the authors identify several areas for future research:

  • Data Modality: The current architecture is specifically optimized for tabular data. Extending this "all-in-one" approach to images or text would require significant architectural adaptations.
  • Diversity of Explanations: HyConEx currently returns a single optimal counterfactual per alternative class. In some scenarios, users might benefit from a "menu" of diverse ways to change the outcome.
  • Robustness: Future work could explore how small perturbations in the classifier itself might affect the stability and reliability of the generated explanations.

The authors suggest that future iterations could swap the backbone for more advanced architectures like TabTransformers to further enhance performance.

The code for HyConEx is available at https://github.com/gmum/HyConEx.

Summary updated

BibTeX
@article{marszalek2026hyconex,
  author = {Patryk Marszalek and Ulvi Movsum-zada and Oleksii Furman and Kamil Ksiazek and Przemyslaw Spurek and Marek Smieja},
  title = {{HyConEx: Hypernetwork classifier with counterfactual explanations}},
  journal = {Neurocomputing},
  year = {2026},
  doi = {10.1016/j.neucom.2026.132748},
}
Figure