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.explain(model, input_tensor, method_name, **kwargs)

Calculates a relevance map using the new dynamic parsing API.

Parameters:
  • model (torch.nn.Module) – PyTorch model

  • input_tensor (torch.Tensor or numpy.ndarray) – Input tensor

  • method_name (str) – Name of the explanation method with embedded parameters (e.g., ‘lrp_epsilon_0_25’, ‘smoothgrad_noise_0_3_samples_50’)

  • kwargs – Additional arguments for the specific method

Returns:

Relevance map as numpy array

Return type:

numpy.ndarray

Note: The old calculate_relevancemap(model, input_tensor, method="gradients") API is deprecated. Use this new unified API instead.

For multiple inputs, use explain() in a loop or with batch processing:

from signxai.api import explain

# For multiple inputs
explanations = []
for input_tensor in input_tensors:
    explanations.append(explain(model, input_tensor, method_name="gradient"))

# Or for batch processing (if supported by the method)
batch_explanation = explain(model, batch_inputs, method_name="gradient")

TensorFlow-Compatible API

Deprecated: The TensorFlow-compatible API style is now replaced by the unified explain() API.

Instead of calculate_relevancemap(method_name, x, model_no_softmax) use:

from signxai.api import explain
explanation = explain(model_no_softmax, x, method_name=method_name)

Deprecated: Use the new unified explain() API instead:

from signxai.api import explain

# For multiple inputs
explanations = []
for input_tensor in X:
    explanations.append(explain(model_no_softmax, input_tensor, method_name=method_name))

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)

Implements vanilla gradient calculation aligned with TensorFlow’s implementation.

Parameters:

model (torch.nn.Module) – PyTorch model

analyze(input_tensor, target_class=None)

Generate vanilla gradient attribution.

New API: Use explain(model, input_tensor, method_name="gradient")

Parameters:
Returns:

Gradient attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.IntegratedGradientsAnalyzer(model, steps=50, baseline=None)

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)

Generate integrated gradients attribution.

New API: Use explain(model, input_tensor, method_name="integrated_gradients_steps_50") or other step values like integrated_gradients_steps_100

Parameters:
Returns:

Integrated gradients attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.SmoothGradAnalyzer(model, noise_level=0.2, num_samples=50)

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)

Generate SmoothGrad attribution.

New API: Use explain(model, input_tensor, method_name="smoothgrad_noise_0_2_samples_50") or other parameter combinations

Parameters:
Returns:

SmoothGrad attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.GuidedBackpropAnalyzer(model)

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)

Generate guided backpropagation attribution.

Parameters:
Returns:

Guided backpropagation attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.GradientXInputAnalyzer(model)

Implements gradient × input method for enhanced feature attribution.

Parameters:

model (torch.nn.Module) – PyTorch model

analyze(input_tensor, target_class=None)

Generate gradient × input attribution.

New API: Use explain(model, input_tensor, method_name="gradient_x_input")

Parameters:
Returns:

Gradient × input attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.GradientXSignAnalyzer(model, mu=0.0)

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)

Generate gradient × sign attribution.

New API: Use explain(model, input_tensor, method_name="gradient_x_sign") or with mu values like gradient_x_sign_mu_0_5 or gradient_x_sign_mu_neg_0_5

Parameters:
Returns:

Gradient × sign attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.VarGradAnalyzer(model, num_samples=50, noise_level=0.2)

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)

Generate VarGrad attribution.

Parameters:
Returns:

VarGrad attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.DeepTaylorAnalyzer(model, epsilon=1e-6)

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)

Generate Deep Taylor attribution.

Parameters:
Returns:

Deep Taylor attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.GradCAMAnalyzer(model, target_layer=None)

Implements Grad-CAM by using the gradients of a target class with respect to feature maps of a convolutional layer.

Parameters:
analyze(input_tensor, target_class=None)

Generate Grad-CAM attribution.

Parameters:
Returns:

Grad-CAM attribution

Return type:

numpy.ndarray

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)

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)

Generate LRP attribution.

New API: Use explain(model, input_tensor, method_name="lrp_epsilon_0_25") or other variants like lrp_alpha_1_beta_0

Parameters:
Returns:

LRP attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.AdvancedLRPAnalyzer(model, rule_type, **kwargs)

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)

Generate advanced LRP attribution.

Parameters:
Returns:

LRP attribution

Return type:

numpy.ndarray

class signxai.torch_signxai.methods.zennit_impl.LRPSequential(model, first_layer_rule='zbox', middle_layer_rule='alphabeta', last_layer_rule='epsilon', **kwargs)

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)

Generate sequential LRP attribution.

Parameters:
Returns:

LRP attribution

Return type:

numpy.ndarray

SIGN Methods

The SIGN methods are implemented for PyTorch models as well.

signxai.torch_signxai.methods.signed.calculate_sign_mu(x, mu=0)

Calculates the sign with a threshold parameter mu for PyTorch inputs.

New API: This is typically used internally, but sign-based methods can be called with explain(model, input_tensor, method_name="gradient_x_sign_mu_0_5") or gradient_x_sign_mu_neg_0_5

Parameters:
Returns:

Sign tensor

Return type:

torch.Tensor or numpy.ndarray (matches input type)

Utility Functions

signxai.torch_signxai.utils.remove_softmax(model)

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:

torch.nn.Module

class signxai.torch_signxai.utils.NoSoftmaxWrapper(model)

Wrapper class that removes softmax from a PyTorch model.

Parameters:

model (torch.nn.Module) – PyTorch model with softmax

forward(x)

Forward pass that returns logits directly (no softmax).

Parameters:

x (torch.Tensor) – Input tensor

Returns:

Model output before softmax

Return type:

torch.Tensor