peal.adaptors.clarc.RRClArC¶
- class peal.adaptors.clarc.RRClArC[source]¶
Bases:
ClArCRight-for-the-right-reasons ClArC.
Instead of projecting, fine-tunes the downstream head with
ce_loss + correction_strength * rrc_losswhererrc_losspenalises the alignment between the gradient of the logits w.r.t. the layer activations and the CAV. Training curves go to TensorBoard underbase_dir/finetuning-logs.- Parameters:
adaptor_config (
RRClArCConfig) – SeeRRClArCConfig.
Methods
Fine-tune the head with cross entropy plus the CAV-gradient penalty.
Build the csv name including loss type, gradient target and epochs.
Reduce logits
(B, K)to the scalar per sample that is differentiated.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.
- 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 validationavg_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)forrrc_loss="l2"or the mean absolute cosine similarity for"cosine"; withcav_modeset 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.