Skip to content

RAP

RAP

Bases: Explainer

Computes Relative Attribute Propagation (RAP) explanations for a given model.

Supported Modules: Linear, Convolution

Parameters:

Name Type Description Default
model Model

The model for which RAP explanations are computed.

required
Reference

Woo-Jeoung Nam, Shir Gur, Jaesik Choi, Lior Wolf, Seong-Whan Lee. Relative Attributing Propagation: Interpreting the Comparative Contributions of Individual Units in Deep Neural Networks.

Source code in pnpxai/explainers/rap/attribute.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class RAP(Explainer):
    """
    Computes Relative Attribute Propagation (RAP) explanations for a given model.

    Supported Modules: `Linear`, `Convolution`

    Parameters:
        model (Model): The model for which RAP explanations are computed.

    Reference:
        Woo-Jeoung Nam, Shir Gur, Jaesik Choi, Lior Wolf, Seong-Whan Lee. Relative Attributing Propagation: Interpreting the Comparative Contributions of Individual Units in Deep Neural Networks.
    """

    SUPPORTED_MODULES = [Linear, Convolution]

    def __init__(self, model: Model):
        super().__init__(model)
        self.method = RelativeAttributePropagation(model)

    def compute_pred(self, output: Tensor) -> Tensor:
        """
        Computes the predicted class probabilities.

        Parameters:
            output (Tensor): The model output.

        Returns:
            Tensor: The one-hot encoded predicted class probabilities.
        """
        # get the index of the max log-probability
        pred = output.max(1, keepdim=True)[1]
        pred = pred.squeeze(-1)

        pred_one_hot = nn.functional.one_hot(pred, output.shape[-1]) * 1.0
        pred_one_hot = pred_one_hot.to(output.device)
        return pred_one_hot

    def attribute(self, inputs: DataSource, targets: DataSource, *args: Any, **kwargs: Any) -> DataSource:
        """
        Computes RAP attributions for the given inputs.

        Parameters:
            inputs (DataSource): The input data.
            targets (DataSource): The target labels.
            *args (Any): Additional positional arguments.
            **kwargs (Any): Additional keyword arguments.

        Returns:
            DataSource: RAP attributions.
        """
        outputs = self.method.run(inputs)
        preds = self.compute_pred(outputs)
        relprop = self.method.relprop(preds)
        return relprop

    def format_outputs_for_visualization(
        self,
        inputs: DataSource,
        targets: DataSource,
        explanations: DataSource,
        task: Task,
        kwargs: Optional[Dict[str, Any]] = None,
    ):
        explanations = explanations.detach().cpu()\
            .transpose(-1, -3)\
            .transpose(-2, -3)

        explanations = explanations.sum(-1)
        agg_dims = list(range(1, explanations.ndim))
        pos_max = explanations.relu().amax(agg_dims, keepdim=True)
        neg_max = (-explanations).relu().amax(agg_dims, keepdim=True)

        explanations = torch.where(
            explanations > 0,
            explanations / pos_max,
            explanations / neg_max
        )

        return explanations.numpy()

attribute(inputs, targets, *args, **kwargs)

Computes RAP attributions for the given inputs.

Parameters:

Name Type Description Default
inputs DataSource

The input data.

required
targets DataSource

The target labels.

required
*args Any

Additional positional arguments.

()
**kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
DataSource DataSource

RAP attributions.

Source code in pnpxai/explainers/rap/attribute.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def attribute(self, inputs: DataSource, targets: DataSource, *args: Any, **kwargs: Any) -> DataSource:
    """
    Computes RAP attributions for the given inputs.

    Parameters:
        inputs (DataSource): The input data.
        targets (DataSource): The target labels.
        *args (Any): Additional positional arguments.
        **kwargs (Any): Additional keyword arguments.

    Returns:
        DataSource: RAP attributions.
    """
    outputs = self.method.run(inputs)
    preds = self.compute_pred(outputs)
    relprop = self.method.relprop(preds)
    return relprop

compute_pred(output)

Computes the predicted class probabilities.

Parameters:

Name Type Description Default
output Tensor

The model output.

required

Returns:

Name Type Description
Tensor Tensor

The one-hot encoded predicted class probabilities.

Source code in pnpxai/explainers/rap/attribute.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def compute_pred(self, output: Tensor) -> Tensor:
    """
    Computes the predicted class probabilities.

    Parameters:
        output (Tensor): The model output.

    Returns:
        Tensor: The one-hot encoded predicted class probabilities.
    """
    # get the index of the max log-probability
    pred = output.max(1, keepdim=True)[1]
    pred = pred.squeeze(-1)

    pred_one_hot = nn.functional.one_hot(pred, output.shape[-1]) * 1.0
    pred_one_hot = pred_one_hot.to(output.device)
    return pred_one_hot