# Real-Time, Model-Agnostic and User-Driven Counterfactual Explanations Using Autoencoders
## 1 Introduction  
Recently, the field of Artificial Intelligence (AI) has witnessed significant progress with the rise of Deep Learning (DL) techniques. These methods have shown great success in various areas, but they also have limitations, particularly in terms of interpretability and robustness. DL models tend to be complex, with millions of parameters, which can make it challenging to understand the reasoning behind their predictions. Additionally, these models are trained on historical data that may contain biases, resulting in unfair and incorrect decisions. These limitations are especially problematic in industrial domains, where a lack of trust in the models' predictions can lead to the use of less effective but more interpretable methods. To address these limitations, research in the area of Explainable Artificial Intelligence (XAI) has gained traction recently. The goal of XAI is to find a balance between model effectiveness and interpretability, allowing for understanding of the decision-making processes of AI models. In recent years, many methods have been proposed regarding the explainability of ML models. Some of these methods use rules to explain other models and are called rule-based methods, such as Anchors [1] or RuleFit [2]. In a different way, attribution-based methods attempt to attribute an influence score to each input variable. These methods are the most used ones in the XAI community, and the influence attributions can be computed in many ways. One way uses perturbation-based methods, such as SHAP [3] or LIME [4], which make small variations in the input variables of a model to see how the output varies. Another way is by gradient-based methods, such as Integrated Gradients [5] for example,which compute the attributions as a function of the partial derivatives of the target with respect to the input features. Other methods for explaining model decisions utilize counterfactual explanations [6]. Counterfactual explanations are presented in the form of conditional statements, which aim to answer the question \"what would happen if...?\". In the context of Machine Learning, a counterfactual explanation describes the modifications that must be made to the input of a model in order to alter its prediction. The properties of counterfactual explanations may vary depending on the particular context in which they are employed [7]. In summary, the following are the general properties that a counterfactual explanation should meet, according to [7]:  
* **Data closeness:** Given an input \(\mathbf{x}\), the counterfactual explanation \(\mathbf{x}_{cf}\) has to be a minimal perturbation of \(\mathbf{x}\), i.e., \[\mathbf{x}\approx\mathbf{x}_{cf}.\] (1) Related to data closeness [7] also mentions _sparsity_. However, this property can be a challenge when working with continuous variables because it may not be possible to find a parsimonious explanation by only modifying a few features. * **User-driven:** The counterfactual explanation has to be user-driven, meaning that the user can indicate whether the class \(\mathbf{y}_{cf}\), to which a counterfactual \(\mathbf{x}_{cf}\) belongs, can be specified. That is, given a black-box model \(f(\cdot)\) \[f(\mathbf{x}_{cf})=\mathbf{y}_{cf}.\] (2) Otherwise, in multiclass problems, for example, the changes would only be directed to the closest class, excluding explanations for other classes. Note that this property also ensures the _validity_ property of [7]. * **Amortized inference:** The counterfactual explanations have to be straightforward, without solving an optimization problem for each input to be changed. In this way, the explanation model should learn to predict the counterfactual. The algorithm needs to quickly calculate a counterfactual for any new input \(\mathbf{x}\). Otherwise, the process of generating explanations is time-consuming.