def test_linear_layer_deeplift_batch(self) -> None: model = ReLULinearModel(inplace=True) _, baselines = _create_inps_and_base_for_deeplift_neuron_layer_testing( ) x1 = torch.tensor( [[-10.0, 1.0, -5.0], [-10.0, 1.0, -5.0], [-10.0, 1.0, -5.0]], requires_grad=True, ) x2 = torch.tensor([[3.0, 3.0, 1.0], [3.0, 3.0, 1.0], [3.0, 3.0, 1.0]], requires_grad=True) inputs = (x1, x2) layer_dl = LayerDeepLift(model, model.l3) attributions, delta = layer_dl.attribute( inputs, baselines, attribute_to_layer_input=True, return_convergence_delta=True, ) assertTensorAlmostEqual(self, attributions[0], [[0.0, 15.0]]) assert_delta(self, delta) attributions, delta = layer_dl.attribute( inputs, baselines, attribute_to_layer_input=False, return_convergence_delta=True, ) assertTensorAlmostEqual(self, attributions, [[15.0]]) assert_delta(self, delta)
def test_linear_layer_deeplift(self) -> None: model = ReLULinearModel(inplace=True) inputs, baselines = _create_inps_and_base_for_deeplift_neuron_layer_testing( ) layer_dl = LayerDeepLift(model, model.l3) attributions, delta = layer_dl.attribute( inputs, baselines, attribute_to_layer_input=True, return_convergence_delta=True, ) assertTensorAlmostEqual(self, attributions[0], [[0.0, 15.0]]) assert_delta(self, delta)
def test_relu_layer_deeplift_add_args(self) -> None: model = ReLULinearModel() inputs, baselines = _create_inps_and_base_for_deeplift_neuron_layer_testing( ) layer_dl = LayerDeepLift(model, model.relu) attributions, delta = layer_dl.attribute( inputs, baselines, additional_forward_args=3.0, attribute_to_layer_input=True, return_convergence_delta=True, ) assertTensorAlmostEqual(self, attributions[0], [[0.0, 45.0]]) assert_delta(self, delta)
def test_relu_layer_deepliftshap(self) -> None: model = ReLULinearModel() ( inputs, baselines, ) = _create_inps_and_base_for_deepliftshap_neuron_layer_testing() layer_dl_shap = LayerDeepLiftShap(model, model.relu) attributions, delta = layer_dl_shap.attribute( inputs, baselines, attribute_to_layer_input=True, return_convergence_delta=True, ) assertTensorAlmostEqual(self, attributions[0], [[0.0, 15.0]]) assert_delta(self, delta)
def test_relu_layer_deeplift_multiple_output(self) -> None: model = BasicModel_MultiLayer(multi_input_module=True) inputs, baselines = _create_inps_and_base_for_deeplift_neuron_layer_testing( ) layer_dl = LayerDeepLift(model, model.multi_relu) attributions, delta = layer_dl.attribute( inputs[0], baselines[0], target=0, attribute_to_layer_input=False, return_convergence_delta=True, ) assertTensorTuplesAlmostEqual( self, attributions, ([[0.0, -1.0, -1.0, -1.0]], [[0.0, -1.0, -1.0, -1.0]])) assert_delta(self, delta)