Train a sparse autoencoder on CLIP features.
- CLIP checkpoint
- Training dataset (images only)
PYTHONPATH=./ python tasks/train_sae_vit.py📝 Configuration files will be added soon
- SAE checkpoint (
.ptfile)
- View our training logs on W&B
Extract and save SAE latent activations for downstream analysis.
- CLIP checkpoint
- SAE checkpoint (from training step)
- Dataset (can differ from training dataset)
PYTHONPATH=./ python tasks/compute_sae_feature_data.py \
--root_dir ./ \
--dataset_name imagenet \
--sae_path /PATH/TO/SAE_CKPT.pt \
--vit_type base-
Download MaPLe from the official repo or Google Drive
-
Run extraction:
PYTHONPATH=./ python tasks/compute_sae_feature_data.py \
--root_dir ./ \
--dataset_name imagenet \
--sae_path /PATH/TO/SAE_CKPT.pt \
--vit_type maple \
--model_path /PATH/TO/MAPLE_CKPT \ # e.g., .../model.pth.tar-5
--config_path /PATH/TO/MAPLE_CFG \ # e.g., .../configs/models/maple/vit_b16_c2_ep5_batch4_2ctx.yamlAll files will be saved to: {root_dir}/out/feature_data/{vit_type}/{dataset_name}/
max_activating_image_indices.ptmax_activating_image_label_indices.ptmax_activating_image_values.ptsae_mean_acts.ptsae_sparsity.pt
Explore the extracted features with our patchsae/analysis/analysis.ipynb
Compute class-level SAE activation patterns.
- CLIP checkpoint
- SAE checkpoint
- SAE feature data (from previous step)
- Dataset (must be the SAME dataset used in the extraction step)
PYTHONPATH=./ python tasks/compute_class_wise_sae_activation.py \
--root_dir ./ \
--dataset_name imagenet \
--threshold 0.2 \
--sae_path /PATH/TO/SAE_CKPT.pt \
--vit_type basePYTHONPATH=./ python tasks/compute_class_wise_sae_activation.py \
--root_dir ./ \
--dataset_name imagenet \
--threshold 0.2 \
--sae_path /PATH/TO/SAE_CKPT.pt \
--vit_type maple \
--model_path /PATH/TO/MAPLE_CKPT \ # e.g., .../model.pth.tar-5
--config_path /PATH/TO/MAPLE_CFG \ # e.g., .../configs/models/maple/vit_b16_c2_ep5_batch4_2ctx.yamlcls_sae_cnt.npy- Matrix of shape(num_sae_latents, num_classes)
Evaluate classification using feature steering with SAE latents.
- CLIP checkpoint
- SAE checkpoint
- Class-level activation data (
cls_sae_cnt.npyfrom previous step) - Dataset (must be the SAME dataset used for class-level activations, though can be a different split)
PYTHONPATH=./ python tasks/classification_with_top_k_masking.py \
--root_dir ./ \
--dataset_name imagenet \
--sae_path /PATH/TO/SAE_CKPT.pt \
--cls_wise_sae_activation_path /PATH/TO/cls_sae_cnt.npyPYTHONPATH=./ python tasks/classification_with_top_k_masking.py \
--root_dir ./ \
--dataset_name imagenet \
--sae_path /PATH/TO/SAE_CKPT.pt \
--cls_wise_sae_activation_path /PATH/TO/cls_sae_cnt.npy \
--vit_type maple \
--model_path /PATH/TO/MAPLE_CKPT \ # e.g., .../model.pth.tar-5
--config_path /PATH/TO/MAPLE_CFG \ # e.g., .../configs/models/maple/vit_b16_c2_ep5_batch4_2ctx.yamlOutput will be saved to eval_outputs/:
metrics.csv- Contains class-wise True Positive Rate (TPR = TP/(TP+FP+TN+FN)) for each masking configuration- Results for both "on" and "off" conditions
- For k values in [1, 2, 5, 10, 50, 100, 500, 1000, 2000, SAE_DIM]