igc.base.AbstractAttributionMethod#
- class igc.base.AbstractAttributionMethod(module, dataset, dtld_kwargs=None, forward_method_name=None, forward_method_kwargs=None, n_embedding_categories=None, dtype=torch.float32, dtype_cat=torch.int32)[source]#
Bases:
objectDefine the base class for an abstract attribution method.
The sub-classes are expected to implement a
compute()method, specific to each attribution method.- Parameters:
module (torch.nn.Module) – PyTorch module defining the model under scrutiny.
dataset (torch.utils.data.Dataset) – PyTorch dataset providing inputs/outputs for any given index. See PyTorch documentation for more information. In addition, inputs must be organized in a specific manner, see warning below.
dtld_kwargs (dict) – Additional keyword arguments to the dataloaders (
torch.utils.data.DataLoader) constructed around thedataset, except:dataset,batch_size,shuffle,sampler,batch_sampler, andgenerator.forward_method_name (str) – Name of the forward method of the
module. IfNone, the defaultforwardis used.forward_method_kwargs (dict) – Additional keyword arguments to the forward method of the
module.n_embedding_categories (None | int | tuple(int)) – Enable the computation of attributions for categorical inputs associated with
torch.nn.Embeddinglayers, by providing the number of embedding categories.dtype (torch.dtype) – Default data type of all intermediary tensors. It also defines the NumPy data type of the attribution results.
dtype_cat (torch.dtype) – Default data type of the categorical input tensors.
Notes
Warning
When computing attributions on models using multiple inputs, e.g., x_1, x_2, and x_cat, with x_cat a categorical input, the dataset must return all inputs packed in a tuple, such as: (x_1, x_2, x_cat), y. Note that categorical inputs must be placed at the end of the tuple.
Note
Using categorical inputs with
torch.nn.Embeddinglayers modifies the output shapes of attributions associated with these categorical inputs. The number of embedding categories is appended to original shapes.