SaliencyExplainer#

class sdm.explain.SaliencyExplainer(signed: bool = False)#

Bases: object

Compute gradients with respect to preprocessed feature cells from the “Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps” paper.

import sdm
import torch

x_context = sdm.TableTensor.from_tensor(
    torch.tensor([
        [-3., 0.],
        [-2., 1.],
        [-1., 2.],
        [ 1., 0.],
        [ 2., 1.],
        [ 3., 2.],
    ], device="cuda"),
    columns=["signal", "noise"]
)
y_context = sdm.TableTensor.from_tensor(
    2 * x_context["signal"].numerical,
    columns=["target"],
)
x_query = sdm.TableTensor.from_tensor(
    torch.tensor([[-0.5, 1.], [0., 1.], [0.5, 1.]], device="cuda"),
    columns=["signal", "noise"],
)

model = sdm.models.TabICLv2(device="cuda")
model.fit(x_context, y_context)

explainer = sdm.explain.SaliencyExplainer()
out = explainer(model, x_query, output="q500")

# "signal" attributions:
out["signal"].numerical.squeeze()
# [5.7793, 4.3657, 7.9020]

# "noise" attributions:
out["noise"].numerical.squeeze()
# [0.1103, 0.0828, 0.1884]
Parameters:

signed (bool) – Whether to return signed gradients.

__call__(model: ICLModel, x: Tensor | TableTensor, *, output: str) → TableTensor#
__call__(model: ICLModel, x: Tensor | TableTensor, related_tables: RelatedTables[TableTensor], *, output: str) → tuple[TableTensor, RelatedTables[TableTensor]]

Explain the attributions of preprocessed feature cells.

Parameters:
  • model – The fitted model.

  • x – The feature tensor of query examples with shape [..., R, D] with R rows and D columns.

  • related_tables – Optional related context in case the model supports_related_tables.

  • output – The name of the predicted column to explain.

Returns:

The gradients of shape [..., R, D].