peal.explainers.no_generator_counterfactual_explainers.DiCEExplainer¶
- class peal.explainers.no_generator_counterfactual_explainers.DiCEExplainer[source]¶
Bases:
ExplainerInterfaceExplainer wrapper for DiCE (Diverse Counterfactual Explanations) https://arxiv.org/pdf/1905.07697
The predictor is a PyTorch tabular classifier; the dataset must expose a pandas
dfand anattributeslist (and optionallytask_configwithx_selection/y_selectionandcontinuous_features) so thatdice_ml.Datacan be built from it.- Parameters:
config (
DiCEExplainerConfig) – The explainer config;explainer_configwins if both are given.explainer_config (
DiCEExplainerConfig) – The explainer config;explainer_configwins if both are given.predictor (
torch.nn.Module) – Classifier mapping[B, num_features]to logits.predictor_datasources (
sequence) – Dataloaders or datasets of the predictor; the first entry (or its.dataset) is used to initialize DiCE.datasourcewins if both are given. Without one,datasetisNoneandexplain_batchraises.datasource (
sequence) – Dataloaders or datasets of the predictor; the first entry (or its.dataset) is used to initialize DiCE.datasourcewins if both are given. Without one,datasetisNoneandexplain_batchraises.**kwargs – Ignored; accepted for factory compatibility.
- Variables:
Methods
No-op clustering hook; returns the first positional argument.
Generate DiCE counterfactuals for every factual in
batch.Build the
dice_mlData,ModelandDiceobjects.Attributes
explainer_config- __init__(config=None, explainer_config=None, predictor=None, predictor_datasources=None, datasource=None, **kwargs)[source]¶
Store config, predictor and dataset; build DiCE if a dataset is given.
- setup_dice()[source]¶
Build the
dice_mlData,ModelandDiceobjects.The outcome column is
task_config.y_selection[0]or, failing that, the last dataset attribute; the feature columns aretask_config.x_selectionor all remaining attributes. Continuous features come fromdataset.continuous_featuresor a heuristic (numeric dtype with more than 10 unique values). The predictor is wrapped in a small sklearn-like object withpredictandpredict_proba. Prints a warning and does nothing if the dataset has nodf/attributes.
- explain_batch(batch, **kwargs)[source]¶
Generate DiCE counterfactuals for every factual in
batch.Each factual is converted to a one-row dataframe (categorical columns snapped to the exact values DiCE saw during setup), explained with
num_attemptscounterfactuals and the resulting rows are scored with the predictor. Failed or empty searches fall back to the factual itself with confidence 0.0; withmerge_clusters != "concatenate"only the highest-confidence counterfactual per factual is kept. The batch dict is updated in place and expanded so that every counterfactual gets its own entry in all*_listkeys.- Parameters:
batch (
dict) – Must contain"x_list"(tensor or list of[num_features]tensors) and"y_target_list";"y_list"is carried along if present.**kwargs –
base_pathandstart_idxfordataset.generate_contrastive_collagewhentracking_level >= 4.
- Returns:
The same dict with
x_counterfactual_list,y_target_end_confidence_list,y_source_list,y_target_start_confidence_list,x_attribution_list(|cf - x|unless collages are rendered),collage_path_listand emptyz_difference_list/history_list/cluster_listplaceholders.- Return type:
- Raises:
ValueError – If no dataset was provided at construction.