Skip to content

Functional utilities

Helpers for constructing attribution baselines and normalizing scores. For practical baseline guidance, see Choosing a baseline.

tslens.get_baseline

get_baseline(inputs: Union[Tensor, Tuple[Tensor, ...]], mode: str = 'zero') -> Union[torch.Tensor, Tuple[torch.Tensor, ...]]

Build a baseline tensor (or tuple of them) matching inputs.

Parameters:

Name Type Description Default
inputs Union[Tensor, Tuple[Tensor, ...]]

tensor or tuple of tensors shaped (batch, seq_len, features).

required
mode str

zero, random, normal or mean.

'zero'

tslens.normalize_scale

normalize_scale(data: Tensor, dim: int = 1, norm_type: str = 'standard', legacy: bool = False) -> torch.Tensor

Normalize data along dim.

Parameters:

Name Type Description Default
data Tensor

tensor to normalize.

required
dim int

dimension to reduce over.

1
norm_type str

one of standard, minmax or l1.

'standard'
legacy bool

reproduce the exact minmax behaviour of the original WinTSR research code, which normalizes the whole tensor by the first slice's min/max instead of per-slice. See :class:tslens.WinTSR for why this switch exists. Ignored for other norm_type values.

False