Skip to content

Control for when output from model is a scalar or a 1D tensor #1521

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 12 additions & 3 deletions captum/testing/helpers/basic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# pyre-strict

from typing import no_type_check, Optional, Tuple, Union
from typing import Dict, no_type_check, Optional, Tuple, Union

import torch
import torch.nn as nn
Expand Down Expand Up @@ -467,7 +467,9 @@ def __init__(
self.linear3.bias = nn.Parameter(torch.tensor([-1.0, 1.0]))

@no_type_check
def forward(self, x: Tensor, add_input: Optional[Tensor] = None) -> Tensor:
def forward(
self, x: Tensor, add_input: Optional[Tensor] = None
) -> Dict[str, Tensor]:
input = x if add_input is None else x + add_input
lin0_out = self.linear0(input)
lin1_out = self.linear1(lin0_out)
Expand All @@ -485,7 +487,14 @@ def forward(self, x: Tensor, add_input: Optional[Tensor] = None) -> Tensor:

lin3_out = self.linear3(lin1_out_alt).to(torch.int64)

return torch.cat((lin2_out, lin3_out), dim=1)
output_tensors = torch.cat((lin2_out, lin3_out), dim=1)

# we return a dictionary of tensors as an output to test the case
# where an output accessor is required
return {
"task {}".format(i + 1): output_tensors[:, i]
for i in range(output_tensors.shape[1])
}


class MultiRelu(nn.Module):
Expand Down