peal.adaptors.clarc

Class Artifact Compensation (ClArC) adaptors for removing known shortcuts.

ClArC (Anders et al.) corrects a classifier that relies on an artifact (a spurious “confounder” feature) by estimating a Concept Activation Vector (CAV) for the artifact from group-annotated activations and then either

  • projecting the artifact direction out of the activations at a chosen layer (PClArC, optionally followed by fine-tuning the downstream head), or

  • fine-tuning the downstream head with a right-for-the-right-reasons penalty that pushes the input gradient orthogonal to the CAV (RRClArC).

Both are PEAL adaptors driven by a ClArCConfig: they load a trained predictor, sweep layer_index x correction_strength, evaluate group accuracies on the validation (and optionally an unpoisoned test) split, write one csv per evaluation dataset plus best_model_result.txt into base_dir and save every corrected model as *.cpl. Module-level helpers split a model into feature extractor / head and compute pattern or SVM CAVs.

Module attributes

CavCache

Memo of the CAV estimated for one layer: layer (int), cav, annotations and activations (tensors from ClArC.get_annotations_and_activations()).

Functions

calculate_cav

Dispatch to calculate_pcav() or calculate_svm_cav().

calculate_pcav

Pattern CAV: covariance of activations with the +-1 artifact label.

calculate_svm_cav

CAV from the normal of a balanced LinearSVC (C=1, up to 10000 iters).

extract_all_children

Flatten nested nn.Sequential containers into a list of leaf children.

get_layer_name

Return the dotted name of the child at 1-based layer_index.

split_model

Split a model into Sequential feature extractor and downstream head.

Classes

CavCache

Memo of the CAV estimated for one layer: layer (int), cav, annotations and activations (tensors from ClArC.get_annotations_and_activations()).

CavProjection

Layer that projects the CAV direction out of its input.

ClArC

Base class of the ClArC adaptors: data setup, sweep loop and evaluation.

ClArCConfig

Configuration shared by the ClArC adaptors.

PClArC

Projective ClArC: remove the CAV direction from activations at a layer.

PClArCConfig

Config of PClArC.

RRClArC

Right-for-the-right-reasons ClArC.

RRClArCConfig

Config of RRClArC.

SimpleProjection

Layer that subtracts the mean artifact minus non-artifact activation.