attribench.functional.compute_attributions

attribench.functional.compute_attributions(model, method_dict, dataset, batch_size, writer=None, device=None)[source]

Compute attributions for a given model and dataset using a dictionary of attribution methods, and optionally write them to a HDF5 file. If the writer is None, the attributions are simply returned in a dictionary. Otherwise, the attributions are written to the HDF5 file and None is returned.

TODO don’t write to file, just return the dict

Parameters:
modelnn.Module

The model for which the attributions should be computed.

method_dictDict[str, AttributionMethod]

Dictionary of attribution methods.

datasetDataset

Torch Dataset to use for computing the attributions.

batch_sizeint

The batch size to use for computing the attributions.

writerOptional[AttributionsDatasetWriter], optional

AttributionsDatasetWriter to write the attributions to, by default None. If None, the attributions are returned in a dictionary.

deviceOptional[torch.device], optional

Device to use, by default None. If None, the CPU is used.

Returns:
Optional[Dict[str, torch.Tensor]]

If writer is None, a dictionary of attributions.

Return type:

Optional[Dict[str, Tensor]]