Exemplo n.º 1
0
 def _guided_backprop_matching_assert(
     self, model: Module, output_layer: Module, test_input: TensorOrTupleOfTensors
 ):
     out = model(test_input)
     attrib = GuidedBackprop(model)
     neuron_attrib = NeuronGuidedBackprop(model, output_layer)
     for i in range(out.shape[1]):
         gbp_vals = attrib.attribute(test_input, target=i)
         neuron_gbp_vals = neuron_attrib.attribute(test_input, (i,))
         assertTensorAlmostEqual(self, gbp_vals, neuron_gbp_vals, delta=0.01)
Exemplo n.º 2
0
 def _guided_backprop_test_assert(self,
                                  model,
                                  test_input,
                                  expected,
                                  additional_input=None):
     guided_backprop = GuidedBackprop(model)
     attributions = guided_backprop.attribute(
         test_input, target=0, additional_forward_args=additional_input)
     for i in range(len(test_input)):
         assertTensorAlmostEqual(self,
                                 attributions[i],
                                 expected[i],
                                 delta=0.01)
Exemplo n.º 3
0
 def _guided_backprop_test_assert(
     self,
     model: Module,
     test_input: TensorOrTupleOfTensorsGeneric,
     expected: Tuple[List[List[float]], ...],
     additional_input: Any = None,
 ) -> None:
     guided_backprop = GuidedBackprop(model)
     attributions = guided_backprop.attribute(
         test_input, target=0, additional_forward_args=additional_input
     )
     for i in range(len(test_input)):
         assertTensorAlmostEqual(self, attributions[i], expected[i], delta=0.01)
Exemplo n.º 4
0
 def _guided_backprop_matching_assert(
     self,
     model: Module,
     output_layer: Module,
     test_input: TensorOrTupleOfTensorsGeneric,
 ):
     out = model(test_input)
     model_copy = copy.deepcopy(model)
     attrib = GuidedBackprop(model_copy)
     self.assertFalse(attrib.multiplies_by_inputs)
     neuron_attrib = NeuronGuidedBackprop(model, output_layer)
     for i in range(out.shape[1]):
         gbp_vals = attrib.attribute(test_input, target=i)
         neuron_gbp_vals = neuron_attrib.attribute(test_input, (i, ))
         assertTensorAlmostEqual(self,
                                 gbp_vals,
                                 neuron_gbp_vals,
                                 delta=0.01)