def test_relu_deepliftshap_baselines_as_function(self): model = ReLULinearDeepLiftModel() x1 = torch.tensor([[-10.0, 1.0, -5.0]]) x2 = torch.tensor([[3.0, 3.0, 1.0]]) def gen_baselines(): b1 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) b2 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) return (b1, b2) def gen_baselines_with_inputs(inputs): b1 = inputs[0].mean(0, keepdim=True) b2 = inputs[1].mean(0, keepdim=True) return (b1, b2) def gen_baselines_returns_array(): b1 = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]] b2 = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]] return (b1, b2) inputs = (x1, x2) dl_shap = DeepLiftShap(model) self._deeplift_assert(model, dl_shap, inputs, gen_baselines) self._deeplift_assert(model, dl_shap, inputs, gen_baselines_with_inputs) with self.assertRaises(AssertionError): self._deeplift_assert( model, DeepLiftShap(model), inputs, gen_baselines_returns_array ) baselines = gen_baselines() attributions = dl_shap.attribute(inputs, baselines) attributions_with_func = dl_shap.attribute(inputs, gen_baselines) assertTensorAlmostEqual(self, attributions[0], attributions_with_func[0]) assertTensorAlmostEqual(self, attributions[1], attributions_with_func[1])
def test_relu_deepliftshap_baselines_as_func(self) -> None: model = ReLULinearDeepLiftModel(inplace=True) x1 = torch.tensor([[-10.0, 1.0, -5.0]]) x2 = torch.tensor([[3.0, 3.0, 1.0]]) def gen_baselines() -> Tuple[Tensor, ...]: b1 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) b2 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) return (b1, b2) def gen_baselines_scalar() -> Tuple[float, ...]: return (0.0, 0.0001) def gen_baselines_with_inputs( inputs: Tuple[Tensor, ...]) -> Tuple[Tensor, ...]: b1 = torch.cat([inputs[0], inputs[0] - 10]) b2 = torch.cat([inputs[1], inputs[1] - 10]) return (b1, b2) def gen_baselines_returns_array() -> Tuple[List[List[float]], ...]: b1 = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]] b2 = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]] return (b1, b2) inputs = (x1, x2) dl_shap = DeepLiftShap(model) self._deeplift_assert(model, dl_shap, inputs, gen_baselines) self._deeplift_assert(model, dl_shap, inputs, gen_baselines_with_inputs) with self.assertRaises(AssertionError): self._deeplift_assert(model, DeepLiftShap(model), inputs, gen_baselines_returns_array) with self.assertRaises(AssertionError): self._deeplift_assert(model, dl_shap, inputs, gen_baselines_scalar) baselines = gen_baselines() attributions = dl_shap.attribute(inputs, baselines) attributions_with_func = dl_shap.attribute(inputs, gen_baselines) assertTensorAlmostEqual(self, attributions[0], attributions_with_func[0]) assertTensorAlmostEqual(self, attributions[1], attributions_with_func[1])
def test_relu_deepliftshap_with_hypothetical_contrib_func(self) -> None: model = Conv1dDeepLiftModel() rand_seq_data = torch.abs(torch.randn(2, 4, 1000)) rand_seq_ref = torch.abs(torch.randn(3, 4, 1000)) dls = DeepLiftShap(model) attr = dls.attribute( rand_seq_data, rand_seq_ref, custom_attribution_func=_hypothetical_contrib_func, target=(0, 0), ) self.assertEqual(attr.shape, rand_seq_data.shape)
def test_relu_deepliftshap_with_custom_attr_func(self): def custom_attr_func(multipliers, inputs, baselines): return tuple(multiplier * 0.0 for multiplier in multipliers) model = ReLULinearDeepLiftModel() x1 = torch.tensor([[-10.0, 1.0, -5.0]]) x2 = torch.tensor([[3.0, 3.0, 1.0]]) b1 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) b2 = torch.tensor([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]) inputs = (x1, x2) baselines = (b1, b2) dls = DeepLiftShap(model) attr_w_func = dls.attribute(inputs, baselines, custom_attribution_func=custom_attr_func) assertTensorAlmostEqual(self, attr_w_func[0], [[0.0, 0.0, 0.0]], 0.0) assertTensorAlmostEqual(self, attr_w_func[1], [[0.0, 0.0, 0.0]], 0.0)