def _conductance_input_sum_test_assert( self, model: Module, target_layer: Module, test_input: TensorOrTupleOfTensors, test_baseline: Optional[Union[Tensor, int, float, Tuple[Union[Tensor, int, float], ...]]] = None, ): layer_cond = LayerConductance(model, target_layer) attributions = cast( Tensor, layer_cond.attribute( test_input, baselines=test_baseline, target=0, n_steps=500, method="gausslegendre", ), ) neuron_cond = NeuronConductance(model, target_layer) for i in range(attributions.shape[1]): for j in range(attributions.shape[2]): for k in range(attributions.shape[3]): neuron_vals = neuron_cond.attribute( test_input, (i, j, k), baselines=test_baseline, target=0, n_steps=500, ) for n in range(attributions.shape[0]): self.assertAlmostEqual( torch.sum(neuron_vals[n]).item(), attributions[n, i, j, k].item(), delta=0.005, )
def test_matching_layer_tuple_selector_fn(self) -> None: net = BasicModel_MultiLayer(multi_input_module=True) inp = torch.tensor([[0.0, 6.0, 0.0]]) lc = LayerConductance(net, net.multi_relu) layer_attr = lc.attribute(inp, target=0, n_steps=500, method="gausslegendre") nc = NeuronConductance(net, net.multi_relu) for i in range(len(layer_attr)): for j in range(layer_attr[i].shape[1]): neuron_attr = nc.attribute( inp, lambda x: x[i][:, j], target=0, n_steps=500, method="gausslegendre", ) self.assertAlmostEqual( neuron_attr.sum().item(), layer_attr[i][0][j].item(), delta=0.005, )