peal.adaptors.counterfactual_knowledge_distillation

Counterfactual Knowledge Distillation (CFKD), PEAL’s central repair loop.

CFKD removes a Clever-Hans strategy from a trained classifier by showing a teacher what the classifier reacts to and finetuning on the verdicts. One iteration draws a class-balanced pool of factuals, asks the configured explainer for a counterfactual per sample, has the teacher label each factual/counterfactual pair as a valid class change (“true”) or a change of a spurious feature (“false”), serialises the pairs with feedback-corrected labels into a dataset, and finetunes the student on a mixture of that dataset and the original training data.

The loop repeats for finetune_iterations rounds. Every round writes its artefacts under base_dir/<iteration>/ so a crashed run resumes from current_iteration in config.yaml instead of recomputing the counterfactuals, which dominate the cost. Validation statistics are logged to TensorBoard as validation_* scalars, and the repaired student is saved as model.cpl.

The method is described in “Mitigating Clever Hans Strategies in Image Classifiers through Generating Counterexamples” (https://arxiv.org/pdf/2510.17524); reproduction_scripts/reproduce_cfkd_results.sh reproduces the paper’s numbers.

See also

peal.adaptors.didae

discovers the directions to correct in a generator latent instead of per-sample counterfactuals, and reuses CFKD for step 9.

peal.explainers.counterfactual_explainer

produces the counterfactuals.

peal.teachers

the feedback sources CFKD queries.

Classes

CFKD

This class implements the counterfactual knowledge distillation approach.

CFKDConfig

The config template for an running the CFKD adaptor.