def _conductance_input_sum_test_assert(self, model, target_layer, test_input, test_baseline=None): layer_cond = LayerConductance(model, target_layer) attributions = 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]), attributions[n, i, j, k], delta=0.005, )
def _conductance_input_test_assert( self, model, target_layer, test_input, test_neuron, expected_input_conductance, additional_input=None, ): for internal_batch_size in (None, 1, 20): cond = NeuronConductance(model, target_layer) attributions = cond.attribute( test_input, test_neuron, target=0, n_steps=500, method="gausslegendre", additional_forward_args=additional_input, internal_batch_size=internal_batch_size, ) if isinstance(expected_input_conductance, tuple): for i in range(len(expected_input_conductance)): for j in range(len(expected_input_conductance[i])): assertArraysAlmostEqual( attributions[i][j:j + 1].squeeze(0).tolist(), expected_input_conductance[i][j], delta=0.1, ) else: assertArraysAlmostEqual( attributions.squeeze(0).tolist(), expected_input_conductance, delta=0.1, )
def _conductance_input_test_assert( self, model: Module, target_layer: Module, test_input: TensorOrTupleOfTensorsGeneric, test_neuron: Union[int, Tuple[int, ...], Callable], expected_input_conductance: Union[List[float], Tuple[List[List[float]], ...]], additional_input: Any = None, multiply_by_inputs: bool = True, ) -> None: for internal_batch_size in (None, 5, 20): cond = NeuronConductance( model, target_layer, multiply_by_inputs=multiply_by_inputs, ) self.assertEqual(cond.multiplies_by_inputs, multiply_by_inputs) attributions = cond.attribute( test_input, test_neuron, target=0, n_steps=500, method="gausslegendre", additional_forward_args=additional_input, internal_batch_size=internal_batch_size, ) if isinstance(expected_input_conductance, tuple): for i in range(len(expected_input_conductance)): for j in range(len(expected_input_conductance[i])): assertTensorAlmostEqual( self, attributions[i][j:j + 1].squeeze(0), expected_input_conductance[i][j], delta=0.1, mode="max", ) else: if isinstance(attributions, Tensor): assertTensorAlmostEqual( self, attributions.squeeze(0), expected_input_conductance, delta=0.1, mode="max", ) else: raise AssertionError( "Attributions not returning a Tensor when expected.")
def _conductance_input_test_assert( self, model: Module, target_layer: Module, test_input: TensorOrTupleOfTensors, test_neuron: Union[int, Tuple[int, ...]], expected_input_conductance: Union[List[float], Tuple[List[List[float]], ...]], additional_input: Any = None, ) -> None: for internal_batch_size in (None, 1, 20): cond = NeuronConductance(model, target_layer) attributions = cond.attribute( test_input, test_neuron, target=0, n_steps=500, method="gausslegendre", additional_forward_args=additional_input, internal_batch_size=internal_batch_size, ) if isinstance(expected_input_conductance, tuple): for i in range(len(expected_input_conductance)): for j in range(len(expected_input_conductance[i])): assertArraysAlmostEqual( attributions[i][j:j + 1].squeeze(0).tolist(), expected_input_conductance[i][j], delta=0.1, ) else: if isinstance(attributions, Tensor): assertArraysAlmostEqual( attributions.squeeze(0).tolist(), expected_input_conductance, delta=0.1, ) else: raise AssertionError( "Attributions not returning a Tensor when expected.")
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, )