How it Works
Diverse Counterfactual Explanations (DiCE) is designed to generate counterfactual explanations for machine learning models by identifying minimal feature changes required to alter a model’s prediction. These explanations address a critical gap in traditional interpretability methods, which often provide static feature importance scores without actionable insights. For example, if a loan application is rejected due to a poor credit history, DiCE generates alternative feature profiles—such as adjusted income or employment status—that would result in approval, thereby offering practical recourse.
The core mechanism of DiCE involves framing counterfactual generation as an optimization problem. This approach ensures that the generated explanations are both proximal to the original input (minimizing unrealistic changes) and diverse (covering multiple plausible alternatives). The optimization balances three key objectives: changing the model’s output, maintaining proximity to the original instance, and ensuring diversity among the counterfactuals. This method is model-agnostic, meaning it can be applied to any machine learning model, including those from libraries like scikit-learn, TensorFlow, or PyTorch.
DiCE supports two primary methods for generating counterfactuals: an explicit loss-based method (default for deep learning models) and a Variational AutoEncoder (VAE)-based method. The loss-based method, described in Mothilal et al. The VAE-based method, outlined in Mahajan et al. Both methods require differentiable models, such as neural networks, but DiCE can also work with pre-trained models without access to the original training data.
To ensure practical feasibility, DiCE incorporates tunable parameters for diversity and proximity, allowing users to control the trade-off between exploring multiple options and staying close to the original input. Additionally, it supports constraints on feature modifications, such as specifying which features can be varied (e.g., income, education) and their permissible ranges (e.g., working hours between 10 and 50). Users can also assign feature weights to reflect the relative difficulty of changing specific attributes, such as education level versus working hours. This flexibility ensures that generated counterfactuals align with real-world constraints.
DiCE operates in three straightforward steps: preparing the dataset, training or loading a pre-trained model, and invoking DiCE to generate counterfactual examples. The library does not require access to the full training dataset, only metadata about feature properties (e.g., min/max values for continuous features or categories for categorical features). For instance, in a loan approval scenario, DiCE can generate counterfactuals based solely on metadata about features like income brackets or employment categories, without exposing raw applicant data.
The generated counterfactuals are presented as feature-perturbed versions of the original input, highlighting the minimal changes needed to achieve a desired outcome.
Use Cases
DiCE is particularly valuable in domains where machine learning models make high-stakes decisions, such as finance, healthcare, education, and criminal justice. Its ability to provide actionable recourse makes it indispensable for stakeholders across the decision-making process.
For decision subjects—such as loan applicants, job seekers, or patients—DiCE offers clear pathways to improve their outcomes. For example, in a loan rejection scenario, DiCE might reveal that increasing income by a specific amount or reducing debt would flip the decision. This contrasts with traditional explanations, which may only list contributing factors without suggesting actionable changes. By presenting multiple diverse counterfactuals, DiCE empowers individuals to choose the most feasible option based on their circumstances.
By analyzing counterfactual explanations across a selection of inputs, developers can uncover inconsistencies, such as features that rarely influence outcomes despite their importance in other cases. This capability is particularly useful for detecting fairness issues, where certain attributes (e.g., gender or race) may disproportionately affect predictions. DiCE’s support for aggregate metrics aims to further assist developers in refining models to meet ethical and performance standards.
Decision-makers, such as doctors, judges, or hiring managers, can also benefit from DiCE by gaining deeper insights into model behavior. For instance, a judge reviewing a risk assessment model might use DiCE to explore how small changes in defendant characteristics (e.g., employment status or prior offenses) could alter the predicted risk level. This transparency helps decision-makers weigh model suggestions against their own judgment and domain expertise. DiCE’s flexibility in handling constraints ensures that generated counterfactuals remain realistic and aligned with practical considerations.
Beyond individual use cases, DiCE has been applied to real-world datasets in case studies, such as the Hotel Bookings dataset and the Titanic Dataset. These examples demonstrate its versatility across different problem types, including binary classification, multi-class classification, and regression tasks.
Key Features & Capabilities
- Model-Agnostic Design: DiCE works with any machine learning model, regardless of the underlying architecture (e.g., decision trees, neural networks, or ensemble methods). This versatility makes it applicable to a wide range of use cases without requiring model-specific modifications.
- Diverse and Proximal Counterfactuals: The library generates multiple counterfactual explanations that are both diverse (covering different pathways to change the outcome) and proximal (minimizing unrealistic deviations from the original input). This dual focus ensures that users receive actionable and feasible options.
- Customizable Constraints: Users can specify which features to vary, their permissible ranges, and the relative difficulty of changing each feature. For example, education level might be weighted higher than working hours to reflect its lower mutability.
- No Full Dataset Requirement: DiCE operates with minimal data requirements, needing only metadata about feature properties (e.g., categories for categorical features or min/max values for continuous features). This design preserves data privacy and enables deployment in sensitive environments.
- Support for Pre-Trained Models: The library can generate counterfactuals for pre-trained models without access to the original training data.
- This integration allows users to combine counterfactual explanations with other explainability techniques for comprehensive model understanding.
- Optimization-Based Methods: DiCE employs two distinct methods for counterfactual generation: a loss-based optimization approach (default for deep learning) and a VAE-based method. These methods ensure that counterfactuals are faithful to the original model while exploring the feature space efficiently.
- Visualization and Export: Generated counterfactuals are presented in tabular format, highlighting changes in feature values. Future updates plan to include natural language explanations for improved accessibility.
- Research and Benchmarking: DiCE serves as a framework for testing and benchmarking counterfactual explanation methods. Its open-source nature encourages collaboration and innovation, with ongoing updates to incorporate new techniques and address emerging challenges in explainable AI.
- Handling Causal Constraints: While DiCE currently focuses on independent feature modifications, ongoing research aims to incorporate causal relationships between features.
- Performance and Scalability: DiCE has been shown to outperform existing methods in generating diverse counterfactuals. Additionally, it demonstrates that a small set of counterfactuals can locally approximate a model’s decision boundary with accuracy comparable to specialized methods like LIME, further validating its utility as an explainability tool.
