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)
def __init__( self, forward_func: Callable, layer: Module, device_ids: Optional[List[int]] = None, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it layer (torch.nn.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 the attribution of each neuron in the input or output of this layer. 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. """ LayerAttribution.__init__(self, forward_func, layer, device_ids=device_ids) GradientAttribution.__init__(self, forward_func) self.ig = IntegratedGradients(forward_func)
def __init__( self, model: Module, layer: Module, device_ids: Union[None, List[int]] = None ) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. Model cannot contain any in-place ReLU submodules; these are not supported by the register_full_backward_hook PyTorch API starting from PyTorch v1.9. layer (Module): Layer for which neuron attributions are computed. Attributions for a particular neuron in the output of this layer are computed using the argument neuron_selector in the attribute method. Currently, only layers with a single tensor output are supported. 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.guided_backprop = GuidedBackprop(model)
def __init__(self, forward_func: Callable, multiply_by_inputs=True) -> None: r""" Args: forward_func (function): The forward function of the model or any modification of it multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in then this type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of gradient shap, if `multiply_by_inputs` is set to True, the sensitivity scores of scaled inputs are being multiplied by (inputs - baselines). """ GradientAttribution.__init__(self, forward_func) self._multiply_by_inputs = multiply_by_inputs
def __init__(self, model: Module, multiply_by_inputs: bool = True) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in then that type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of DeepLift, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by (inputs - baselines). This flag applies only if `custom_attribution_func` is set to None. """ GradientAttribution.__init__(self, model) self.model = model self.forward_handles: List[RemovableHandle] = [] self.backward_handles: List[RemovableHandle] = [] self._multiply_by_inputs = multiply_by_inputs
def __init__( self, model: Module, layer: Module, multiply_by_inputs: bool = True ) -> None: r""" Args: model (torch.nn.Module): The reference to PyTorch model instance. layer (torch.nn.Module): Layer for which neuron attributions are computed. Attributions for a particular neuron for the input or output of this layer are computed using the argument neuron_selector in the attribute method. Currently, only layers with a single tensor input and output are supported. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in then that type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of Neuron DeepLift Shap, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by (inputs - baselines). This flag applies only if `custom_attribution_func` is set to None. """ NeuronAttribution.__init__(self, model, layer) GradientAttribution.__init__(self, model) self._multiply_by_inputs = multiply_by_inputs
def __init__(self, forward_func: Callable) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it """ GradientAttribution.__init__(self, forward_func)
def __init__(self, model: Module) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. """ GradientAttribution.__init__(self, model) self.model = model self.forward_handles: List[RemovableHandle] = [] self.backward_handles: List[RemovableHandle] = []
def __init__(self, model: Module) -> None: r""" Args: model (module): The forward function of the model or any modification of it. Custom rules for a given layer need to be defined as attribute `module.rule` and need to be of type PropagationRule. If no rule is specified for a layer, a pre-defined default rule for the module type is used. """ GradientAttribution.__init__(self, model) self.model = model self._check_rules()
def __init__( self, forward_func: Callable, layer: Module, device_ids: Union[None, List[int]] = None, multiply_by_inputs: bool = True, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it layer (torch.nn.Module): Layer for which neuron attributions are computed. Attributions for a particular neuron in the input or output of this layer are computed using the argument neuron_selector in the attribute method. Currently, only layers with a single tensor input or output are supported. layer (torch.nn.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. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in then that type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of Neuron Conductance, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by (inputs - baselines). """ NeuronAttribution.__init__(self, forward_func, layer, device_ids) GradientAttribution.__init__(self, forward_func) self._multiply_by_inputs = multiply_by_inputs
def __init__(self, model: Module, use_relu_grad_output: bool = False): r""" Args: model (nn.Module): The reference to PyTorch model instance. """ GradientAttribution.__init__(self, model) self.model = model self.backward_hooks: List[RemovableHandle] = [] self.use_relu_grad_output = use_relu_grad_output assert isinstance(self.model, torch.nn.Module), ( "Given model must be an instance of torch.nn.Module to properly hook" " ReLU layers." )
def __init__( self, forward_func: Callable, m: float = 20.0, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modificationof it m (float): The hyperparameter forRiemman approximation of the integration (https://arxiv.org/abs/1703.01365). """ GradientAttribution.__init__(self, forward_func) self. self.m = m
def __init__(self, model: Module) -> None: r""" Args: model (module): The forward function of the model or any modification of it. Custom rules for a given layer need to be defined as attribute `module.rule` and need to be of type PropagationRule. If no rule is specified for a layer, a pre-defined default rule for the module type is used. Model cannot contain any in-place nonlinear submodules; these are not supported by the register_full_backward_hook PyTorch API starting from PyTorch v1.9. """ GradientAttribution.__init__(self, model) self.model = model self._check_rules()
def __init__( self, forward_func: Callable, layer: ModuleOrModuleList, device_ids: Union[None, List[int]] = None, multiply_by_inputs: bool = True, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it layer (torch.nn.Module or list(torch.nn.Module)): Layer or layers 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. If multiple layers are provided, attributions are returned as a list, each element corresponding to the attributions of the corresponding layer. 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. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in, then this type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of layer gradient x activation, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by layer activations for inputs. """ LayerAttribution.__init__(self, forward_func, layer, device_ids) GradientAttribution.__init__(self, forward_func) self._multiply_by_inputs = multiply_by_inputs
def __init__(self, model: Module, layer: Module, device_ids: Union[None, List[int]] = None) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. layer (torch.nn.Module): Layer for which GradCAM attributions are computed. Currently, only layers with a single tensor output are supported. 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. """ GradientAttribution.__init__(self, model) self.grad_cam = LayerGradCam(model, layer, device_ids) self.guided_backprop = GuidedBackprop(model)
def __init__( self, model: Module, multiply_by_inputs: bool = True, eps: float = 1e-10, ) -> None: r""" Args: model (nn.Module): The reference to PyTorch model instance. Model cannot contain any in-place nonlinear submodules; these are not supported by the register_full_backward_hook PyTorch API starting from PyTorch v1.9. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in then that type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of DeepLift, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by (inputs - baselines). This flag applies only if `custom_attribution_func` is set to None. eps (float, optional): A value at which to consider output/input change significant when computing the gradients for non-linear layers. This is useful to adjust, depending on your model's bit depth, to avoid numerical issues during the gradient computation. Default: 1e-10 """ GradientAttribution.__init__(self, model) self.model = model self.eps = eps self.forward_handles: List[RemovableHandle] = [] self.backward_handles: List[RemovableHandle] = [] self._multiply_by_inputs = multiply_by_inputs
def __init__( self, forward_func: Callable, layer: Module, device_ids: Union[None, List[int]] = None, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it layer (torch.nn.Module): Layer for which attributions are computed. Output size of attribute matches this layer's output dimensions, except for dimension 2, which will be 1, since GradCAM sums over channels. 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. """ LayerAttribution.__init__(self, forward_func, layer, device_ids) GradientAttribution.__init__(self, forward_func)
def __init__( self, forward_func: Callable, layer: ModuleOrModuleList, device_ids: Union[None, List[int]] = None, multiply_by_inputs: bool = True, ) -> None: r""" Args: forward_func (callable): The forward function of the model or any modification of it layer (ModuleOrModuleList): Layer or list of layers for which attributions are computed. For each layer the output size of the attribute matches this layer's input or output dimensions, depending on whether we attribute to the inputs or outputs of the layer, corresponding to the attribution of each neuron in the input or output of this layer. Please note that layers to attribute on cannot be dependent on each other. That is, a subset of layers in `layer` cannot produce the inputs for another layer. For example, if your model is of a simple linked-list based graph structure (think nn.Sequence), e.g. x -> l1 -> l2 -> l3 -> output. If you pass in any one of those layers, you cannot pass in another due to the dependence, e.g. if you pass in l2 you cannot pass in l1 or l3. 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. multiply_by_inputs (bool, optional): Indicates whether to factor model inputs' multiplier in the final attribution scores. In the literature this is also known as local vs global attribution. If inputs' multiplier isn't factored in, then this type of attribution method is also called local attribution. If it is, then that type of attribution method is called global. More detailed can be found here: https://arxiv.org/abs/1711.06104 In case of layer integrated gradients, if `multiply_by_inputs` is set to True, final sensitivity scores are being multiplied by layer activations for inputs - layer activations for baselines. """ LayerAttribution.__init__(self, forward_func, layer, device_ids=device_ids) GradientAttribution.__init__(self, forward_func) self.ig = IntegratedGradients(forward_func, multiply_by_inputs) if isinstance(layer, list) and len(layer) > 1: warnings.warn( "Multiple layers provided. Please ensure that each layer is" "**not** solely solely dependent on the outputs of" "another layer. Please refer to the documentation for more" "detail.")