Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion captum/attr/_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def _select_targets(output: Tensor, target: TargetType) -> Tensor:

num_examples = output.shape[0]
dims = len(output.shape)
device = output.device
if isinstance(target, (int, tuple)):
return _verify_select_column(output, target)
elif isinstance(target, torch.Tensor):
Expand All @@ -467,7 +468,9 @@ def _select_targets(output: Tensor, target: TargetType) -> Tensor:
assert len(target) == num_examples, "Target list length does not match output!"
if isinstance(target[0], int):
assert dims == 2, "Output must be 2D to select tensor of targets."
return torch.gather(output, 1, torch.tensor(target).reshape(len(output), 1))
return torch.gather(
output, 1, torch.tensor(target, device=device).reshape(len(output), 1)
)
elif isinstance(target[0], tuple):
return torch.stack(
[
Expand Down
Loading