PyTorch Module
The signxai.torch_signxai
module provides explainability methods for PyTorch models, leveraging the Zennit library for LRP implementations.
Main Functions
This module provides two API styles: a PyTorch-native style and a TensorFlow-compatible style.
PyTorch-Native API
- signxai.torch_signxai.calculate_relevancemap(model, input_tensor, method='gradients', **kwargs)[source]
Calculates a relevance map for a given model, input, and method in a PyTorch-native style.
- Parameters:
model (torch.nn.Module) – PyTorch model
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
method (str, optional) – Name of the explanation method
kwargs – Additional arguments for the specific method
- Returns:
Relevance map as numpy array
- Return type:
- signxai.torch_signxai.calculate_relevancemaps(model, input_tensors, method='gradients', **kwargs)
Calculates relevance maps for multiple inputs in a PyTorch-native style.
- Parameters:
model (torch.nn.Module) – PyTorch model
input_tensors (list or torch.Tensor or numpy.ndarray) – List or batch of input tensors
method (str, optional) – Name of the explanation method
kwargs – Additional arguments for the specific method
- Returns:
List or batch of relevance maps
- Return type:
TensorFlow-Compatible API
- signxai.torch_signxai.methods.wrappers.calculate_relevancemap(method_name, x, model_no_softmax, **kwargs)[source]
Calculates a relevance map using the TensorFlow-compatible API style.
- Parameters:
method_name (str) – Name of the explanation method
x (numpy.ndarray or torch.Tensor) – Input tensor
model_no_softmax (torch.nn.Module) – PyTorch model with softmax removed
kwargs – Additional arguments for the specific method
- Returns:
Relevance map as numpy array
- Return type:
- signxai.torch_signxai.methods.wrappers.calculate_relevancemaps(method_name, X, model_no_softmax, **kwargs)[source]
Calculates relevance maps for multiple inputs using the TensorFlow-compatible API style.
- Parameters:
method_name (str) – Name of the explanation method
X (list or numpy.ndarray or torch.Tensor) – List or batch of input tensors
model_no_softmax (torch.nn.Module) – PyTorch model with softmax removed
kwargs – Additional arguments for the specific method
- Returns:
List or batch of relevance maps
- Return type:
Zennit Integration
The module signxai.torch_signxai.methods.zennit_impl
provides Zennit-based implementations of explanation methods.
- class signxai.torch_signxai.methods.zennit_impl.GradientAnalyzer(model)[source]
Implements vanilla gradient calculation aligned with TensorFlow’s implementation.
- Parameters:
model (torch.nn.Module) – PyTorch model
- analyze(input_tensor, target_class=None)[source]
Generate vanilla gradient attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Gradient attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.IntegratedGradientsAnalyzer(model, steps=50, baseline=None)[source]
Implements integrated gradients by integrating gradients along a straight path from baseline to input.
- Parameters:
model (torch.nn.Module) – PyTorch model
steps (int, optional) – Number of steps for integration
baseline (torch.Tensor, optional) – Baseline input (None for zeros)
- analyze(input_tensor, target_class=None)[source]
Generate integrated gradients attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Integrated gradients attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.SmoothGradAnalyzer(model, noise_level=0.2, num_samples=50)[source]
Implements SmoothGrad by adding Gaussian noise to the input multiple times and averaging the resulting gradients.
- Parameters:
model (torch.nn.Module) – PyTorch model
noise_level (float, optional) – Level of Gaussian noise to add
num_samples (int, optional) – Number of noisy samples to average
- analyze(input_tensor, target_class=None)[source]
Generate SmoothGrad attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
SmoothGrad attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.GuidedBackpropAnalyzer(model)[source]
Implements guided backpropagation by modifying the backward pass of ReLU to only pass positive gradients.
- Parameters:
model (torch.nn.Module) – PyTorch model
- analyze(input_tensor, target_class=None)[source]
Generate guided backpropagation attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Guided backpropagation attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.GradientXInputAnalyzer(model)[source]
Implements gradient × input method for enhanced feature attribution.
- Parameters:
model (torch.nn.Module) – PyTorch model
- analyze(input_tensor, target_class=None)[source]
Generate gradient × input attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Gradient × input attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.GradientXSignAnalyzer(model, mu=0.0)[source]
Implements gradient × sign method with configurable threshold parameter.
- Parameters:
model (torch.nn.Module) – PyTorch model
mu (float, optional) – Threshold parameter for sign calculation
- analyze(input_tensor, target_class=None)[source]
Generate gradient × sign attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Gradient × sign attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.VarGradAnalyzer(model, num_samples=50, noise_level=0.2)[source]
Implements variance of gradients across multiple noisy samples.
- Parameters:
model (torch.nn.Module) – PyTorch model
num_samples (int, optional) – Number of noisy samples to average
noise_level (float, optional) – Level of Gaussian noise to add
- analyze(input_tensor, target_class=None)[source]
Generate VarGrad attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
VarGrad attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.DeepTaylorAnalyzer(model, epsilon=1e-6)[source]
Implements Deep Taylor decomposition using LRP epsilon as proxy.
- Parameters:
model (torch.nn.Module) – PyTorch model
epsilon (float, optional) – Stabilizing factor for epsilon rule
- analyze(input_tensor, target_class=None)[source]
Generate Deep Taylor attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Deep Taylor attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.GradCAMAnalyzer(model, target_layer=None)[source]
Implements Grad-CAM by using the gradients of a target class with respect to feature maps of a convolutional layer.
- Parameters:
model (torch.nn.Module) – PyTorch model
target_layer (torch.nn.Module, optional) – Target convolutional layer (None to auto-detect)
- analyze(input_tensor, target_class=None)[source]
Generate Grad-CAM attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
Grad-CAM attribution
- Return type:
Layer-wise Relevance Propagation (LRP)
The Zennit library is used to implement various LRP variants.
- class signxai.torch_signxai.methods.zennit_impl.LRPAnalyzer(model, rule='epsilon', epsilon=1e-6)[source]
Layer-wise Relevance Propagation analyzer using Zennit’s implementation.
- Parameters:
model (torch.nn.Module) – PyTorch model
rule (str, optional) – LRP rule (‘epsilon’, ‘zplus’, ‘alphabeta’)
epsilon (float, optional) – Stabilizing factor for epsilon rule
- analyze(input_tensor, target_class=None)[source]
Generate LRP attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
LRP attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.AdvancedLRPAnalyzer(model, rule_type, **kwargs)[source]
Advanced LRP analyzer with specialized rules and composites.
- Parameters:
model (torch.nn.Module) – PyTorch model
rule_type (str) – Type of LRP rule/composite
kwargs – Additional parameters for specific rules
Available rule types:
“alpha1beta0”: Alpha-Beta rule with alpha=1, beta=0
“alpha2beta1”: Alpha-Beta rule with alpha=2, beta=1
“epsilon”: Epsilon rule with custom epsilon value
“gamma”: Gamma rule with custom gamma value
“flat”: Flat rule
“wsquare”: W-Square rule
“zbox”: Z-Box rule with custom low/high values
“sequential”: Sequential application of different rules
- analyze(input_tensor, target_class=None)[source]
Generate advanced LRP attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
LRP attribution
- Return type:
- class signxai.torch_signxai.methods.zennit_impl.LRPSequential(model, first_layer_rule='zbox', middle_layer_rule='alphabeta', last_layer_rule='epsilon', **kwargs)[source]
Implements LRP with sequential application of different rules to different layers.
- Parameters:
model (torch.nn.Module) – PyTorch model
first_layer_rule (str, optional) – Rule for first layers
middle_layer_rule (str, optional) – Rule for middle layers
last_layer_rule (str, optional) – Rule for last layers
kwargs – Additional parameters for specific rules
- analyze(input_tensor, target_class=None)[source]
Generate sequential LRP attribution.
- Parameters:
input_tensor (torch.Tensor or numpy.ndarray) – Input tensor
target_class (int, optional) – Target class index (None for argmax)
- Returns:
LRP attribution
- Return type:
SIGN Methods
The SIGN methods are implemented for PyTorch models as well.
- signxai.torch_signxai.methods.signed.calculate_sign_mu(x, mu=0)[source]
Calculates the sign with a threshold parameter mu for PyTorch inputs.
- Parameters:
x (torch.Tensor or numpy.ndarray) – Input tensor
mu (float) – Threshold parameter (default: 0)
- Returns:
Sign tensor
- Return type:
torch.Tensor or numpy.ndarray (matches input type)
Utility Functions
- signxai.torch_signxai.utils.remove_softmax(model)[source]
Removes the softmax activation from a PyTorch model.
- Parameters:
model (torch.nn.Module) – PyTorch model
- Returns:
Model with softmax removed (outputs raw logits)
- Return type:
- class signxai.torch_signxai.utils.NoSoftmaxWrapper(model)[source]
Wrapper class that removes softmax from a PyTorch model.
- Parameters:
model (torch.nn.Module) – PyTorch model with softmax
- forward(x)[source]
Forward pass that returns logits directly (no softmax).
- Parameters:
x (torch.Tensor) – Input tensor
- Returns:
Model output before softmax
- Return type: