pyhealth.interpret.methods.baseline#
Overview#
pyhealth.interpret.methods.RandomBaseline assigns random importance
scores to input features. It works with any PyHealth model and needs no
gradients, and it is intended as a sanity-check baseline: a meaningful
attribution method should be clearly more faithful to the model than random
scores. See examples/interpretability/interpret_playground.py for usage
alongside other methods.
API Reference#
- class pyhealth.interpret.methods.RandomBaseline(model, random_seed=None, distribution='uniform')[source]#
Bases:
BaseInterpreterRandom feature attribution baseline for PyHealth models.
This interpreter assigns uniformly random importance scores to input features. It is intended as a sanity-check baseline: any meaningful interpretability method should produce attributions that are significantly more faithful to the model than random scores.
The method works with all PyHealth models and requires no gradients, embeddings, or special model capabilities.
- Parameters:
model (
BaseModel) – A trained PyHealth model to interpret. Can be any model that inherits fromBaseModel.random_seed (
Optional[int]) – Optional random seed for reproducibility. When set, the same inputs will always receive the same random attributions. Default isNone(non-deterministic).distribution (
str) – Distribution used to sample importances. One of"uniform"(values in[0, 1]),"normal"(standard normal), or"signed_uniform"(values in[-1, 1]). Default is"uniform".
Examples
>>> import torch >>> from pyhealth.datasets import create_sample_dataset, get_dataloader >>> from pyhealth.interpret.methods import RandomBaseline >>> from pyhealth.models import MLP >>> >>> samples = [ ... {"patient_id": "p0", "visit_id": "v0", ... "conditions": ["cond-33", "cond-86", "cond-80"], ... "procedures": [1.0, 2.0, 3.5, 4.0], "label": 1}, ... {"patient_id": "p1", "visit_id": "v1", ... "conditions": ["cond-55", "cond-12"], ... "procedures": [5.0, 2.0, 3.5, 4.0], "label": 0}, ... ] >>> dataset = create_sample_dataset( ... samples=samples, ... input_schema={"conditions": "sequence", "procedures": "tensor"}, ... output_schema={"label": "binary"}, ... ) >>> model = MLP(dataset=dataset, embedding_dim=32, hidden_dim=32) >>> model.eval() >>> test_loader = get_dataloader(dataset, batch_size=1, shuffle=False) >>> >>> rand_interp = RandomBaseline(model, random_seed=42) >>> batch = next(iter(test_loader)) >>> attributions = rand_interp.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(**kwargs)[source]#
Compute random attributions for input features.
Generates random importance scores with the same shape as each input feature tensor. No gradients or forward passes are needed.
- Parameters:
**kwargs (
Tensor|tuple[Tensor,...]) – Input data dictionary from a dataloader batch. Should contain feature tensors (or tuples of tensors) keyed by the model’s feature keys, plus optional label or metadata tensors (which are ignored).- Return type:
- Returns:
Dictionary mapping each feature key to a random attribution tensor whose shape matches the raw input values.