Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compute_expected_attribution_and_sq(attribution):
bsz = attribution.shape[0] // n_samples
attribution_shape = (bsz, n_samples)
if len(attribution.shape) > 1:
attribution_shape += attribution.shape[1:]
attribution = attribution.view(attribution_shape)
expected_attribution = attribution.mean(dim=1, keepdim=False)
expected_attribution_sq = torch.mean(attribution ** 2, dim=1, keepdim=False)
return expected_attribution, expected_attribution_sq
# Keeps track whether original input is a tuple or not before
# converting it into a tuple.
is_inputs_tuple = isinstance(inputs, tuple)
inputs = _format_input(inputs)
_validate_noise_tunnel_type(nt_type, SUPPORTED_NOISE_TUNNEL_TYPES)
delta = 0
inputs_with_noise = add_noise_to_inputs()
# if the algorithm supports targets, baselines and/or additional_forward_args
# they will be expanded based on the n_steps and corresponding kwargs
# variables will be updated accordingly
expand_and_update_baselines()
expand_and_update_additional_forward_args()
expand_and_update_target()
# smoothgrad_Attr(x) = 1 / n * sum(Attr(x + N(0, sigma^2))
attributions = self.attribution_method.attribute(inputs_with_noise, **kwargs)
return_convergence_delta = (
"return_convergence_delta" in kwargs and kwargs["return_convergence_delta"]
Examples::
>>> # SimpleClassifier takes a single input tensor of size Nx4x4,
>>> # and returns an Nx3 tensor of class probabilities.
>>> net = SimpleClassifier()
>>> # Generating random input with size 2 x 4 x 4
>>> input = torch.randn(2, 4, 4)
>>> # Defining Occlusion interpreter
>>> ablator = Occlusion(net)
>>> # Computes occlusion attribution, ablating each 3x3 patch,
>>> # shifting in each direction by the default of 1.
>>> attr = ablator.attribute(input, target=1, sliding_window_shapes=(3,3))
"""
formatted_inputs = _format_input(inputs)
# Formatting strides
strides = _format_and_verify_strides(strides, formatted_inputs)
# Formatting sliding window shapes
sliding_window_shapes = _format_and_verify_sliding_window_shapes(
sliding_window_shapes, formatted_inputs
)
# Construct tensors from sliding window shapes
sliding_window_tensors = tuple(
torch.ones(window_shape, device=formatted_inputs[i].device)
for i, window_shape in enumerate(sliding_window_shapes)
)
# Construct counts, defining number of steps to make of occlusion block in
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
>>> # and returns an Nx10 tensor of class probabilities.
>>> net = ImageClassifier()
>>> # Generating random input with size 2x3x3x32
>>> input = torch.randn(2, 3, 32, 32, requires_grad=True)
>>> # Defining InputXGradient interpreter
>>> input_x_gradient = InputXGradient(net)
>>> # Computes inputXgradient for class 4.
>>> attribution = input_x_gradient.attribute(input, target=4)
"""
# Keeps track whether original input is a tuple or not before
# converting it into a tuple.
is_inputs_tuple = isinstance(inputs, tuple)
inputs = _format_input(inputs)
gradient_mask = apply_gradient_requirements(inputs)
gradients = self.gradient_func(
self.forward_func, inputs, target, additional_forward_args
)
attributions = tuple(
input * gradient for input, gradient in zip(inputs, gradients)
)
undo_gradient_requirements(inputs, gradient_mask)
return _format_attributions(is_inputs_tuple, attributions)
>>> # It contains an attribute conv1, which is an instance of nn.conv2d,
>>> # and the output of this layer has dimensions Nx12x32x32.
>>> net = ImageClassifier()
>>> neuron_ig = NeuronGradient(net, net.conv1)
>>> input = torch.randn(2, 3, 32, 32, requires_grad=True)
>>> # To compute neuron attribution, we need to provide the neuron
>>> # index for which attribution is desired. Since the layer output
>>> # is Nx12x32x32, we need a tuple in the form (0..11,0..31,0..31)
>>> # which indexes a particular neuron in the layer output.
>>> # For this example, we choose the index (4,1,2).
>>> # Computes neuron gradient for neuron with
>>> # index (4,1,2).
>>> attribution = neuron_ig.attribute(input, (4,1,2))
"""
is_inputs_tuple = isinstance(inputs, tuple)
inputs = _format_input(inputs)
additional_forward_args = _format_additional_forward_args(
additional_forward_args
)
gradient_mask = apply_gradient_requirements(inputs)
_, input_grads = _forward_layer_eval_with_neuron_grads(
self.forward_func,
inputs,
self.layer,
additional_forward_args,
neuron_index,
device_ids=self.device_ids,
attribute_to_layer_input=attribute_to_neuron_input,
)
undo_gradient_requirements(inputs, gradient_mask)
def _batched_generator(
inputs, additional_forward_args=None, target_ind=None, internal_batch_size=None
):
"""
Returns a generator which returns corresponding chunks of size internal_batch_size
for both inputs and additional_forward_args. If batch size is None,
generator only includes original inputs and additional args.
"""
assert internal_batch_size is None or (
isinstance(internal_batch_size, int) and internal_batch_size > 0
), "Batch size must be greater than 0."
inputs = _format_input(inputs)
additional_forward_args = _format_additional_forward_args(additional_forward_args)
num_examples = inputs[0].shape[0]
if internal_batch_size is None:
yield inputs, additional_forward_args, target_ind
else:
for current_total in range(0, num_examples, internal_batch_size):
yield _tuple_splice_range(
inputs, current_total, current_total + internal_batch_size
), _tuple_splice_range(
additional_forward_args,
current_total,
current_total + internal_batch_size,
), target_ind[
current_total : current_total + internal_batch_size
] if isinstance(
target_ind, list
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
>>> # and returns an Nx10 tensor of class probabilities.
>>> # It contains an attribute conv4, which is an instance of nn.conv2d,
>>> # and the output of this layer has dimensions Nx50x8x8.
>>> # It is the last convolution layer, which is the recommended
>>> # use case for GuidedGradCAM.
>>> net = ImageClassifier()
>>> guided_gc = GuidedGradCam(net, net.conv4)
>>> input = torch.randn(2, 3, 32, 32, requires_grad=True)
>>> # Computes guided GradCAM attributions for class 3.
>>> # attribution size matches input size, Nx3x32x32
>>> attribution = guided_gc.attribute(input, 3)
"""
is_inputs_tuple = isinstance(inputs, tuple)
inputs = _format_input(inputs)
grad_cam_attr = self.grad_cam.attribute(
inputs=inputs,
target=target,
additional_forward_args=additional_forward_args,
attribute_to_layer_input=attribute_to_layer_input,
relu_attributions=True,
)
guided_backprop_attr = self.guided_backprop.attribute(
inputs=inputs,
target=target,
additional_forward_args=additional_forward_args,
)
output_attr = []
for i in range(len(inputs)):
try:
output_attr.append(
Examples::
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
>>> # and returns an Nx10 tensor of class probabilities.
>>> net = ImageClassifier()
>>> dl = DeepLift(net)
>>> input = torch.randn(2, 3, 32, 32, requires_grad=True)
>>> # Computes deeplift attribution scores for class 3.
>>> attribution = dl.attribute(input, target=3)
"""
# Keeps track whether original input is a tuple or not before
# converting it into a tuple.
is_inputs_tuple = isinstance(inputs, tuple)
inputs = _format_input(inputs)
baselines = _format_baseline(baselines, inputs)
gradient_mask = apply_gradient_requirements(inputs)
_validate_input(inputs, baselines)
# set hooks for baselines
warnings.warn(
"""Setting forward, backward hooks and attributes on non-linear
activations. The hooks and attributes will be removed
after the attribution is finished"""
)
self.model.apply(self._register_hooks_ref)
baselines = _tensorize_baseline(inputs, baselines)
>>> # It is the last convolution layer, which is the recommended
>>> # use case for GradCAM.
>>> net = ImageClassifier()
>>> layer_gc = LayerGradCam(net, net.conv4)
>>> input = torch.randn(2, 3, 32, 32, requires_grad=True)
>>> # Computes layer GradCAM for class 3.
>>> # attribution size matches layer output except for dimension
>>> # 1, so dimensions of attr would be Nx1x8x8.
>>> attr = layer_gc.attribute(input, 3)
>>> # GradCAM attributions are often upsampled and viewed as a
>>> # mask to the input, since the convolutional layer output
>>> # spatially matches the original input image.
>>> # This can be done with LayerAttribution's interpolate method.
>>> upsampled_attr = LayerAttribution.interpolate(attr, (32, 32))
"""
inputs = _format_input(inputs)
additional_forward_args = _format_additional_forward_args(
additional_forward_args
)
# Returns gradient of output with respect to
# hidden layer and hidden layer evaluated at each input.
layer_gradients, layer_eval = compute_layer_gradients_and_eval(
self.forward_func,
self.layer,
inputs,
target,
additional_forward_args,
device_ids=self.device_ids,
attribute_to_layer_input=attribute_to_layer_input,
)
summed_grads = torch.mean(
layer_gradients,
it is not guaranteed and depends on the specifics of the
`custom_attribution_func`.
Examples::
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
>>> # and returns an Nx10 tensor of class probabilities.
>>> net = ImageClassifier()
>>> # creates an instance of LayerDeepLift to interpret target
>>> # class 1 with respect to conv4 layer.
>>> dl = LayerDeepLift(net, net.conv4)
>>> input = torch.randn(1, 3, 32, 32, requires_grad=True)
>>> # Computes deeplift attribution scores for conv4 layer and class 3.
>>> attribution = dl.attribute(input, target=1)
"""
inputs = _format_input(inputs)
baselines = _format_baseline(baselines, inputs)
gradient_mask = apply_gradient_requirements(inputs)
_validate_input(inputs, baselines)
# set hooks for baselines
self.model.apply(self._register_hooks_ref)
baselines = _tensorize_baseline(inputs, baselines)
attr_baselines = _forward_layer_eval(
self.model,
baselines,
self.layer,
additional_forward_args=additional_forward_args,
attribute_to_layer_input=attribute_to_layer_input,