I don't understand the error. Is not the output means the model's output. If so, my predictor's output shape is (batch_size,1).
Here is the complete error trace,
----> 2 interpret_sentence(model, doc)
3 # for tv_batch in source_test_dataloader:
4 # xtv_batch=tv_batch[0]
5 # ytv_batch=tv_batch[1]
<ipython-input-46-789ee45a76d0> in interpret_sentence(model, doc)
42 reference_indices=reference_indices.to(device)
43 print(reference_indices.shape)
---> 44 attributions_ig, delta = lig.attribute(b_input_ids, reference_indices,target=2 , \
45 n_steps=100, return_convergence_delta=True)
46 print(attributions_ig.shape)
/opt/conda/lib/python3.8/site-packages/captum/attr/_core/layer/layer_integrated_gradients.py in attribute(self, inputs, baselines, target, additional_forward_args, n_steps, method, internal_batch_size, return_convergence_delta, attribute_to_layer_input)
350 else inps
351 )
--> 352 attributions = self.ig.attribute(
353 inputs_layer,
354 baselines=baselines_layer,
/opt/conda/lib/python3.8/site-packages/captum/attr/_core/integrated_gradients.py in attribute(self, inputs, baselines, target, additional_forward_args, n_steps, method, internal_batch_size, return_convergence_delta)
276
277 # grads: dim -> (bsz * #steps x inputs[0].shape[1:], ...)
--> 278 grads = _batched_operator(
279 self.gradient_func,
280 scaled_features_tpl,
/opt/conda/lib/python3.8/site-packages/captum/attr/_utils/batching.py in _batched_operator(operator, inputs, additional_forward_args, target_ind, internal_batch_size, **kwargs)
154 of the results of each batch.
155 """
--> 156 all_outputs = [
157 operator(
158 inputs=input,
/opt/conda/lib/python3.8/site-packages/captum/attr/_utils/batching.py in <listcomp>(.0)
155 """
156 all_outputs = [
--> 157 operator(
158 inputs=input,
159 additional_forward_args=additional,
/opt/conda/lib/python3.8/site-packages/captum/attr/_core/layer/layer_integrated_gradients.py in gradient_func(forward_fn, inputs, target_ind, additional_forward_args)
331 hook = self.layer.register_forward_hook(layer_forward_hook)
332
--> 333 output = _run_forward(
334 self.forward_func, tuple(), target_ind, additional_forward_args
335 )
/opt/conda/lib/python3.8/site-packages/captum/attr/_utils/common.py in _run_forward(forward_func, inputs, target, additional_forward_args)
503 else inputs
504 )
--> 505 return _select_targets(output, target)
506
507
/opt/conda/lib/python3.8/site-packages/captum/attr/_utils/common.py in _select_targets(output, target)
452 dims = len(output.shape)
453 if isinstance(target, (int, tuple)):
--> 454 return _verify_select_column(output, target)
455 elif isinstance(target, torch.Tensor):
456 if torch.numel(target) == 1 and isinstance(target.item(), int):
/opt/conda/lib/python3.8/site-packages/captum/attr/_utils/common.py in _verify_select_column(output, target)
439 ) -> Tensor:
440 target = cast(Tuple[int, ...], (target,) if isinstance(target, int) else target)
--> 441 assert (
442 len(target) <= len(output.shape) - 1
443 ), "Cannot choose target column with output shape %r." % (output.shape,)
AssertionError: Cannot choose target column with output shape torch.Size([100]).
Edit: I just noticed this 100 is coming from "n_step". If change n_step=200, the error shows 200. Other than this, I have no idea why this error happens and how to solve this. Can anybody help?
@mainulquraishi , the output of the model should be 2 dimensional for an integer-valued target. Currently, you have one dimensional output. We always assume that the first dimension is the number of examples. You want to make sure that the forward function returns an output in a shape [#example x #classes]
I am doing regression. For regression, should not be the output shape [#example] ?
Hi @mainulquraishi , the output shape is fine, but if the output shape is [#examples], you shouldn't provide the target argument when calling LayerIntegratedGradients. This argument should only be provided when there are multiple outputs per example and one needs to be selected, such as in a classification case. More information about this can be found in the documentation for the target parameter here. Hope this helps!
@mainulquraishi, do you still see the issue ?