peal.adaptors.clarc.RRClArC

class peal.adaptors.clarc.RRClArC[source]

Bases: ClArC

Right-for-the-right-reasons ClArC.

Instead of projecting, fine-tunes the downstream head with ce_loss + correction_strength * rrc_loss where rrc_loss penalises the alignment between the gradient of the logits w.r.t. the layer activations and the CAV. Training curves go to TensorBoard under base_dir/finetuning-logs.

Parameters:

adaptor_config (RRClArCConfig) – See RRClArCConfig.

Methods

finetune

Fine-tune the head with cross entropy plus the CAV-gradient penalty.

get_evaluation_filename

Build the csv name including loss type, gradient target and epochs.

get_gradient_target

Reduce logits (B, K) to the scalar per sample that is differentiated.

run

Run the sweep of ClArC.run() and close the TensorBoard writer.

__init__(adaptor_config)[source]
Parameters:

adaptor_config (RRClArCConfig)

get_evaluation_filename(dataset_name)[source]

Build the csv name including loss type, gradient target and epochs.

Parameters:

dataset_name (str)

Return type:

str

run()[source]

Run the sweep of ClArC.run() and close the TensorBoard writer.

finetune(cav, downstream_head, lamb, feature_extractor=None, model_name='corrected_model')[source]

Fine-tune the head with cross entropy plus the CAV-gradient penalty.

Parameters:
  • cav (torch.Tensor) – Unit CAV in the (pooled) activation space of the split layer.

  • downstream_head (torch.nn.Module) – Module trained with SGD (momentum 0.95, training.learning_rate).

  • lamb (float) – Weight of the RR penalty.

  • feature_extractor (torch.nn.Module, optional) – Frozen prefix producing the representation; None uses the input.

  • model_name (str) – Prefix of the TensorBoard scalar tags.

Returns:

  • best_model (torch.nn.Module) – Head with the best validation avg_group_acc (ties broken by lower epoch RR loss).

  • number_epochs_finetuned (int) – Epoch at which it was selected.

Notes

The penalty is mean((grad . cav) ** 2) for rrc_loss="l2" or the mean absolute cosine similarity for "cosine"; with cav_mode set the gradient is rearranged to (B*H*W, C) so the CAV lives in channel space. A NaN / inf total loss aborts the epoch.

get_gradient_target(prediction)[source]

Reduce logits (B, K) to the scalar per sample that is differentiated.

Selected by config.gradient_target: "max" (top logit), "attacked_class" (that class’ logit), "all" (sum of logits) or "all_random" (sum with random signs).

Raises:

NotImplementedError – For any other value.