pyhealth.interpret.methods.ig_gim#
Overview#
pyhealth.interpret.methods.IntegratedGradientGIM runs the standard
Integrated Gradients path integral with the Gradient Interaction Modifications
(GIM) backward rules applied at every step. This removes gradient artefacts
caused by softmax redistribution and LayerNorm re-centring. The model must
implement forward_from_embedding() and get_embedding_model(). See also
pyhealth.interpret.methods.gim and
pyhealth.interpret.methods.integrated_gradients.
API Reference#
- class pyhealth.interpret.methods.IntegratedGradientGIM(model, temperature=2.0, steps=50)[source]#
Bases:
BaseInterpreterIntegrated Gradients with GIM backward-pass modifications.
This interpreter runs the standard IG path-integral computation but wraps every forward/backward step inside a GIM context so that the accumulated gradients are free of the self-repair artefacts caused by softmax redistribution and LayerNorm re-centring.
- Parameters:
model (
BaseModel) – Trained PyHealth model that implementsforward_from_embedding()andget_embedding_model().temperature (
float) – Softmax temperature used for the TSG backward rule. Values > 1 flatten the softmax Jacobian, counteracting gradient suppression.2.0is the paper’s recommended default.steps (
int) – Default number of Riemann-sum interpolation steps. Can be overridden per call inattribute().
Examples
>>> import torch >>> from pyhealth.datasets import create_sample_dataset, get_dataloader >>> from pyhealth.models import Transformer >>> from pyhealth.interpret.methods.ig_gim import IntegratedGradientGIM >>> >>> # Assume ``dataset`` and a trained Transformer are available. >>> model = Transformer(dataset=dataset).eval() >>> ig_gim = IntegratedGradientGIM(model, temperature=2.0, steps=50) >>> >>> batch = next(iter(get_dataloader(dataset, batch_size=1))) >>> attributions = ig_gim.attribute(**batch) >>> print({k: v.shape for k, v in attributions.items()})
Initialize the base interpreter.
- Parameters:
model (
BaseModel) – A trained PyHealth model to interpret. The model should be in evaluation mode during interpretation.
Note
Subclasses should call super().__init__(model) and then perform any method-specific initialization.
- attribute(baseline=None, steps=None, target_class_idx=None, **kwargs)[source]#
Compute IG-GIM attributions for a batch.
- Parameters:
baseline (
Optional[Dict[str,Tensor]]) – Per-feature baseline tensors.Noneuses the default strategy (UNK-token for discrete features, near-zero for continuous features).steps (
Optional[int]) – Number of interpolation steps. Overrides the instance default when given.target_class_idx (
Optional[int]) – Target class for attribution. For binary classification (single logit output), this is a no-op.Noneuses the argmax of model output.**kwargs (
Tensor|tuple[Tensor,...]) – Dataloader batch (feature tensors + optional labels).
- Return type:
- Returns:
Dictionary mapping each feature key to an attribution tensor whose shape matches the raw input value tensor.