Package: lfxai
Label-Free XAICode Author: Jonathan Crabbé (jc2133@cam.ac.uk)This repository contains the implementation of LFXAI, a framework to explain the latent
representations of unsupervised black-box models with the help of usual feature importance and example-based methods.
For more details, please read ourICML 2022 paper: 'Label-Free Explainability for Unsupervised Models'.1. InstallationFrom PyPIpipinstalllfxaiFrom repository:Clone the repositoryCreate a new virtual environment with Python 3.8Run the following command from the repository folder:pipinstall.When the packages are installed, you are ready to explain unsupervised models.2. Toy exampleBellow, you can find a toy demonstration where we compute label-free feature and example importance
with a MNIST autoencoder. The relevant code can be found in the folderexplanations.importtorchfrompathlibimportPathfromtorchvision.datasetsimportMNISTfromtorch.utils.dataimportDataLoader,Subsetfromtorchvisionimporttransformsfromtorch.nnimportMSELossfromcaptum.attrimportIntegratedGradientsfromlfxai.models.imagesimportAutoEncoderMnist,EncoderMnist,DecoderMnistfromlfxai.models.pretextimportIdentityfromlfxai.explanations.featuresimportattribute_auxiliaryfromlfxai.explanations.examplesimportSimplEx# Select torch devicedevice=torch.device("cuda")iftorch.cuda.is_available()elsetorch.device("cpu")# Load datadata_dir=Path.cwd()/"data/mnist"train_dataset=MNIST(data_dir,train=True,download=True)test_dataset=MNIST(data_dir,train=False,download=True)train_dataset.transform=transforms.Compose([transforms.ToTensor()])test_dataset.transform=transforms.Compose([transforms.ToTensor()])train_loader=DataLoader(train_dataset,batch_size=100)test_loader=DataLoader(test_dataset,batch_size=100,shuffle=False)# Get a modelencoder=EncoderMnist(encoded_space_dim=10)decoder=DecoderMnist(encoded_space_dim=10)model=AutoEncoderMnist(encoder,decoder,latent_dim=10,input_pert=Identity())model.to(device)# Get label-free feature importancebaseline=torch.zeros((1,1,28,28)).to(device)# black image as baselineattr_method=IntegratedGradients(model)feature_importance=attribute_auxiliary(encoder,test_loader,device,attr_method,baseline)# Get label-free example importancetrain_subset=Subset(train_dataset,indices=list(range(500)))# Limit the number of training examplestrain_subloader=DataLoader(train_subset,batch_size=500)attr_method=SimplEx(model,loss_f=MSELoss())example_importance=attr_method.attribute_loader(device,train_subloader,test_loader)3. Reproducing the paper resultsMNIST experimentsIn theexperimentsfolder, run the following scriptpython-mmnist--nameexperiment_namewhere experiment_name can take the following values:experiment_namedescriptionconsistency_featuresConsistency check for label-freefeature importance (paper Section 4.1)consistency_examplesConsistency check for label-freeexample importance (paper Section 4.1)roar_testROAR test for label-freefeature importance (paper Appendix C)pretextPretext task sensitivityuse case (paper Section 4.2)disvaeChallenging assumptions withdisentangled VAEs (paper Section 4.3)The resulting plots and data are savedhere.ECG5000 experimentsRun the following scriptpython-mecg5000--nameexperiment_namewhere experiment_name can take the following values:experiment_namedescriptionconsistency_featuresConsistency check for label-freefeature importance (paper Section 4.1)consistency_examplesConsistency check for label-freeexample importance (paper Section 4.1)The resulting plots and data are savedhere.CIFAR10 experimentsRun the following scriptpython-mcifar10The experiment can be selected by changing the experiment_name
parameter inthis file.
The parameter can take the following values:experiment_namedescriptionconsistency_featuresConsistency check for label-freefeature importance (paper Section 4.1)consistency_examplesConsistency check for label-freeexample importance (paper Section 4.1)The resulting plots and data are savedhere.dSprites experimentRun the following scriptpython-mdspritesThe experiment needs several hours to run since several VAEs are trained.
The resulting plots and data are savedhere.4. CitingIf you use this code, please cite the associated paper:@misc{Crabbe2022LFXAI,
  doi = {10.48550/ARXIV.2203.01928},
  url = {https://arxiv.org/abs/2203.01928},
  author = {Crabbé, Jonathan and van der Schaar, Mihaela},
  keywords = {Machine Learning (cs.LG), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
  title = {Label-Free Explainability for Unsupervised Models},
  publisher = {arXiv},
  year = {2022},
  copyright = {Creative Commons Attribution 4.0 International}
}