attribench.MethodFactory

class attribench.MethodFactory(config_dict)[source]

Bases: object

This class accepts a config dictionary for attribution methods in its constructor, and will return a dictionary of ready-to-use AttributionMethod objects when called with a model (nn.Module) as argument. This allows the attribution methods to be instantiated in subprocesses, which is necessary for computing attributions on multiple GPUs, as the methods need access to the specific copy of the model for their process.

The config dictionary should map strings to either AttributionMethod constructors, or tuples consisting of an AttributionMethod constructor and a dictionary of keyword arguments to pass to the constructor.

Example:

{
    "method1": AttributionMethod1,
    "method2": (AttributionMethod2, {"kwarg1": 1, "kwarg2": 2}),
}
Parameters:
config_dictConfigDict

Dictionary mapping strings to either AttributionMethod constructors, or tuples consisting of an AttributionMethod constructor and a dictionary of keyword arguments to pass to the constructor.

Methods

get_method_names

rtype:

List[str]

__call__(model)[source]

Create dictionary mapping method names to AttributionMethod objects.

Parameters:
modelnn.Module

Model to compute attributions for.

Returns:
Dict[str, AttributionMethod]

Dictionary mapping method names to AttributionMethod objects.

Return type:

Dict[str, AttributionMethod]