Пример #1
0
    def _assert_attributions(
        self,
        model: Module,
        layer: Module,
        inputs: Tensor,
        baselines: Union[Tensor, Callable[..., Tensor]],
        neuron_ind: Union[int, Tuple[Union[int, slice], ...]],
        n_samples: int = 5,
    ) -> None:
        ngs = NeuronGradientShap(model, layer)
        nig = NeuronIntegratedGradients(model, layer)
        attrs_gs = ngs.attribute(
            inputs, neuron_ind, baselines=baselines, n_samples=n_samples, stdevs=0.09
        )

        if callable(baselines):
            baselines = baselines(inputs)

        attrs_ig = []
        for baseline in torch.unbind(baselines):
            attrs_ig.append(
                nig.attribute(inputs, neuron_ind, baselines=baseline.unsqueeze(0))
            )
        combined_attrs_ig = torch.stack(attrs_ig, dim=0).mean(dim=0)
        self.assertTrue(ngs.multiplies_by_inputs)
        assertTensorAlmostEqual(self, attrs_gs, combined_attrs_ig, 0.5)
Пример #2
0
    def _assert_attributions(self,
                             model,
                             layer,
                             inputs,
                             baselines,
                             neuron_ind,
                             n_samples=5):
        ngs = NeuronGradientShap(model, layer)
        nig = NeuronIntegratedGradients(model, layer)
        attrs_gs = ngs.attribute(inputs,
                                 neuron_ind,
                                 baselines=baselines,
                                 n_samples=n_samples,
                                 stdevs=0.09)

        if callable(baselines):
            baselines = baselines(inputs)

        attrs_ig = []
        for baseline in baselines:
            attrs_ig.append(
                nig.attribute(inputs,
                              neuron_ind,
                              baselines=baseline.unsqueeze(0)))
        attrs_ig = torch.stack(attrs_ig, axis=0).mean(axis=0)
        assertTensorAlmostEqual(self, attrs_gs, attrs_ig, 0.5)
Пример #3
0
    def test_basic_multilayer(self) -> None:
        model = BasicModel_MultiLayer(inplace=True)
        model.eval()

        inputs = torch.tensor([[1.0, 20.0, 10.0]])
        baselines = torch.zeros(2, 3)
        ngs = NeuronGradientShap(model, model.linear1, multiply_by_inputs=False)
        attr = ngs.attribute(inputs, 0, baselines=baselines, stdevs=0.0)
        self.assertFalse(ngs.multiplies_by_inputs)
        assertTensorAlmostEqual(self, attr, [1.0, 1.0, 1.0])