Ejemplo n.º 1
0
 def _deconv_matching_assert(self, model, output_layer, test_input):
     out = model(test_input)
     attrib = Deconvolution(model)
     neuron_attrib = NeuronDeconvolution(model, output_layer)
     for i in range(out.shape[1]):
         deconv_vals = attrib.attribute(test_input, target=i)
         neuron_deconv_vals = neuron_attrib.attribute(test_input, (i, ))
         assertTensorAlmostEqual(self,
                                 deconv_vals,
                                 neuron_deconv_vals,
                                 delta=0.01)
Ejemplo n.º 2
0
 def _deconv_test_assert(self,
                         model,
                         test_input,
                         expected,
                         additional_input=None):
     deconv = Deconvolution(model)
     attributions = deconv.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)
Ejemplo n.º 3
0
 def _deconv_test_assert(
     self,
     model: Module,
     test_input: TensorOrTupleOfTensorsGeneric,
     expected: Tuple[List[List[float]], ...],
     additional_input: Any = None,
 ) -> None:
     deconv = Deconvolution(model)
     attributions = deconv.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)
Ejemplo n.º 4
0
 def _deconv_matching_assert(
     self,
     model: Module,
     output_layer: Module,
     test_input: TensorOrTupleOfTensorsGeneric,
 ) -> None:
     out = model(test_input)
     attrib = Deconvolution(model)
     self.assertFalse(attrib.multiplies_by_inputs)
     neuron_attrib = NeuronDeconvolution(model, output_layer)
     for i in range(out.shape[1]):
         deconv_vals = attrib.attribute(test_input, target=i)
         neuron_deconv_vals = neuron_attrib.attribute(test_input, (i,))
         assertTensorAlmostEqual(self, deconv_vals, neuron_deconv_vals, delta=0.01)
Ejemplo n.º 5
0
    def __init__(self,
                 model: Module,
                 layer: Module,
                 device_ids: Union[None, List[int]] = None) -> None:
        r"""
        Args:

            model (Module):  The reference to PyTorch model instance.
            layer (Module): Layer for which attributions are computed.
                          Output size of attribute matches this layer's input or
                          output dimensions, depending on whether we attribute to
                          the inputs or outputs of the layer, corresponding to
                          attribution of each neuron in the input or output of
                          this layer.
                          Currently, it is assumed that the inputs or the outputs
                          of the layer, depending on which one is used for
                          attribution, can only be a single tensor.
            device_ids (list(int)): Device ID list, necessary only if forward_func
                          applies a DataParallel model. This allows reconstruction of
                          intermediate outputs from batched results across devices.
                          If forward_func is given as the DataParallel model itself,
                          then it is not necessary to provide this argument.
        """
        NeuronAttribution.__init__(self, model, layer, device_ids)
        GradientAttribution.__init__(self, model)
        self.deconv = Deconvolution(model)