def test_simple_progress(self, mock_stderr) -> None: test_data = [1, 3, 5] desc = "test progress" progressed = progress(test_data, desc=desc, use_tqdm=False) assert list(progressed) == test_data assert mock_stderr.getvalue().startswith(f"\r{desc}: 0% 0/3") assert mock_stderr.getvalue().endswith(f"\r{desc}: 100% 3/3\n") # progress iterable without len but explicitly specify total def gen(): for n in test_data: yield n mock_stderr.seek(0) mock_stderr.truncate(0) progressed = progress(gen(), desc=desc, total=len(test_data), use_tqdm=False) assert list(progressed) == test_data assert mock_stderr.getvalue().startswith(f"\r{desc}: 0% 0/3") assert mock_stderr.getvalue().endswith(f"\r{desc}: 100% 3/3\n")
def test_simple_progress_update_manually(self, mock_stderr) -> None: desc = "test progress" p = progress(total=5, desc=desc, use_tqdm=False) p.update(0) p.update(2) p.update(2) p.update(1) p.close() assert mock_stderr.getvalue().startswith(f"\r{desc}: 0% 0/5") assert mock_stderr.getvalue().endswith(f"\r{desc}: 100% 5/5\n")
def test_progress_tqdm(self, mock_stderr) -> None: try: import tqdm # noqa: F401 except ImportError: raise unittest.SkipTest("Skipping tqdm test, tqdm not available.") test_data = [1, 3, 5] progressed = progress(test_data, desc="test progress") assert list(progressed) == test_data assert "test progress: " in mock_stderr.getvalue()
def test_simple_progress_without_total(self, mock_stderr) -> None: test_data = [1, 3, 5] desc = "test progress" def gen(): for n in test_data: yield n progressed = progress(gen(), desc=desc, use_tqdm=False) assert list(progressed) == test_data assert mock_stderr.getvalue().startswith(f"\r{desc}: ") assert mock_stderr.getvalue().endswith(f"\r{desc}: ...\n")
def _get_k_most_influential_helper( influence_src_dataloader: DataLoader, influence_batch_fn: Callable, inputs: Tuple[Any, ...], targets: Optional[Tensor], k: int = 5, proponents: bool = True, show_progress: bool = False, desc: Optional[str] = None, ) -> Tuple[Tensor, Tensor]: r""" Helper function that computes the quantities returned by `TracInCPBase._get_k_most_influential`, using a specific implementation that is constant memory. Args: influence_src_dataloader (DataLoader): The DataLoader, representing training data, for which we want to compute proponents / opponents. influence_batch_fn (Callable): A callable that will be called via `influence_batch_fn(inputs, targets, batch)`, where `batch` is a batch in the `influence_src_dataloader` argument. inputs (Tuple of Any): A batch of examples. Does not represent labels, which are passed as `targets`. targets (Tensor, optional): If computing TracIn scores on a loss function, these are the labels corresponding to the batch `inputs`. Default: None k (int, optional): The number of proponents or opponents to return per test instance. Default: 5 proponents (bool, optional): Whether seeking proponents (`proponents=True`) or opponents (`proponents=False`) Default: True show_progress (bool, optional): To compute the proponents (or opponents) for the batch of examples, we perform computation for each batch in training dataset `influence_src_dataloader`, If `show_progress`is true, the progress of this computation will be displayed. In particular, the number of batches for which the computation has been performed will be displayed. It will try to use tqdm if available for advanced features (e.g. time estimation). Otherwise, it will fallback to a simple output of progress. Default: False desc (str, optional): If `show_progress` is true, this is the description to show when displaying progress. If `desc` is none, no description is shown. Default: None Returns: (indices, influence_scores): `indices` is a torch.long Tensor that contains the indices of the proponents (or opponents) for each test example. Its dimension is `(inputs_batch_size, k)`, where `inputs_batch_size` is the number of examples in `inputs`. For example, if `proponents==True`, `indices[i][j]` is the index of the example in training dataset `influence_src_dataloader` with the k-th highest influence score for the j-th example in `inputs`. `indices` is a `torch.long` tensor so that it can directly be used to index other tensors. Each row of `influence_scores` contains the influence scores for a different test example, in sorted order. In particular, `influence_scores[i][j]` is the influence score of example `indices[i][j]` in training dataset `influence_src_dataloader` on example `i` in the test batch represented by `inputs` and `targets`. """ # For each test instance, maintain the best indices and corresponding distances # initially, these will be empty topk_indices = torch.Tensor().long() topk_tracin_scores = torch.Tensor() multiplier = 1.0 if proponents else -1.0 # needed to map from relative index in a batch fo index within entire `dataloader` num_instances_processed = 0 # if show_progress, create progress bar total: Optional[int] = None if show_progress: try: total = len(influence_src_dataloader) except AttributeError: pass influence_src_dataloader = progress( influence_src_dataloader, desc=desc, total=total, ) for batch in influence_src_dataloader: # calculate tracin_scores for the batch batch_tracin_scores = influence_batch_fn(inputs, targets, batch) batch_tracin_scores *= multiplier # get the top-k indices and tracin_scores for the batch batch_size = batch_tracin_scores.shape[1] batch_topk_tracin_scores, batch_topk_indices = torch.topk( batch_tracin_scores, min(batch_size, k), dim=1) batch_topk_indices = batch_topk_indices + num_instances_processed num_instances_processed += batch_size # combine the top-k for the batch with those for previously seen batches topk_indices = torch.cat([topk_indices, batch_topk_indices], dim=1) topk_tracin_scores = torch.cat( [topk_tracin_scores, batch_topk_tracin_scores], dim=1) # retain only the top-k in terms of tracin_scores topk_tracin_scores, topk_argsort = torch.topk( topk_tracin_scores, min(k, topk_indices.shape[1]), dim=1) topk_indices = torch.gather(topk_indices, dim=1, index=topk_argsort) # if seeking opponents, we were actually keeping track of negative tracin_scores topk_tracin_scores *= multiplier return topk_indices, topk_tracin_scores
def attribute( self, inputs: TensorOrTupleOfTensorsGeneric, baselines: BaselineType = None, target: TargetType = None, additional_forward_args: Any = None, feature_mask: Union[None, TensorOrTupleOfTensorsGeneric] = None, n_samples: int = 25, perturbations_per_eval: int = 1, show_progress: bool = False, ) -> TensorOrTupleOfTensorsGeneric: r""" NOTE: The feature_mask argument differs from other perturbation based methods, since feature indices can overlap across tensors. See the description of the feature_mask argument below for more details. Args: inputs (tensor or tuple of tensors): Input for which Shapley value sampling attributions are computed. If forward_func takes a single tensor as input, a single input tensor should be provided. If forward_func takes multiple tensors as input, a tuple of the input tensors should be provided. It is assumed that for all given input tensors, dimension 0 corresponds to the number of examples (aka batch size), and if multiple input tensors are provided, the examples must be aligned appropriately. baselines (scalar, tensor, tuple of scalars or tensors, optional): Baselines define reference value which replaces each feature when ablated. Baselines can be provided as: - a single tensor, if inputs is a single tensor, with exactly the same dimensions as inputs or the first dimension is one and the remaining dimensions match with inputs. - a single scalar, if inputs is a single tensor, which will be broadcasted for each input value in input tensor. - a tuple of tensors or scalars, the baseline corresponding to each tensor in the inputs' tuple can be: - either a tensor with matching dimensions to corresponding tensor in the inputs' tuple or the first dimension is one and the remaining dimensions match with the corresponding input tensor. - or a scalar, corresponding to a tensor in the inputs' tuple. This scalar value is broadcasted for corresponding input tensor. In the cases when `baselines` is not provided, we internally use zero scalar corresponding to each input tensor. Default: None target (int, tuple, tensor or list, optional): Output indices for which difference is computed (for classification cases, this is usually the target class). If the network returns a scalar value per example, no target index is necessary. For general 2D outputs, targets can be either: - a single integer or a tensor containing a single integer, which is applied to all input examples - a list of integers or a 1D tensor, with length matching the number of examples in inputs (dim 0). Each integer is applied as the target for the corresponding example. For outputs with > 2 dimensions, targets can be either: - A single tuple, which contains #output_dims - 1 elements. This target index is applied to all examples. - A list of tuples with length equal to the number of examples in inputs (dim 0), and each tuple containing #output_dims - 1 elements. Each tuple is applied as the target for the corresponding example. Default: None additional_forward_args (any, optional): If the forward function requires additional arguments other than the inputs for which attributions should not be computed, this argument can be provided. It must be either a single additional argument of a Tensor or arbitrary (non-tuple) type or a tuple containing multiple additional arguments including tensors or any arbitrary python types. These arguments are provided to forward_func in order following the arguments in inputs. For a tensor, the first dimension of the tensor must correspond to the number of examples. For all other types, the given argument is used for all forward evaluations. Note that attributions are not computed with respect to these arguments. Default: None feature_mask (tensor or tuple of tensors, optional): feature_mask defines a mask for the input, grouping features which should be added together. feature_mask should contain the same number of tensors as inputs. Each tensor should be the same size as the corresponding input or broadcastable to match the input tensor. Values across all tensors should be integers in the range 0 to num_features - 1, and indices corresponding to the same feature should have the same value. Note that features are grouped across tensors (unlike feature ablation and occlusion), so if the same index is used in different tensors, those features are still grouped and added simultaneously. If the forward function returns a single scalar per batch, we enforce that the first dimension of each mask must be 1, since attributions are returned batch-wise rather than per example, so the attributions must correspond to the same features (indices) in each input example. If None, then a feature mask is constructed which assigns each scalar within a tensor as a separate feature Default: None n_samples (int, optional): The number of feature permutations tested. Default: `25` if `n_samples` is not provided. perturbations_per_eval (int, optional): Allows multiple ablations to be processed simultaneously in one call to forward_fn. Each forward pass will contain a maximum of perturbations_per_eval * #examples samples. For DataParallel models, each batch is split among the available devices, so evaluations on each available device contain at most (perturbations_per_eval * #examples) / num_devices samples. If the forward function returns a single scalar per batch, perturbations_per_eval must be set to 1. Default: 1 show_progress (bool, optional): Displays the progress of computation. It will try to use tqdm if available for advanced features (e.g. time estimation). Otherwise, it will fallback to a simple output of progress. Default: False Returns: *tensor* or tuple of *tensors* of **attributions**: - **attributions** (*tensor* or tuple of *tensors*): The attributions with respect to each input feature. If the forward function returns a scalar value per example, attributions will be the same size as the provided inputs, with each value providing the attribution of the corresponding input index. If the forward function returns a scalar per batch, then attribution tensor(s) will have first dimension 1 and the remaining dimensions will match the input. If a single tensor is provided as inputs, a single tensor is returned. If a tuple is provided for inputs, a tuple of corresponding sized tensors is returned. 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 ShapleyValueSampling interpreter >>> svs = ShapleyValueSampling(net) >>> # Computes attribution, taking random orderings >>> # of the 16 features and computing the output change when adding >>> # each feature. We average over 200 trials (random permutations). >>> attr = svs.attribute(input, target=1, n_samples=200) >>> # Alternatively, we may want to add features in groups, e.g. >>> # grouping each 2x2 square of the inputs and adding them together. >>> # This can be done by creating a feature mask as follows, which >>> # defines the feature groups, e.g.: >>> # +---+---+---+---+ >>> # | 0 | 0 | 1 | 1 | >>> # +---+---+---+---+ >>> # | 0 | 0 | 1 | 1 | >>> # +---+---+---+---+ >>> # | 2 | 2 | 3 | 3 | >>> # +---+---+---+---+ >>> # | 2 | 2 | 3 | 3 | >>> # +---+---+---+---+ >>> # With this mask, all inputs with the same value are added >>> # together, and the attribution for each input in the same >>> # group (0, 1, 2, and 3) per example are the same. >>> # The attributions can be calculated as follows: >>> # feature mask has dimensions 1 x 4 x 4 >>> feature_mask = torch.tensor([[[0,0,1,1],[0,0,1,1], >>> [2,2,3,3],[2,2,3,3]]]) >>> attr = svs.attribute(input, target=1, feature_mask=feature_mask) """ # Keeps track whether original input is a tuple or not before # converting it into a tuple. is_inputs_tuple = _is_tuple(inputs) inputs, baselines = _format_input_baseline(inputs, baselines) additional_forward_args = _format_additional_forward_args( additional_forward_args ) feature_mask = ( _format_tensor_into_tuples(feature_mask) if feature_mask is not None else None ) assert ( isinstance(perturbations_per_eval, int) and perturbations_per_eval >= 1 ), "Ablations per evaluation must be at least 1." with torch.no_grad(): baselines = _tensorize_baseline(inputs, baselines) num_examples = inputs[0].shape[0] if feature_mask is None: feature_mask, total_features = _construct_default_feature_mask(inputs) else: total_features = int( max(torch.max(single_mask).item() for single_mask in feature_mask) + 1 ) if show_progress: attr_progress = progress( desc=f"{self.get_name()} attribution", total=self._get_n_evaluations( total_features, n_samples, perturbations_per_eval ) + 1, # add 1 for the initial eval ) attr_progress.update(0) initial_eval = _run_forward( self.forward_func, baselines, target, additional_forward_args ) if show_progress: attr_progress.update() agg_output_mode = _find_output_mode_and_verify( initial_eval, num_examples, perturbations_per_eval, feature_mask ) # Initialize attribution totals and counts total_attrib = [ torch.zeros_like( input[0:1] if agg_output_mode else input, dtype=torch.float ) for input in inputs ] iter_count = 0 # Iterate for number of samples, generate a permutation of the features # and evalute the incremental increase for each feature. for feature_permutation in self.permutation_generator( total_features, n_samples ): iter_count += 1 prev_results = initial_eval for ( current_inputs, current_add_args, current_target, current_masks, ) in self._perturbation_generator( inputs, additional_forward_args, target, baselines, feature_mask, feature_permutation, perturbations_per_eval, ): if sum(torch.sum(mask).item() for mask in current_masks) == 0: warnings.warn( "Feature mask is missing some integers between 0 and " "num_features, for optimal performance, make sure each" " consecutive integer corresponds to a feature." ) # modified_eval dimensions: 1D tensor with length # equal to #num_examples * #features in batch modified_eval = _run_forward( self.forward_func, current_inputs, current_target, current_add_args, ) if show_progress: attr_progress.update() if agg_output_mode: eval_diff = modified_eval - prev_results prev_results = modified_eval else: all_eval = torch.cat((prev_results, modified_eval), dim=0) eval_diff = all_eval[num_examples:] - all_eval[:-num_examples] prev_results = all_eval[-num_examples:] for j in range(len(total_attrib)): current_eval_diff = eval_diff if not agg_output_mode: # current_eval_diff dimensions: # (#features in batch, #num_examples, 1,.. 1) # (contains 1 more dimension than inputs). This adds extra # dimensions of 1 to make the tensor broadcastable with the # inputs tensor. current_eval_diff = current_eval_diff.reshape( (-1, num_examples) + (len(inputs[j].shape) - 1) * (1,) ) total_attrib[j] += ( current_eval_diff * current_masks[j].float() ).sum(dim=0) if show_progress: attr_progress.close() # Divide total attributions by number of random permutations and return # formatted attributions. attrib = tuple( tensor_attrib_total / iter_count for tensor_attrib_total in total_attrib ) formatted_attr = _format_output(is_inputs_tuple, attrib) return formatted_attr
def attribute( self, inputs: TensorOrTupleOfTensorsGeneric, baselines: BaselineType = None, target: TargetType = None, additional_forward_args: Any = None, feature_mask: Union[None, Tensor, Tuple[Tensor, ...]] = None, perturbations_per_eval: int = 1, show_progress: bool = False, **kwargs: Any, ) -> TensorOrTupleOfTensorsGeneric: r""" Args: inputs (tensor or tuple of tensors): Input for which ablation attributions are computed. If forward_func takes a single tensor as input, a single input tensor should be provided. If forward_func takes multiple tensors as input, a tuple of the input tensors should be provided. It is assumed that for all given input tensors, dimension 0 corresponds to the number of examples (aka batch size), and if multiple input tensors are provided, the examples must be aligned appropriately. baselines (scalar, tensor, tuple of scalars or tensors, optional): Baselines define reference value which replaces each feature when ablated. Baselines can be provided as: - a single tensor, if inputs is a single tensor, with exactly the same dimensions as inputs or broadcastable to match the dimensions of inputs - a single scalar, if inputs is a single tensor, which will be broadcasted for each input value in input tensor. - a tuple of tensors or scalars, the baseline corresponding to each tensor in the inputs' tuple can be: - either a tensor with matching dimensions to corresponding tensor in the inputs' tuple or the first dimension is one and the remaining dimensions match with the corresponding input tensor. - or a scalar, corresponding to a tensor in the inputs' tuple. This scalar value is broadcasted for corresponding input tensor. In the cases when `baselines` is not provided, we internally use zero scalar corresponding to each input tensor. Default: None target (int, tuple, tensor or list, optional): Output indices for which gradients are computed (for classification cases, this is usually the target class). If the network returns a scalar value per example, no target index is necessary. For general 2D outputs, targets can be either: - a single integer or a tensor containing a single integer, which is applied to all input examples - a list of integers or a 1D tensor, with length matching the number of examples in inputs (dim 0). Each integer is applied as the target for the corresponding example. For outputs with > 2 dimensions, targets can be either: - A single tuple, which contains #output_dims - 1 elements. This target index is applied to all examples. - A list of tuples with length equal to the number of examples in inputs (dim 0), and each tuple containing #output_dims - 1 elements. Each tuple is applied as the target for the corresponding example. Default: None additional_forward_args (any, optional): If the forward function requires additional arguments other than the inputs for which attributions should not be computed, this argument can be provided. It must be either a single additional argument of a Tensor or arbitrary (non-tuple) type or a tuple containing multiple additional arguments including tensors or any arbitrary python types. These arguments are provided to forward_func in order following the arguments in inputs. For a tensor, the first dimension of the tensor must correspond to the number of examples. For all other types, the given argument is used for all forward evaluations. Note that attributions are not computed with respect to these arguments. Default: None feature_mask (tensor or tuple of tensors, optional): feature_mask defines a mask for the input, grouping features which should be ablated together. feature_mask should contain the same number of tensors as inputs. Each tensor should be the same size as the corresponding input or broadcastable to match the input tensor. Each tensor should contain integers in the range 0 to num_features - 1, and indices corresponding to the same feature should have the same value. Note that features within each input tensor are ablated independently (not across tensors). If the forward function returns a single scalar per batch, we enforce that the first dimension of each mask must be 1, since attributions are returned batch-wise rather than per example, so the attributions must correspond to the same features (indices) in each input example. If None, then a feature mask is constructed which assigns each scalar within a tensor as a separate feature, which is ablated independently. Default: None perturbations_per_eval (int, optional): Allows ablation of multiple features to be processed simultaneously in one call to forward_fn. Each forward pass will contain a maximum of perturbations_per_eval * #examples samples. For DataParallel models, each batch is split among the available devices, so evaluations on each available device contain at most (perturbations_per_eval * #examples) / num_devices samples. If the forward function's number of outputs does not change as the batch size grows (e.g. if it outputs a scalar value), you must set perturbations_per_eval to 1 and use a single feature mask to describe the features for all examples in the batch. Default: 1 show_progress (bool, optional): Displays the progress of computation. It will try to use tqdm if available for advanced features (e.g. time estimation). Otherwise, it will fallback to a simple output of progress. Default: False **kwargs (Any, optional): Any additional arguments used by child classes of FeatureAblation (such as Occlusion) to construct ablations. These arguments are ignored when using FeatureAblation directly. Default: None Returns: *tensor* or tuple of *tensors* of **attributions**: - **attributions** (*tensor* or tuple of *tensors*): The attributions with respect to each input feature. If the forward function returns a scalar value per example, attributions will be the same size as the provided inputs, with each value providing the attribution of the corresponding input index. If the forward function returns a scalar per batch, then attribution tensor(s) will have first dimension 1 and the remaining dimensions will match the input. If a single tensor is provided as inputs, a single tensor is returned. If a tuple of tensors is provided for inputs, a tuple of corresponding sized tensors is returned. 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 FeatureAblation interpreter >>> ablator = FeatureAblation(net) >>> # Computes ablation attribution, ablating each of the 16 >>> # scalar input independently. >>> attr = ablator.attribute(input, target=1) >>> # Alternatively, we may want to ablate features in groups, e.g. >>> # grouping each 2x2 square of the inputs and ablating them together. >>> # This can be done by creating a feature mask as follows, which >>> # defines the feature groups, e.g.: >>> # +---+---+---+---+ >>> # | 0 | 0 | 1 | 1 | >>> # +---+---+---+---+ >>> # | 0 | 0 | 1 | 1 | >>> # +---+---+---+---+ >>> # | 2 | 2 | 3 | 3 | >>> # +---+---+---+---+ >>> # | 2 | 2 | 3 | 3 | >>> # +---+---+---+---+ >>> # With this mask, all inputs with the same value are ablated >>> # simultaneously, and the attribution for each input in the same >>> # group (0, 1, 2, and 3) per example are the same. >>> # The attributions can be calculated as follows: >>> # feature mask has dimensions 1 x 4 x 4 >>> feature_mask = torch.tensor([[[0,0,1,1],[0,0,1,1], >>> [2,2,3,3],[2,2,3,3]]]) >>> attr = ablator.attribute(input, target=1, feature_mask=feature_mask) """ # Keeps track whether original input is a tuple or not before # converting it into a tuple. is_inputs_tuple = _is_tuple(inputs) inputs, baselines = _format_input_baseline(inputs, baselines) additional_forward_args = _format_additional_forward_args( additional_forward_args) num_examples = inputs[0].shape[0] feature_mask = _format_input( feature_mask) if feature_mask is not None else None assert ( isinstance(perturbations_per_eval, int) and perturbations_per_eval >= 1 ), "Perturbations per evaluation must be an integer and at least 1." with torch.no_grad(): if show_progress: feature_counts = self._get_feature_counts( inputs, feature_mask, **kwargs) total_forwards = (sum( math.ceil(count / perturbations_per_eval) for count in feature_counts) + 1 ) # add 1 for the initial eval attr_progress = progress(desc=f"{self.get_name()} attribution", total=total_forwards) attr_progress.update(0) # Computes initial evaluation with all features, which is compared # to each ablated result. initial_eval = _run_forward(self.forward_func, inputs, target, additional_forward_args) if show_progress: attr_progress.update() agg_output_mode = FeatureAblation._find_output_mode( perturbations_per_eval, feature_mask) # get as a 2D tensor (if it is not a scalar) if isinstance(initial_eval, torch.Tensor): initial_eval = initial_eval.reshape(1, -1) num_outputs = initial_eval.shape[1] else: num_outputs = 1 if not agg_output_mode: assert ( isinstance(initial_eval, torch.Tensor) and num_outputs == num_examples ), ("expected output of `forward_func` to have " + "`batch_size` elements for perturbations_per_eval > 1 " + "and all feature_mask.shape[0] > 1") # Initialize attribution totals and counts attrib_type = cast( dtype, initial_eval.dtype if isinstance(initial_eval, Tensor) else type(initial_eval), ) total_attrib = [ torch.zeros( (num_outputs, ) + input.shape[1:], dtype=attrib_type, device=input.device, ) for input in inputs ] # Weights are used in cases where ablations may be overlapping. if self.use_weights: weights = [ torch.zeros((num_outputs, ) + input.shape[1:], device=input.device).float() for input in inputs ] # Iterate through each feature tensor for ablation for i in range(len(inputs)): # Skip any empty input tensors if torch.numel(inputs[i]) == 0: continue for ( current_inputs, current_add_args, current_target, current_mask, ) in self._ith_input_ablation_generator( i, inputs, additional_forward_args, target, baselines, feature_mask, perturbations_per_eval, **kwargs, ): # modified_eval dimensions: 1D tensor with length # equal to #num_examples * #features in batch modified_eval = _run_forward( self.forward_func, current_inputs, current_target, current_add_args, ) if show_progress: attr_progress.update() # (contains 1 more dimension than inputs). This adds extra # dimensions of 1 to make the tensor broadcastable with the inputs # tensor. if not isinstance(modified_eval, torch.Tensor): eval_diff = initial_eval - modified_eval else: if not agg_output_mode: assert ( modified_eval.numel() == current_inputs[0].shape[0] ), """expected output of forward_func to grow with batch_size. If this is not the case for your model please set perturbations_per_eval = 1""" eval_diff = (initial_eval - modified_eval.reshape( (-1, num_outputs)) ).reshape((-1, num_outputs) + (len(inputs[i].shape) - 1) * (1, )) eval_diff = eval_diff.to(total_attrib[i].device) if self.use_weights: weights[i] += current_mask.float().sum(dim=0) total_attrib[i] += (eval_diff * current_mask.to(attrib_type)).sum( dim=0) if show_progress: attr_progress.close() # Divide total attributions by counts and return formatted attributions if self.use_weights: attrib = tuple( single_attrib.float() / weight for single_attrib, weight in zip(total_attrib, weights)) else: attrib = tuple(total_attrib) _result = _format_output(is_inputs_tuple, attrib) return _result